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.

day 1

input


parse


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

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

input


parse


helper functions


part 1

In[]:=
Length @ Select[data, safeQ]
Out[]=
334

part 2

In[]:=
Length @ Select[data, dampSafeQ]
Out[]=
400

day 3

input


helper functions


part 1

In[]:=
Total[Times @@@ mulArgs[aocInput[3]]]
Out[]=
188741603

part 2

In[]:=
Total[Times @@@ mulArgs[filterDisabled[aocInput[3]]]]
Out[]=
67269798

day 4

input


parse


helper functions


part 1

In[]:=
Total[Length /@ StringCases[allLines[data], "XMAS"]]
Out[]=
2297

part 2

In[]:=
Count[Flatten[Partition[mat, {3,3}, 1], 1], $xmasPattern]
Out[]=
1745

day 5

input


parse


helper functions


part 1

In[]:=
Total[middleElement /@ Cases[tests, $orderedPattern]]
Out[]=
5391

part 2

In[]:=
Total[middleElement /@ (DeleteCases[tests, $orderedPattern] //. $reorderings)]
Out[]=
6142

day 6

input


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

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:
In[]:=
step[] := If[path[[x,y]]  dir,​​ Throw[1], ​​ Which[​​ dir  "^" && x > 1,​​ If[mat[[x-1,y]]  "#", dir = ">", path[[x,y]] = dir; x = x - 1]​​ ,​​ dir  "v" && x < m,​​ If[mat[[x+1,y]]  "#", dir = "<", path[[x,y]] = dir; x = x + 1]​​ ,​​ dir  "<" && y > 1,​​ If[mat[[x,y-1]]  "#", dir = "^", path[[x,y]] = dir; y = y - 1]​​ ,​​ dir  ">" && y < n,​​ If[mat[[x,y+1]]  "#", dir = "v", path[[x,y]] = dir; y = y + 1]​​ ,​​ True,​​ path[[x,y]] = dir; Throw[0]​​ ]​​]
Iterate for each obstacle position:
In[]:=
Total @ Flatten @ Table[​​ init[];​​ If[mat[[i,j]]  "#" || {i,j}  {xi,yi},​​ 0,​​ mat[[i,j]] = "#";​​ Catch[While[True, step[]]]​​ ],​​ {i,m}, {j,n}​​]
Out[]=
1719

day 7


day 8


day 9


day 10


day 11


day 12


day 13


day 14


day 15


day 16


day 17


day 18


day 19


day 20


day 21


day 22


day 23


day 24


day 25


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