The Advent of Code challenge just finished for 2020. I did the first one five years ago or so. I was in the mood again this year. I’ve completed 46 of the 49 puzzles (2 per day). I think they’ve increased the difficulty some from what I remember, or my skills are deteriorating, haha. I’ve attached my solutions so far in the notebook.
The people who make the leaderboard are unbelievably fast at solving the puzzles, but one of the leaders posts his Python solutions after scoring is locked for the day, and I think several of my Wolfram solutions look structurally a bit more concise and clean in comparison. Although again, it’s hard not to be impressed by how quickly the top 100 people solve the puzzles. They’ve often solved both parts of the day’s puzzle before I finish reading the first part, haha.
Some highlights for me were using the built-in Chinese remainder theorem function for puzzle 13.2:
The people who make the leaderboard are unbelievably fast at solving the puzzles, but one of the leaders posts his Python solutions after scoring is locked for the day, and I think several of my Wolfram solutions look structurally a bit more concise and clean in comparison. Although again, it’s hard not to be impressed by how quickly the top 100 people solve the puzzles. They’ve often solved both parts of the day’s puzzle before I finish reading the first part, haha.
Some highlights for me were using the built-in Chinese remainder theorem function for puzzle 13.2:
ChineseRemainder@@Transpose@MapIndexed[If[NumberQ@#,{-#2[[1]]+1,#},Nothing]&,Import["input.txt","CSV"][[2]]]
And solving 17.2 with the built in cellular automaton function with outer totalistic code 224 in 4 dimensions:
Count[CellularAutomaton[<|"OuterTotalisticCode"->224,"Dimension"->4|>,{{{Characters@Import["input.txt","List"]/.{"#"->1,"."->0}}},0},{{{6}}}],1,∞]
They actually had three days focused on cellular automata this year, which was pretty fun.
Anyway, I can’t guarantee I’m going to get around to solving the last three puzzles, so I figured I’d go ahead and post what I had. Merry Christmas and happy holidays!
Anyway, I can’t guarantee I’m going to get around to solving the last three puzzles, so I figured I’d go ahead and post what I had. Merry Christmas and happy holidays!
1.1
1.1
In[]:=
Times@@SelectFirst[Subsets[Import["input.txt","List"],{2}],Total@#2020&]
Out[]=
1016964
1.2
1.2
In[]:=
Times@@SelectFirst[Subsets[Import["input.txt","List"],{3}],Total@#2020&]
Out[]=
182588480
2.1
2.1
In[]:=
Count[StringCases[Import["input.txt","List"],min:NumberString~~"-"~~max:NumberString~~" "~~letter:LetterCharacter~~": "~~password:LetterCharacter..ToExpression@min≤StringCount[password,letter]≤ToExpression@max],{True}]
Out[]=
625
2.2
2.2
In[]:=
Count[StringCases[Import["input.txt","List"],pos1:NumberString~~"-"~~pos2:NumberString~~" "~~letter:LetterCharacter~~": "~~password:LetterCharacter..Count[StringTake[password,{{ToExpression@pos1},{ToExpression@pos2}}],letter]1],{True}]
Out[]=
391
3.1
3.1
In[]:=
Count[MapIndexed[StringTake[#,{Mod[1+3*(First@#2-1),StringLength@#,1]}]&,Import["input.txt","List"]],"#"]
Out[]=
244
3.2
3.2
In[]:=
Times@@Function[{right,down},Count[MapIndexed[StringTake[#,{Mod[1+right*(First@#2-1),StringLength@#,1]}]&,Import["input.txt","List"][[;;;;down]]],"#"]]@@@{{1,1},{3,1},{5,1},{7,1},{1,2}}
Out[]=
9406609920
4.1
4.1
In[]:=
Count[StringCases[StringSplit[Import["input.txt"],"\n\n"],a:LetterCharacter..~~":"a],_?(ContainsAll@{"byr","iyr","eyr","hgt","hcl","ecl","pid"})]
Out[]=
204
4.2
4.2
In[]:=
Count[StringSplit[Import["input.txt"],"\n\n"],_?(AllTrue[{"byr:"~~(n:Repeated[DigitCharacter,{4}]/;1920≤ToExpression@n≤2002),"iyr:"~~(n:Repeated[DigitCharacter,{4}]/;2010≤ToExpression@n≤2020),"eyr:"~~(n:Repeated[DigitCharacter,{4}]/;2020≤ToExpression@n≤2030),"hgt:"~~(((n1:NumberString/;150≤ToExpression@n1≤193)~~"cm")|((n2:NumberString/;59≤ToExpression@n2≤76)~~"in")),"hcl:#"~~Repeated[DigitCharacter|CharacterRange["a","f"],{6}],"ecl:"~~"amb"|"blu"|"brn"|"gry"|"grn"|"hzl"|"oth","pid:"~~Repeated[DigitCharacter,{9}]},Function[patt,StringContainsQ[#,patt~~(WhitespaceCharacter|EndOfString)]]]&)]
Out[]=
179
5.1
5.1
In[]:=
Max@Map[8*FromDigits[Characters@StringTake[#,7]/.{"F"0,"B"1},2]+FromDigits[Characters@StringTake[#,-3]/.{"L"0,"R"1},2]&,Import["input.txt","List"]]
Out[]=
928
5.2
5.2
In[]:=
Map[8*FromDigits[Characters@StringTake[#,7]/.{"F"0,"B"1},2]+FromDigits[Characters@StringTake[#,-3]/.{"L"0,"R"1},2]&,Import["input.txt","List"]]//Complement[Range@@MinMax@#,#]&
Out[]=
{610}
6.1
6.1
In[]:=
Total[CountDistinct/@Characters@StringDelete[StringSplit[Import["input.txt"],"\n\n"],"\n"]]
Out[]=
6878
6.2
6.2
In[]:=
Total[Length/@Intersection@@@Characters@StringSplit[StringSplit[Import["input.txt"],"\n\n"],"\n"]]
Out[]=
3464
7.1
7.1
In[]:=
With[{data=Flatten@StringCases[Import["input.txt","List"],a__~~" bags contain "~~b__aStringCases[b,Shortest[NumberString~~" "~~c__~~" bag"]c]]},Length@Select[data,Length@NestWhile[Flatten[#/.data]&,{First@#},Length@#>0&&FreeQ[#,"shiny gold"]&]>0&]-1]
Out[]=
103
7.2
7.2
In[]:=
With[{data=Flatten@StringCases[Import["input.txt","List"],a__~~" bags contain "~~b__aFlatten@StringCases[b,Shortest[n:NumberString~~" "~~c__~~" bag"]Table[c,ToExpression@n]]]},Length@Flatten@NestWhileList[Flatten[#/.data]&,{"shiny gold"},Length@#>0&]-1]
Out[]=
1469
8.1
8.1
In[]:=
With[{data=MapAt[ToExpression,StringSplit[Import["input.txt","List"]],{All,2}]},NestWhile[Switch[data[[#i,1]],"acc",<|"i"#i+1,"acc"#acc+data[[#i,2]],"hist"Append[#hist,#i]|>,"jmp",<|"i"#i+data[[#i,2]],"acc"#acc,"hist"Append[#hist,#i]|>,"nop",<|"i"#i+1,"acc"#acc,"hist"Append[#hist,#i]|>]&,<|"i"1,"acc"0,"hist"{}|>,FreeQ[#hist,#i]&]]["acc"]
Out[]=
1217
8.2
8.2
In[]:=
With[{data=MapAt[ToExpression,StringSplit[Import["input.txt","List"]],{All,2}]},SelectFirst[Table[With[{dataMod=MapAt[#/.{"nop""jmp","jmp""nop"}&,data,{mod,1}]},NestWhile[Switch[dataMod[[#i,1]],"acc",<|"i"#i+1,"acc"#acc+dataMod[[#i,2]],"hist"Append[#hist,#i]|>,"jmp",<|"i"#i+dataMod[[#i,2]],"acc"#acc,"hist"Append[#hist,#i]|>,"nop",<|"i"#i+1,"acc"#acc,"hist"Append[#hist,#i]|>]&,<|"i"1,"acc"0,"hist"{}|>,FreeQ[#hist,#i]&&#i≤Length@dataMod&]],{mod,Length@data}],#iLength@data+1&]]["acc"]
Out[]=
501
9.1
9.1
In[]:=
Last@SelectFirst[Partition[Import["input.txt","List"],26,1],FreeQ[Total/@Subsets[Most@#,{2}],Last@#]&]
Out[]=
50047984
9.2
9.2
In[]:=
Total@MinMax@First@SequenceCases[Import["input.txt","List"],_?(Length@#>1&&Total@#50047984&)]
Out[]=
5407707
10.1
10.1
In[]:=
Counts[Append[Differences@Prepend[Sort@Import["input.txt","List"],0],3]]//#[1]*#[3]&
Out[]=
2775
10.2
10.2
In[]:=
Module[{data,count},data=Prepend[Sort@Import["input.txt","List"],0];count[i_]:=count[i]=If[iLength@data,1,Total[If[Length@data≥#&&data[[#]]-data[[i]]≤3,count[#],0]&/@(i+{1,2,3})]];count[1]]
Out[]=
518344341716992
11.1
11.1
In[]:=
Count[FixedPoint[ArrayFilter[Which[#[[2,2]]"L"&&FreeQ[#,"#",{2}],"#",#[[2,2]]"#"&&Count[#,"#",{2}]≥5,"L",True,#[[2,2]]]&,#,{1,1},Padding"."]&,Characters@Import["input.txt","List"]],"#",{2}]
Out[]=
2483
11.2
11.2
In[]:=
Count[FixedPoint[Function[data,ArrayPad[MapIndexed[With[{vis=Function[dir,NestWhile[Function[pos,With[{val=Extract[data,pos+dir]},Switch[val,"L"|"#",val,"-",".",".",pos+dir]]],#2+{1,1},ListQ]]/@DeleteCases[Tuples[{1,0,-1},2],{0,0}]},Which[#"L"&&FreeQ[vis,"#"],"#",#"#"&&Count[vis,"#"]≥5,"L",True,#]]&,data[[2;;-2,2;;-2]],{2}],1,"-"]],ArrayPad[Characters@Import["input.txt","List"],1,"-"]],"#",{2}]
Out[]=
2285
12.1
12.1
In[]:=
Total@Abs@Most@Fold[With[{n=ToExpression@StringDrop[#2,1]},Switch[StringTake[#2,1],"N",#+{0,n,0},"S",#+{0,-n,0},"E",#+{n,0,0},"W",#+{-n,0,0},"L",#+{0,0,n},"R",#+{0,0,-n},"F",#+Append[n*AngleVector[#[[3]]Degree],0]]]&,{0,0,0},Import["input.txt","List"]]
12.2
12.2
13.1
13.1
13.2
13.2
14.1
14.1
14.2
14.2
15.1
15.1
15.2
15.2
16.1
16.1
16.2
16.2
17.1
17.1
17.2
17.2
18.1
18.1
18.2
18.2
19.1
19.1
19.2
19.2
20.1
20.1
21.1
21.1
21.2
21.2
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Advent of code 2020 solutions in Wolfram language
by Michael Hale
Wolfram Community, STAFF PICKS, December 26, 2020
https://community.wolfram.com/groups/-/m/t/2147697
by Michael Hale
Wolfram Community, STAFF PICKS, December 26, 2020
https://community.wolfram.com/groups/-/m/t/2147697