Xah Talk Show 2025-12-12 Ep724 Wolfram Language, Advent of Code 2025, Day 2
Video Summary (Generated by AI, Edited by Human.)
Introduction to the Problem (0:56-5:00): Xah introduces the Advent of Code Day 2 problem, which involves identifying "invalid product IDs" within given numerical ranges. An invalid ID is defined as a number made only of some sequence of digits repeated twice (e.g., 55, 6464, 123123). Numbers with leading zeros are explicitly stated as not being valid IDs. The task is to find all invalid IDs within specified ranges and sum them up.
Understanding the Invalid ID Pattern (5:25-7:27): Xah notes that if a number has an odd number of digits, it cannot be an invalid ID because a repeated sequence will always result in an even number of digits. He then considers using regular expressions to identify the pattern of repeated digits.
Initial Regular Expression Attempt and Debugging (14:30-46:27): Xah attempts to craft a regular expression using StringMatchQ in Wolfram Language. He encounters unexpected "true" results for patterns that shouldn't match, leading to a long debugging session. He also tries to replicate the issue in Python.
Discovery of Regular Expression Nuances (46:27-56:01): Through the debugging process, Xah realizes two key things:
In Wolfram Language, StringMatchQ requires the pattern to match the entire string, making the ^ (beginning) and $ (end) anchors redundant for this specific function.
The core of the issue was that d+ (one or more digits) followed by d+ was matching any two sequences of digits, even if they were different (e.g., "123" was matching "1" then "23" as two sequences), due to the non-specific nature of the d+ pattern.
Corrected Regular Expression (56:01-59:31): Xah corrects the regular expression by using a capturing group (d+) for the first sequence and then referring back to that captured group with 1 for the second sequence. This ensures that the exact same sequence of digits is repeated (e.g., (d+)1). This corrected regex successfully identifies the invalid IDs.
Further Problem Analysis and Input Handling (1:05:04-1:07:48): Xah reviews the problem statement again and notes that the restriction about leading zeros is effectively redundant because the input ranges are numerical, not string-based, so numbers naturally wouldn't have leading zeros. He then prepares to process the input by splitting the string of ranges.
Beginning of Wolfram Language Implementation (1:08:01-1:18:02): Xah starts coding the solution in Wolfram Language. The plan involves splitting the input string by commas, then splitting each resulting string by a dash to get the start and end of each range. He then intends to generate all numbers within each range and apply the validated regular expression. He briefly discusses the Apply function and its shortcut @@ in Wolfram Language, explaining its use to convert a list of two numbers into arguments for the Range function.
- Wolfram Language for Programers
- Wolfram: Download Wolfram Engine
- Emacs: Xah Wolfram Mode 📦
- Emacs: Xah Fly Keys 📦
- Wolfram: Regular Expression
- Wolfram: Convert to String
- Wolfram: String to Number
- Wolfram: List. Filter
- Wolfram: Apply
import re xx = re.match(r"^(\d+){2}$", "123" ) # it matchs 1 as first sequence of digits # it matchs 23 as second sequence of digits # therefore, result is true. # because the pattern (\d+) is repeated twice. if xx: print("yes") else: print("no")
(* we don't need the anchor ^ and $ because StringMatchQ require the pattern to match entire string. *) StringMatchQ["abc" , RegularExpression[ "ab"] ] (* False *) StringMatchQ["abc" , RegularExpression[ "abc"] ] (* True *) StringMatchQ["abc" , RegularExpression[ "^abc$"] ] (* True *) (* s------------------------------ *) (* this example, return True because the pattern (\\d+) is one or more digits, but not specific. so, the first match is 1, the second match is 23, therefore, the pattern is repeated twice. *) StringMatchQ["123" , RegularExpression["(\\d+){2,2}"] ] (* True *)
Range @@ {3,11} (* {3, 4, 5, 6, 7, 8, 9, 10, 11} *) Apply[Range , {3,11}] (* {3, 4, 5, 6, 7, 8, 9, 10, 11} *)
xinput = "11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124"; xsplitedInput = StringSplit[xinput,","]; (* {11-22, 95-115, 998-1012, 1188511880-1188511890, 222220-222224, 1698522-1698528, 446443-446449, 38593856-38593862, 565653-565659, 824824821-824824827, 2121212118-2121212124} *) newInputList = Map[ Function[x, Apply[Range , Map[ ToExpression, StringSplit[x,"-"]]] ] , xsplitedInput] (* {{11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}, {95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115}, {998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012}, {1188511880, 1188511881, 1188511882, 1188511883, 1188511884, 1188511885, 1188511886, 1188511887, 1188511888, 1188511889, 1188511890}, {222220, 222221, 222222, 222223, 222224}, {1698522, 1698523, 1698524, 1698525, 1698526, 1698527, 1698528}, {446443, 446444, 446445, 446446, 446447, 446448, 446449}, {38593856, 38593857, 38593858, 38593859, 38593860, 38593861, 38593862}, {565653, 565654, 565655, 565656, 565657, 565658, 565659}, {824824821, 824824822, 824824823, 824824824, 824824825, 824824826, 824824827}, {2121212118, 2121212119, 2121212120, 2121212121, 2121212122, 2121212123, 2121212124}} *) (* s------------------------------ *) Select[{998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012}, Function[x, StringMatchQ[ ToString[x] , RegularExpression[ "(\\d+)\\1"] ]] ] (* {1010} *) (* s------------------------------ *) Total @ Flatten @ Map[ Function[xnumberList, Select[xnumberList, Function[x, StringMatchQ[ ToString[x] , RegularExpression[ "(\\d+)\\1"] ]] ]] , newInputList ]
xinput = "8123221734-8123333968,2665-4538,189952-274622,4975-9031,24163352-24202932,1233-1772,9898889349-9899037441,2-15,2147801-2281579,296141-327417,8989846734-8989940664,31172-42921,593312-632035,862987-983007,613600462-613621897,81807088-81833878,13258610-13489867,643517-782886,986483-1022745,113493-167913,10677-16867,372-518,3489007333-3489264175,1858-2534,18547-26982,16-29,247-366,55547-103861,57-74,30-56,1670594-1765773,76-129,134085905-134182567,441436-566415,7539123416-7539252430,668-1146,581563513-581619699"; xsplitedInput = StringSplit[xinput,","]; newInputList = Map[ Function[x, Apply[Range , Map[ ToExpression, StringSplit[x,"-"]]] ] , xsplitedInput]; Total @ Flatten @ Map[ Function[xnumberList, Select[xnumberList, Function[x, StringMatchQ[ ToString[x] , RegularExpression[ "(\\d+)\\1"] ]] ]] , newInputList ] (* 55916882972 *)
summary, we solved the problem by using regular expression. we can also use Wolfram language's string expression (which is basically alternative syntax for regular expression.)
or, another way to solve it, is by taking the digits apart.
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)