Leif's Website

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]}, 
   AssociationThread[
    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]

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";
{items,inspect,throw}=Association/@Transpose[Thread[#[[1,1]]->{#[[2]],(x|->a[x,b])/.{a->#[[3,1]],b->If[Length@#[[3]]>1,#[[3,2]],x]},(x|->If[Divisible[x,a],b,c])/.{a->#[[4,1]],b->#[[5,1]],c->#[[6,1]]}}]&@ToExpression[StringSplit[#,Except[DigitCharacter|"*"|"+"]..]/.{"+"->Plus,"*"->Times}]&/@StringSplit[StringSplit[i,"\n\n"],"\n"]];
Times@@TakeLargest[Merge[Reap[Fold[Merge[{Append[#1,#2->{}],Sow[#2->Length@#1@#2];With[{items=Mod[inspect@#2/@#1@#2,LCM@@throw[[;;,2,1,2]]]},Thread[throw@#2/@items->items]]},Flatten]&,items,Flatten@Table[Keys@items,20]]][[2,1]],Total],2]

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

Read more...

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]
Read more...

This walks through my submission for the first project of the ENGGEN 131 course taught in 2019. Our task for this project was to implement eight functions described in the project outline, with marks being given for correctness, documentation, and performance. Surprisingly, the last of these requirements turned out the be the most difficult to satisfy.

Read more...

Enter your email to subscribe to updates.