Basic Functions
Basic Functions
In[]:=
ExportScreenImage[dir_:$WorkDirectory<>"Wolfram/UltraRapidCoding/Blog/aoc/screenshots/",fn_:UnixTime,fmt_:".png"]:=Export[FileNameJoin[{dir,ToString@fn[]<>fmt}],CurrentScreenImage[]]
In[]:=
SessionSubmit[ScheduledTask[ExportScreenImage[],Quantity[15,"Seconds"]]];
In[]:=
TaskRemove/@Tasks[]
Out[]=
TaskObject
Advent of Code in Wolfram Language
Advent of Code in Wolfram Language
Question 1
Question 1
In[]:=
SystemOpen["https://adventofcode.com"]
In[]:=
"17219793662996751456"//StringSplit//ToExpression
Out[]=
{1721,979,366,299,675,1456}
In[]:=
ClearAll@advent1;advent1[vals_,n_:2]:=Pick[vals,KnapsackSolve[{#,1,1}&/@vals,{2020,2020,n}],1]
In[]:=
Times@@{3,4,2}
Out[]=
24
In[]:=
advent1@{1721,979,366,299,675,1456}
Out[]=
514579
In[]:=
advent1[{1721,979,366,299,675,1456},3]
Out[]=
241861950
In[]:=
session=StartWebSession[]
In[]:=
GetAOCInput[url_]:=StringReplace[Last@WebExecute[session,{"OpenPage"url,"PageSource"}],"<"~~Except[">"]..~~">"""]
In[]:=
WebExecute[session,"OpenPage""https://adventofcode.com/2020/day/1/input"];
In[]:=
input=StringCases[WebExecute[session,"PageSource"],d:DigitCharacter..ToExpression@d];
In[]:=
advent1@input
Out[]=
1014624
In[]:=
advent1[input,3]
Out[]=
{1084,936}
In[]:=
DeleteObject@session
In[]:=
Length@input
Out[]=
200
In[]:=
Length@Subsets[input,{3}]
Out[]=
1313400
In[]:=
Select[Subsets[input,{3}],Total@#2020&]
Out[]=
{{704,1223,93}}
In[]:=
Times@@First@%
Out[]=
80072256
Question 2
Question 2
In[]:=
input2a=StringSplit["1-3 a: abcde1-3 b: cdefg2-9 c: ccccccccc","\n"]
Out[]=
{1-3 a: abcde,1-3 b: cdefg,2-9 c: ccccccccc}
In[]:=
advent2[in_]:=Length@Select[in,With[{s=StringSplit[#," "]},With[{minmax=ToExpression/@StringSplit[First@s,"-"]},First@minmax≤Count[Characters@Last@s,StringTake[s〚2〛,1]]≤Last@minmax]]&]
In[]:=
advent2@input2a
Out[]=
2
In[]:=
advent2@input2b
Out[]=
396
In[]:=
input2b=StringSplit[StringTrim@GetAOCInput["https://adventofcode.com/2020/day/2/input"],"\n"];
In[]:=
Characters["abcdeaaa"]
Out[]=
{a,b,c,d,e,a,a,a}
In[]:=
advent2b[in_]:=Length@Select[in,With[{s=StringSplit[#," "]},With[{yesno=ToExpression/@StringSplit[First@s,"-"]},Count[Characters[Last@s]〚yesno〛,StringTake[s〚2〛,1]]1]]&]
In[]:=
advent2b[input2a]
Out[]=
1
In[]:=
advent2b[input2b]
Out[]=
428
Question 3
Question 3
In[]:=
input3a=Characters/@StringSplit@"..##.......#...#...#...#....#..#...#.#...#.#.#...##..#...#.##......#.#.#....#.#........##.##...#...#...##....#.#..#...#.#"
Out[]=
{{.,.,#,#,.,.,.,.,.,.,.},{#,.,.,.,#,.,.,.,#,.,.},{.,#,.,.,.,.,#,.,.,#,.},{.,.,#,.,#,.,.,.,#,.,#},{.,#,.,.,.,#,#,.,.,#,.},{.,.,#,.,#,#,.,.,.,.,.},{.,#,.,#,.,#,.,.,.,.,#},{.,#,.,.,.,.,.,.,.,.,#},{#,.,#,#,.,.,.,#,.,.,.},{#,.,.,.,#,#,.,.,.,.,#},{.,#,.,.,#,.,.,.,#,.,#}}
In[]:=
NestList[1+Mod[#-1+3,Length@First@input3a]&,1,Length@input3a]
Out[]=
{1,4,7,10,2,5,8,11,3,6,9,1}
In[]:=
Length@%
Out[]=
12
In[]:=
advent3a[in_]:=Count[Extract[in,MapIndexed[{First@#2,#}&,Most@NestList[1+Mod[#-1+3,Length@First@in]&,1,Length@in]]],"#"]
In[]:=
advent3a@input3a
Out[]=
7
In[]:=
input3b=Characters/@StringSplit@GetAOCInput["https://adventofcode.com/2020/day/3/input"];
In[]:=
Length@input3b
Out[]=
323
In[]:=
advent3a[input3b]
Out[]=
211
In[]:=
advent3a[in_,right_:3]:=Count[Extract[in,MapIndexed[{First@#2,#}&,Most@NestList[1+Mod[#-1+right,Length@First@in]&,1,Length@in]]],"#"]
In[]:=
advent3a[input3a,#]&/@{1,3,5,7}
Out[]=
{2,7,3,4}
In[]:=
advent3a[input3a〚;;;;2〛,1]
Out[]=
2
In[]:=
advent3b[in_]:=Times@@Append[advent3a[in,#]&/@{1,3,5,7},advent3a[in〚;;;;2〛,1]]
In[]:=
advent3b[input3a]
Out[]=
336
In[]:=
advent3b[input3b]
Question 4
Question 4
Question 5
Question 5
Question 6
Question 6