# Leif's Website

## Advent of Code 2022 Day 16 — Mathematica

The input is small enough that brute-forcing is doable, but a best-first search would likely be faster.

input = Import@"input.txt"

lines = ToExpression@
StringCases[StringSplit[input, "\n"],
NumberString | (CharacterRange["A", "Z"] ~~
CharacterRange["A", "Z"])];
flow = Select[AssociationThread @@ Transpose@lines[[;; , 1 ;; 2]],
Positive];
d = With[{g = Graph@Flatten[Thread[#[[1]] -> #[[3 ;;]]] & /@ lines]},
VertexList@
g -> (AssociationThread[VertexList@g -> #] & /@
GraphDistanceMatrix@g)]];
f[closed_, from_, minutes_] :=
f[closed, from, minutes] =
Max[0, With[{remaining = minutes - d[from, closed[[#]]] - 1},
If[remaining > 0,
flow[closed[[#]]]*remaining +
f[Delete[closed, #], closed[[#]], remaining], Nothing]] & /@
Range@Length@closed]
f[{}, _, _] = 0;

f[Keys@flow, AA, 30]
Max[f[#, AA, 26] + f[Complement[Keys@flow, #], AA, 26] & /@
Subsets@Keys@flow]


## Advent of Code 2022 Day 11 — Mathematica

Functions in Mathematica are symbolic expressions you can examine and modify just like any other symbolic expressions, which allows for some fun:

i=Import@"input.txt";


## A geometric proof of $\sin 2\theta = 2 \sin \theta \cos \theta$

To start with, let's draw the standard trigonometric unit circle diagram for the angles $\theta$ and $2\theta$:

## Advent of Code 2020 Day 1 — Mathematica

The first task of day 1 was to find a pair of numbers in a given list that sum to 2020:

Intersection[input, 2020 - input]