Xah Talk Show 2026-01-02 Ep736 Wolfram Language, Advent of Code 2025, Day 6
Video Summary (Generated by AI, Edited by Human.)
- Introduction to Advent of Code and Wolfram Language (0:08-3:51).
- Discuss the typical time it takes to solve Advent of Code problems, noting that later problems can take several hours or days (1:00-1:27).
- On "speed coders" who solve problems extremely fast, sometimes in a minute, and the removal of the global leaderboard due to abuse (1:44-2:40).
- for those new to Wolfram Language, watch talk show episode (3:04-3:27) and a tutorial on his website (3:31-3:43), and download link for the Wolfram Engine (3:44-3:52).
- Problem Description: Day 6, Problem 1 (6:19-12:12).
- The input is a list of problems arranged vertically, where each problem consists of numbers and an operation symbol (+ or *) at the bottom.
- Problems are separated by columns of spaces.
- The goal is to calculate the sum of all individual problem answers to find the "grand total.". On using matrix transposition (11:23-12:02).
- Initial Approach (12:20-33:00):
- Starts by preparing the input data, converting the raw text into a matrix of strings (13:49-21:13).
- special handle of the last row, which contains operation symbols.
- Separates the operation list from the number matrix (24:44-25:56).
- Core solution involves transposing the number matrix and converting its elements to numbers (25:57-26:02).
- Use MapThread to apply the correct operation (sum or product) to each row of numbers based on the corresponding operator (27:56-32:00).
- Debugging and Solution (35:10-43:05): The initial solution yields an incorrect grand total (35:28).
- Debug. Checking intermediate results (39:03-40:07) and realizing that a simple syntax error in his conditional logic was causing the issue (42:33-42:40).
- The correct solution is obtained (42:50-43:05).
- Refining and Shortening the Code (46:24-1:19:00). Compacting the code into a "one-line" solution (46:24-1:19:00).
- This involves:
- Use string replacement to change '+' and '*' characters to Wolfram Language's Plus and Times symbols (1:00:59-1:01:11, 1:14:53-1:15:04).
- Leveraging RotateRight to move the operator row to the beginning (1:15:18-1:15:26).
- Apply ToExpression to convert all strings in the matrix to expressions (numbers and function symbols) (1:15:28-1:15:33).
- Use Transpose to align operators with their respective number lists (1:15:37-1:15:53).
- Finally, use Map with Apply to perform the operation (first element) on the rest of the elements in each row (1:15:54-1:16:10), followed by Total to sum the results (1:16:13-1:16:15).
- Claim that this compact Wolfram Language solution is likely the shortest possible in any programming language by token count (1:16:21-1:17:14).
solution
Times @@ {3,4} (* 12 *) (* the at at is a shortcut syntax for *) Apply[Times , {3,4}] (* 12 *)
Total[{3,4}] (* 7 *) Apply[Plus , {3,4}] (* 7 *)
xinput = "123 328 51 64 45 64 387 23 6 98 215 314 * + * +"; (* first, turn the text block into a matrix of string*) xinputStringMatrix = Map[ Function[ xline, StringSplit[ xline] ] , StringSplit[ xinput, "\n"] ] (* {{123, 328, 51, 64}, {45, 64, 387, 23}, {6, 98, 215, 314}, {*, +, *, +}} *) (* extract the last row as list of operator strings *) xoperatorList = Last @ xinputStringMatrix; (* now, transpose the top rows, then turn them from string to number *) xnumberMatrix = ToExpression @ Transpose @ Most @ xinputStringMatrix; Total @ MapThread[ Function[{numRow, xops}, If[ xops === "+", Apply[Plus , numRow], Apply[Times , numRow] ] ] , {xnumberMatrix, xoperatorList} ] (* 4277556 *)
compact code, one-line
Total @ MapThread[ Function[{numRow, xops}, If[ xops === "+", Apply[Plus , numRow], Apply[Times , numRow] ] ] , Function[x, { ToExpression @ Transpose @ Most @ x, Last @ x}] @ Map[ Function[ x, StringSplit @ x ] , StringSplit[ "123 328 51 64 45 64 387 23 6 98 215 314 * + * +", "\n"] ] ] (* 4277556 *)
new algo. shortest code
xinput = "123 328 51 64 45 64 387 23 6 98 215 314 * + * +"; Total @ Map[ Function[x, Apply[ First @ x, Rest @ x]] , Transpose @ ToExpression @ RotateRight @ Map[ StringSplit , StringSplit[ StringReplace[ xinput, {"+" -> "Plus", "*" -> "Times"}] , "\n"] ] ] (* 4277556 *)
Advent of Code 2025
- Xah Talk Show 2025-12-08 Ep720 Wolfram Language, Advent of Code 2025, Day 1
- Xah Talk Show 2025-12-09 Ep721 Wolfram Language, Advent of Code 2025, Day 1, Part 2
- Xah Talk Show 2025-12-11 Ep723 Wolfram Language, Advent of Code 2025, Day 1, Part 2, take 2
- Xah Talk Show 2025-12-12 Ep724 Wolfram Language, Advent of Code 2025, Day 2
- Xah Talk Show 2025-12-15 Ep725 Wolfram Language, Advent of Code 2025, Day 2, Part 2
- Xah Talk Show 2025-12-17 Ep726 Wolfram Language, Advent of Code 2025, Day 3 (aborted)
- Xah Talk Show 2025-12-18 Ep727 Wolfram Language, Advent of Code 2025, Day 3, take 2
- Xah Talk Show 2025-12-19 Ep728 Wolfram Language, Advent of Code 2025, Day 3, Part 2 (failed)
- Xah Talk Show 2025-12-20 Ep729 Wolfram Language, Advent of Code 2025, Day 4
- Xah Talk Show 2025-12-21 Ep730 Wolfram Language, Advent of Code 2025, Day 4, take 2
- Xah Talk Show 2025-12-22 Ep731 Wolfram Language, Advent of Code 2025, Day 4, Part 2. Wolfram vs SageMath
- Xah Talk Show 2025-12-23 Ep732 Wolfram Language, Advent of Code 2025, Day 4, Part 2. take 2
- Xah Talk Show 2025-12-26 Ep733 Wolfram Language, Advent of Code 2025, Day 5
- Xah Talk Show 2025-12-27 Ep734 Wolfram Language, Advent of Code 2025, Day 5, Part 2 (failed)
- Xah Talk Show 2026-01-02 Ep736 Wolfram Language, Advent of Code 2025, Day 6
- Xah Talk Show 2026-01-05 Ep738 Wolfram Language, Advent of Code 2025, Day 6, Part 2