In December 2024 I had a lot of fun completing Advent of Code for the first time, using Wolfram Language exclusively. In case it’s of interest to any fellow solvers, the below notebook contains my solutions together with the inputs (which are user-specific).
The puzzle questions can be found on the Advent of Code 2024 page.
I found days 14, 21 and 24 to be the most challenging.
The puzzle questions can be found on the Advent of Code 2024 page.
I found days 14, 21 and 24 to be the most challenging.
day 1
day 1
input
input
parse
parse
part 1
part 1
Sort the columns separately then evaluate the sum of pairwise absolute differences:
In[]:=
Total @ Flatten @ Abs @ Differences[Sort /@ {colA, colB}]
Out[]=
1223326
part 2
part 2
Sum of each value in left column multiplied by how many times it appears in right column:
In[]:=
Total @ Table[x Count[colB, x], {x, colA}]
Out[]=
21070419
day 2
day 2
input
input
parse
parse
helper functions
helper functions
part 1
part 1
In[]:=
Length @ Select[data, safeQ]
Out[]=
334
part 2
part 2
In[]:=
Length @ Select[data, dampSafeQ]
Out[]=
400
day 3
day 3
input
input
helper functions
helper functions
part 1
part 1
In[]:=
Total[Times @@@ mulArgs[aocInput[3]]]
Out[]=
188741603
part 2
part 2
In[]:=
Total[Times @@@ mulArgs[filterDisabled[aocInput[3]]]]
Out[]=
67269798
day 4
day 4
input
input
parse
parse
helper functions
helper functions
part 1
part 1
In[]:=
Total[Length /@ StringCases[allLines[data], "XMAS"]]
Out[]=
2297
part 2
part 2
In[]:=
Count[Flatten[Partition[mat, {3,3}, 1], 1], $xmasPattern]
Out[]=
1745
day 5
day 5
input
input
parse
parse
helper functions
helper functions
part 1
part 1
In[]:=
Total[middleElement /@ Cases[tests, $orderedPattern]]
Out[]=
5391
part 2
part 2
In[]:=
Total[middleElement /@ (DeleteCases[tests, $orderedPattern] //. $reorderings)]
Out[]=
6142
day 6
day 6
input
input
part 1
part 1
Initializations:
mat = Characters /@ StringSplit[aocInput[6], "\n"];{m, n} = Dimensions[mat];{x, y} = FirstPosition[mat, "^"|"v"|"<"|">"];dir = mat[[x, y]];mat[[x,y]] = ".";path = mat;path[[x, y]] = "X";
Function to take a step:
In[]:=
step[] := Which[ dir "^" && x > 1, If[mat[[x-1,y]] "#", dir = ">", x = x - 1; path[[x,y]] = "X"] , dir "v" && x < m, If[mat[[x+1,y]] "#", dir = "<", x = x + 1; path[[x,y]] = "X"] , dir "<" && y > 1, If[mat[[x,y-1]] "#", dir = "^", y = y - 1; path[[x,y]] = "X"] , dir ">" && y < n, If[mat[[x,y+1]] "#", dir = "v", y = y + 1; path[[x,y]] = "X"] , True, dir = ""]
Iterate until done:
In[]:=
While[dir≠"", step[]]
Result:
In[]:=
StringCount[StringRiffle[StringJoin /@ path, "\n"], "X"]
Out[]=
4752
part 2
part 2
Initialization:
In[]:=
init[] := mat = Characters /@ StringSplit[aocInput[6], "\n"]; {m, n} = Dimensions[mat]; {x, y} = FirstPosition[mat, "^"|"v"|"<"|">"]; dir = mat[[x, y]]; mat[[x,y]] = "."; path = mat; {xi,yi} = {x,y};
Step:
Iterate for each obstacle position:
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Advent of code 2024: Wolfram Language solutions by Peter Falloon
by Peter Falloon
Wolfram Community, STAFF PICKS, January 8, 2025
https://community.wolfram.com/groups/-/m/t/3353033
by Peter Falloon
Wolfram Community, STAFF PICKS, January 8, 2025
https://community.wolfram.com/groups/-/m/t/3353033