Xah Talk Show 2025-12-19 Ep728 Wolfram Language, Advent of Code 2025, Day 3, part 2 (failed)
Video Summary (Generated by AI, Edited by Human.)
Introduction to Advent of Code and Wolfram Language: The video starts with the host introducing the Advent of Code 2025 Day 3 Part 2 challenge, emphasizing the use of Wolfram Language (0:03).
Critique of Advent of Code Website: The host criticizes the Advent of Code website for its hardcoded layout, dark theme, and readability issues (1:34).
Problem Statement (Part 1 Review): The host explains the Day 3 Part 1 problem, which involves picking two digits from a number, combining them, and finding the maximum, then summing these maximums (2:49).
Problem Statement (Part 2): The focus shifts to Part 2, which requires picking 12 digits (6:04).
Initial Approach and Challenges with Recursion: The host attempts to solve the problem using a recursive function in Wolfram Language, encountering issues with handling the first step differently from subsequent steps (28:09, 58:40, 1:00:12).
Exploring Wolfram Language Functions (PositionLargest, TakeLargest): The host investigates Wolfram Language's built-in functions like PositionLargest and TakeLargest to find the largest digits and their positions (1:16:36, 1:51:53).
Issues with TakeLargest and Sorting: It's discovered that TakeLargest reorders elements in descending order, which is not desired for the problem (1:18:48).
Further Challenges and Debugging: The host continues to face difficulties in getting the correct output for specific examples, highlighting issues with sorting and truncating the results (2:19:02, 2:25:19, 2:31:42, 2:34:45).
Reflecting on Wolfram Language Syntax and Programming Paradigms: The host discusses the flexibility and potential complexities of Wolfram Language syntax, comparing it to other programming paradigms like functional programming vs. imperative/procedural languages (2:51:51).
Frustration with Advent of Code: The host expresses frustration with the Advent of Code problems, noting their time-consuming nature (2:45:38).
- Wolfram Language for Programers
- Wolfram: Download Wolfram Engine
- Emacs: Xah Wolfram Mode 📦
- Emacs: Xah Fly Keys 📦
(* experiment. most intuitive, procedural way. *) xdigitList = IntegerDigits[818181911112111] (* {8, 1, 8, 1, 8, 1, 9, 1, 1, 1, 1, 2, 1, 1, 1} *) (* the code will roughly have these steps *) xdigit1 = Max @ Drop[xdigitList,-11] xdigit1Position = Position[ xdigitList, xdigit1, {1}, 1]; xdigit2 = Max @ Drop[xdigitList, Flatten[{1, xdigit1Position }] ]; xdigit2Position = Position[ xdigitList, xdigit2, {1}, 1]; xdigit3 = Max @ Drop[xdigitList, Flatten[{1, xdigit2Position }] ]; xdigit3Position = Position[ xdigitList, xdigit3, {1}, 1];
(* algo steps. we gonna do recursion. write a function pickDigit. pickDigit takes 2 args pickDigit( resultList, InputList) and return a list like this {newResultList, newInputList} where new newResultList has a choosen digit appended to resultList. and newInputList is rest of list to pick a digit in next round. then the result is the First Part of Fold[pickDigit , xinput] *)
(* algo steps. write a function pickDigit(resultList, InputList) and return a list {newResultList, newInputList} where newResultList has a choosen digit appended to resultList. and newInputList is rest of list to pick a digit in next round. *) (* this approach is abanoned. because, the First iteration differ from rest iteration. the first iteration needs to truncate list from tail. the rest iterations need to truncate list from head. therefore, you cannot make them into one single recursion. you need to, either do the first step manually, then recurse. or, re-design your function so it does not have this issue. *) Clear[pickDigit] pickDigit[ resultList_, xInputList_ ] := With[{xDigit = Max @ xInputList}, With[{xPosition = Position[ xInputList, xDigit, {1}, 1]}, { Append[resultList, xDigit], Drop[xInputList, Flatten[{1, xPosition }] ] }]] pickDigit[ {}, {8, 1, 8, 1} ] Drop[IntegerDigits[818181911112111],-11] (* {8, 1, 8, 1} *) (* s------------------------------ *) (* experiment on the final recursion *) Fold[ pickDigit, {} , Drop[IntegerDigits[x],-11] ] xFirstResult = pickDigit[{} , Drop[IntegerDigits[x],-11] ] Fold[ pickDigit , xFirstResult , Drop[xInputList, Flatten[{1, Position[ xInputList, First @ xFirstResult, {1}, 1] }] ] ]
(* we realized, we can use this *) PositionLargest[{8, 7, 8, 2, 3, 9} , 3 ] (* {{6}, {3, 1}, {2}} *) Part[{8, 7, 8, 2, 3, 9} , 6] (* 9 *) Flatten @ {{6}, {3, 1}, {2}} (* {6, 3, 1, 2} *) Sort[{6, 3, 1, 2}] (* {1, 2, 3, 6} *) Part[{8, 7, 8, 2, 3, 9} , {1, 2, 3, 6}] (* {8, 7, 8, 9} *) (* s------------------------------ *) (* now we compact it, and have this *) xlist = {8, 7, 8, 2, 3, 9}; Part[xlist , Sort @ Flatten @ PositionLargest[xlist , 3 ] ] (* {8, 7, 8, 9} *)
xinput= {987654321111111, 811111111111119, 234234234234278, 818181911112111}; PositionLargest[IntegerDigits @ 818181911112111 , 12 ] (* {{7}, {5, 3, 1}, {12}, {15, 14, 13, 11, 10, 9, 8, 6, 4, 2}} *) xlist = IntegerDigits @ 818181911112111; Part[xlist , Sort @ Function[x, Take[x, 12 ]] @ Flatten @ PositionLargest[xlist , 12 ] ] (* {8, 8, 8, 9, 1, 1, 1, 1, 2, 1, 1, 1} *) PositionLargest[IntegerDigits @ 234234234234278 , 12 ] (* {{15}, {14}, {12, 9, 6, 3}, {11, 8, 5, 2}, {13, 10, 7, 4, 1}} *) PositionLargest[ {2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 7, 8} , 10 ] (* {{15}, {14}, {12, 9, 6, 3}, {11, 8, 5, 2}, {13, 10, 7, 4, 1}} *) Sort @ {{15}, {14}, {12, 9, 6, 3}, {11, 8, 5, 2}, {13, 10, 7, 4, 1}} (* {{14}, {15}, {11, 8, 5, 2}, {12, 9, 6, 3}, {13, 10, 7, 4, 1}} *) Map[ Sort , {{15}, {14}, {12, 9, 6, 3}, {11, 8, 5, 2}, {13, 10, 7, 4, 1}}] (* {{15}, {14}, {3, 6, 9, 12}, {2, 5, 8, 11}, {1, 4, 7, 10, 13}} *) Flatten @ (Sort /@ {{15}, {14}, {12, 9, 6, 3}, {11, 8, 5, 2}, {13, 10, 7, 4, 1}}) {15, 14, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, 10, 13} be back in 10 minutes (* s------------------------------ *) xlist = IntegerDigits @ 234234234234278; Part[xlist , Sort @ Function[x, Take[x, 12 ]] @ Flatten @ PositionLargest[xlist , 12 ] ] (* {3, 4, 3, 4, 3, 4, 2, 3, 4, 2, 7, 8} *) xlist = IntegerDigits @ 234234234234278; Part[xlist , Sort @ Function[x, Take[x, 12 ]] @ Flatten @ PositionLargest[xlist , 12 ] ] (* {3, 4, 3, 4, 3, 4, 2, 3, 4, 2, 7, 8} *) Map[ Function[xnum, With[{xlist = IntegerDigits[xnum]}, FromDigits @ Part[xlist, Function[x, Take[x, 12 ]] @ Flatten @ (Sort /@ PositionLargest[xlist , 12 ]) ] ] ] , xinput] (* {987654321111, 811111111111, 234234234234, 818181911112} *) (* 2851181577568 *)
xinput= {987654321111111, 811111111111119, 234234234234278, 818181911112111}; Map[ Function[xnum, With[{xlist = IntegerDigits[xnum]}, FromDigits @ Part[xlist, Function[x, Take[x, 12 ]] @ Sort @ Flatten @ ( PositionLargest[xlist , 12 ]) ] ] ] , xinput] (* {987654321111, 811111111111, 234234234234, 818181911112} *) (* {987654321111, 811111111119, 234234343478, 818181911112} *) (* {987654321111, 811111111111, 234234234234, 818181911112} *) (* 2851181577568 *)
Tuples[IntegerDigits @ 2217222212362341131525335321122324234324223212742131242212142527224532246323122744833459212221122131 , 12] // Length (* Tuples::toomany: The length of the output of Tuples[{2, 2, 1, 7, 2, 2, 2, 2, 1, 2, 3, 6, 2, 3, 4, 1, 1, 3, 1, 5, 2, 5, 3, 3, 5, 3, 2, 1, 1, 2, 2, 3, 2, 4, 2, 3, 4, 3, 2, 4, 2, 2, 3, 2, 1, 2, 7, 4, 2, 1, 3, 1, 2, 4, 2, 2, 1, 2, 1, 4, 2, 5, 2, 7, 2, 2, 4, 5, 3, 2, 2, 4, 6, 3, 2, 3, 1, 2, 2, 7, 4, 4, 8, 3, 3, 4, 5, 9, 2, 1, 2, 2, 2, 1, 1, 2, 2, 1, 3, 1}, 12] should be a machine integer. *) (* 2 *)
(* experiment on using TakeLargest. won't work. because if input is 666666666666666666666666666666666666789 result would be twelve 6. but TakeLargest would includ 7 8 9 in it. *) xlist = IntegerDigits @ 234234234234278; (* {2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 7, 8} *) xlargests = TakeLargest[ xlist , 12]; (* {8, 7, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2} *) xpositions = Map[ Function[x, Position[xlist, x]], xlargests] (* {{{15}}, {{14}}, {{3}, {6}, {9}, {12}}, {{3}, {6}, {9}, {12}}, {{3}, {6}, {9}, {12}}, {{3}, {6}, {9}, {12}}, {{2}, {5}, {8}, {11}}, {{2}, {5}, {8}, {11}}, {{2}, {5}, {8}, {11}}, {{2}, {5}, {8}, {11}}, {{1}, {4}, {7}, {10}, {13}}, {{1}, {4}, {7}, {10}, {13}}} *)
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, Problem 2
- Xah Talk Show 2025-12-11 Ep723 Wolfram Language, Advent of Code 2025, Day 1, Problem 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, Problem 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)