In[]:=
SetDirectory["C:\\Users\\Michael\\Downloads"];

Day 1

In[]:=
i=Total/@SequenceSplit[Import["input.txt","List"],{""}];
In[]:=
Max@i
Out[]=
66487
In[]:=
Total@TakeLargest[i,3]
Out[]=
197301

Day 2

In[]:=
score[a_,b_]:=b+3(Mod[b-a,3,-1]+1)
In[]:=
i=Import["input.txt","Table"]/.{"A"->1,"B"->2,"C"->3};
In[]:=
Total[score@@@(i/.{"X"->1,"Y"->2,"Z"->3})]
Out[]=
14375
In[]:=
Total[score[#,Mod[#+#2,3,1]]&@@@(i/.{"X"->-1,"Y"->0,"Z"->1})]
Out[]=
10274

Day 3

In[]:=
pri=If[UpperCaseQ@#,26,0]+LetterNumber@#&;
In[]:=
i=Characters@Import["input.txt","Lines"];
In[]:=
Total[pri@@@Map[Intersection@@Partition[#,Length@#/2]&,i]]
Out[]=
7597
In[]:=
Total[pri@@@Intersection@@@Partition[i,3]]
Out[]=
2607

Day 4

In[]:=
i=Interval/@{{#,#2},{#3,#4}}&@@@ToExpression@StringSplit[Import["input.txt","List"],","|"-"];
In[]:=
Count[IntervalMemberQ[#,#2]||IntervalMemberQ[#2,#]&@@@i,True]
Out[]=
483
In[]:=
Count[IntervalIntersection@@@i,Interval[_]]
Out[]=
874

Day 5

In[]:=
{cInp,mInp}=SequenceSplit[Import["input.txt","Lines"],{""}];
In[]:=
m=ToExpression@StringSplit[mInp][[All,{2,4,6}]];
In[]:=
f[op_]:=(​​c=Select@LetterQ/@Transpose[Characters@cInp][[2;;;;4]];​​Do[​​{t1,t2}=TakeDrop[c[[mi[[2]]]],mi[[1]]];​​c[[mi[[2]]]]=t2;​​c[[mi[[3]]]]=Join[op@t1,c[[mi[[3]]]]]​​,{mi,m}];​​StringJoin@c[[All,1]]​​)
In[]:=
f@Reverse
Out[]=
TWSGQHNHL
In[]:=
f@Identity
Out[]=
JNRSCDWPP

Day 6

In[]:=
i=Characters@Import["input.txt"];
In[]:=
f=LengthWhile[Partition[i,#,1],Not@*DuplicateFreeQ]+#&;
In[]:=
f@4
Out[]=
1210
In[]:=
f@14
Out[]=
3476

Day 7

sizes=<||>;cur={""};​​StringCases[Import["input.txt","List"],{​​"$ cd ..":>(cur=Most@cur),​​"$ cd "~~d__:>AppendTo[cur,Last@cur<>"/"<>d],​​s:DigitCharacter..~~__:>Do[​​AssociateTo[sizes,f->Lookup[sizes,f,0]+ToExpression@s],​​{f,cur}]​​}];
In[]:=
sizes//Select[#<=100000&]//Total
Out[]=
1297159
In[]:=
SelectFirst[Sort@sizes,#>=30000000-(70000000-sizes[[1]])&]
Out[]=
3866390

Day 8

In[]:=
i=ToExpression@Characters@Import["input.txt","Lines"];
In[]:=
f[s_,e_]:=Flatten@Extract[i,#;;#2;;(Sign[#2-#]/.(0->1))&@@@({s,e})]//{LengthWhile[Rest@#,LessThan@#[[1]]],Length@#-1}&
In[]:=
p={{{x,1},y-1},{{x,99},99-y},{{1,y},x-1},{{99,y},99-x}};
In[]:=
Count[Table[f[{x,y},#][[1]]>=#2&@@@Or@@p,{x,99},{y,99}],True,{2}]
Out[]=
1560
In[]:=
Max[Times@@@Flatten[Table[Min[#+1,#2]&@@f[{x,y},#]&@@@p,{x,99},{y,99}],1]]
Out[]=
252000

Day 9

In[]:=
i=Flatten[Map[Table[#[[1]]/.{"U"->{0,1},"D"->{0,-1},"L"->{-1,0},"R"->{1,0}},#[[2]]]&,Import["input.txt","Table"]],1];
In[]:=
step[n_,l_]:=FoldList[If[Norm[#-#2[[1]]]>Sqrt@2,#2[[1]]+Sign[#-#2[[1]]],#2[[1]]]&,n,{Rest@l,Range[Length@l-1]}]
In[]:=
f=CountDistinct@FoldList[step[#[[1]]+#2,#]&,Table[{0,0},#],i][[All,-1]]&;
In[]:=
f@2
Out[]=
6266
In[]:=
f@10
Out[]=
2369

Day 10

In[]:=
i=FoldList[#+If[#2[[1]]=="addx",{2,#2[[2]]},{1,0}]&,{0,1},Import["input.txt","Table"]];
In[]:=
#*TakeWhile[i,s|->s[[1]]<#][[-1,-1]]&/@Range[20,220,40]//Total
Out[]=
14040
In[]:=
Partition[Table[If[Abs[Mod[c,40]-TakeWhile[i,s|->s[[1]]<=c][[-1,2]]]<=1,"#"," "],{c,0,239}],40]//StringRiffle
Out[]=
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

Day 11

In[]:=
i={​​{{99,67,92,61,83,64,98},#*17&,{3,4,2}},​​{{78,74,88,89,50},#*11&,{5,3,5}},​​{{98,91},#+4&,{2,6,4}},​​{{59,72,94,91,79,88,94,51},
2
#
&,{13,0,5}},​​{{95,72,78},#+7&,{11,7,6}},​​{{76},#+8&,{17,0,2}},​​{{69,60,53,89,71,88},#+5&,{19,7,1}},​​{{72,54,63,80},#+3&,{7,1,3}}​​};
In[]:=
turn[n_,d_]:=While[Length@i[[n,1]]>0,​​c[[n]]++;​​w=i[[n,1,1]];​​w2=Floor[i[[n,2]][w]/d];​​n2=If[Divisible[w2,i[[n,3,1]]],i[[n,3,2]],i[[n,3,3]]]+1;​​i[[n,1]]=Rest@i[[n,1]];​​i[[n2,1]]=Append[i[[n2,1]],Mod[w2,Times@@i[[All,3,1]]]];​​]
In[]:=
round[d_]:=turn[#,d]&/@Range@Length@i;
In[]:=
f[r_,d_]:=(​​c=Table[0,Length@i];​​Do[round[d],{ri,r}];​​Times@@TakeLargest[c,2]​​)
In[]:=
f[20,3]
Out[]=
120736
In[]:=
f[10000,1]
Out[]=
32059801242

Day 12

In[]:=
i0=Characters@Import["input.txt","Lines"];
In[]:=
i=Map[Switch[#,"S",1,"E",26,_,LetterNumber@#]&,i0,{2}];

Day 13