Xah Talk Show 2026-01-05 Ep738 Wolfram Language, Advent of Code 2025, Day 6, Part 2
Video Summary (Generated by AI, Edited by Human.)
- This video is a live coding session where the host, Xah Lee, works on solving Advent of Code 2025, Day 6, Part 2, using Wolfram Language (0:09).
- Explain the problem statement for Part 1 (1:58) and shows his solution in Wolfram Language (3:05).
- The core of the video then shifts to understanding the more complex problem of Part 2 (3:50).
- The speaker spends a significant amount of time understanding the problem, which involves reading numbers from right to left, column by column, with specific alignment rules (4:50).
- Initially attempts an algorithm that involves padding numbers with zeros (17:01), but then realizes his understanding of the problem's alignment rules is incorrect (58:20, 1:01:46).
- A key revelation (1:11:03) occurs when he understands that the exact spacing in the input text is crucial and must be preserved, leading to a new algorithm where the input is treated as a matrix of single characters (1:13:31).
- The video concludes with him starting to implement this new approach (1:16:50), focusing on converting the text into a character matrix and then transposing it (1:17:41).
- He also highlights a sneaky aspect of the Advent of Code problem, where trailing spaces in the input lines are significant (1:21:13).
analysis of the problem (incorrect take)
s------------------------------
Analysis
consider this column
64
23
314
+
we want to pad it like this
640
230
314
+
and for each single digit column, starting from right, turn it into a number.
so that we have
4
431
623
s------------------------------
Algo Description.
we already turned it into a list, like this
{"+", "64", "23", "314"}
for each row, find the max number. (max length of string).
e.g. 3 because 314.
then, for each number, pad "0" to the right, by the max length.
e.g. {"+", "640", "230", "314"}
then, consider all these number, turn it into a matrix, each elementis a single digit.
{
{"6","4","0"},
{"2","3","0"},
{"3","1","4"}
}
then, do a transpose. we get
{
{6, 2, 3},
{4, 3, 1},
{0, 0, 4}
}
then, join string, and turn it into a number.
{
623,
431,
4
}
then, just apply the operator to these numbers.
Realization. our previous understanding of the problem is incorrect
actually, the given input text, the number of spaces between columns is significant.
look at the first column and the last column
123 328 51 64 45 64 387 23 6 98 215 314 * + * +
if you just split the string for each row of text, you lose the alignment info.
new algorithm
- consider the input text as a matrix of single chars.
- turn the input text into such a matrix. each element is a single digit, or single space string.
- then, transpose the matrix.
- then, remove rows that are all empty spaces.
- then, turn each row's element from string to number.
- then, for each row, apply the operator.
- then, total them.
Characters @ "123.328.51.64" (* {1, 2, 3, ., 3, 2, 8, ., 5, 1, ., 6, 4} *) MapThread[f , {{a,b,c}, {1,2,3}}] (* {f[a, 1], f[b, 2], f[c, 3]} *)
(* be careful. trailing spaces in input text are significant. If you delete them, it won't work. make sure your text editor does not auto delete trailing spaces. *) (* let's change the input text by replace space to dot. that way, we don't have this unintended deletion of trailing whitespace. *) xinput = "123.328..51.64. .45.64..387.23. ..6.98..215.314 *...+...*...+.."; (* first, turn the text block into a matrix of string, each element is a single char. *) xinputStringMatrix = Map[ Characters , StringSplit[ xinput, "\n"] ] (* { {"1", "2", "3", ".", "3", "2", "8", ".", ".", "5", "1", ".", "6", "4", "."}, {".", "4", "5", ".", "6", "4", ".", ".", "3", "8", "7", ".", "2", "3", "."}, {".", ".", "6", ".", "9", "8", ".", ".", "2", "1", "5", ".", "3", "1", "4"}, {"*", ".", ".", ".", "+", ".", ".", ".", "*", ".", ".", ".", "+", ".", "."}} *) xoperators = Select[ ReplaceAll[Last @ xinputStringMatrix, { "*" -> Times , "+" -> Plus}] , Function[x, UnsameQ["." , x] ]] (* {Times, Plus, Times, Plus} *) xmatrix = StringJoin /@ Transpose @ Most @ xinputStringMatrix (* {"1..", "24.", "356", "...", "369", "248", "8..", "...", ".32", "581", "175", "...", "623", "431", "..4"} *) xsplitMatrix = SplitBy[xmatrix, Function[x, StringContainsQ[x, RegularExpression["[0-9]"]]] ] (* { {"1..", "24.", "356"}, {"..."}, {"369", "248", "8.."}, {"..."}, {".32", "581", "175"}, {"..."}, {"623", "431", "..4"}} *) xemptyRowRemovedMatrix = Select[ xsplitMatrix, Function[x, Length @ x > 1]] (* {{"1..", "24.", "356"}, {"369", "248", "8.."}, {".32", "581", "175"}, {"623", "431", "..4"}} *) xnumberMatrix = Map[ Function[xrow, ToExpression @ Map[ Function[xstr, StringReplace[xstr, "." -> "" ]] , xrow]] , xemptyRowRemovedMatrix] (* {{1, 24, 356}, {369, 248, 8}, {32, 581, 175}, {623, 431, 4}} *) Total @ MapThread[ Function[{xop, xnumList}, Apply[xop, xnumList]] , {xoperators, xnumberMatrix}] (* 3263827 *)
personal solution is 12542543681221
shortened code
xinput = "123.328..51.64. .45.64..387.23. ..6.98..215.314 *...+...*...+.."; With[{xinputStringMatrix = Map[Characters, StringSplit[xinput, "\n"]]}, Total@MapThread[ Function[{xop, xnumList}, Apply[xop, xnumList]], {Select[ ReplaceAll[Last@xinputStringMatrix, {"*" -> Times, "+" -> Plus}], Function[x, UnsameQ[".", x]]], Map[Function[xrow, ToExpression@ Map[Function[xstr, StringReplace[xstr, "." -> ""]], xrow]], Select[SplitBy[StringJoin /@ Transpose@Most@xinputStringMatrix, Function[x, StringContainsQ[x, RegularExpression["[0-9]"]]]], Function[x, Length@x > 1]]]}]] (*3263827*)
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