Xah Talk Show 2024-05-07 Ep552 WolframLang Coding, Advent of Code 2023, Day 9
- Timestamp
- 0:56 read problem
- 4:25 Wolfram language intro
- 5:07 understand the problem
- 12:55 begin coding
- 30:46 FixedPoint function
- 49:00 solution
- 51:26 summary
- 56:22 shorten code
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
Differences[{10, 13, 16, 21, 30, 45}]
Differences[ {1,2} ]
Differences[ {1} ]
Differences[ {} ]
FixedPointList[
Function[{x}, Differences[ x ]],
{10, 13, 16, 21, 30, 45}
]
FixedPoint[ f , 3, 4]
FixedPointList[ f , 3, 4]
solution
input = "
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45";
input = ReadString[ "c:/Users/xah/web/xahlee_info/talk_show/i/advent_of_code_2023_day_9_input.txt" ];
input2 = ToExpression[ Map[ Function[{x}, StringSplit[ x, " " ]],
StringSplit[ input, "\n" ]] ];
Total @
Map[
Function[{xline},
Total @ Map[
Function[{yy}, If[yy === {}, 0, Last[yy]]] ,
FixedPointList[ Function[{x}, Differences[ x ]], xline ] ] ],
input2]
short solution
input = "
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45";
input2 = ToExpression@ (StringSplit /@ StringSplit[ input, "\n" ]) ;
Total @ (( Total @ (((If[# === {}, 0, Last[#]]) &) /@ FixedPointList[ Differences , # ]) & ) /@ input2)