diff --git a/10.html b/10.html new file mode 100644 index 0000000..7f7befa --- /dev/null +++ b/10.html @@ -0,0 +1,18 @@ + + + + + + + + +
\ No newline at end of file diff --git a/10.jl b/10.jl new file mode 100644 index 0000000..4273cc5 --- /dev/null +++ b/10.jl @@ -0,0 +1,109 @@ +### A Pluto.jl notebook ### +# v0.20.3 + +using Markdown +using InteractiveUtils + +# ╔═╡ 207b57d2-bb33-11ef-0e5b-8d2ff4f89af8 +begin + const data=read("10.txt", String) +end + +# ╔═╡ af0c8dd7-266e-41d2-a348-309f96bb65fd +md"# Problem 1" + +# ╔═╡ f57806d4-92ef-4428-82de-76d2fca0ba9a +begin + function p1(data) + width = first(findfirst("\n",data)) - 1 + height = length(data) ÷ (width + 1) + # find character at column i row j + charat(i,j) = data[i + (j - 1) * (width + 1)] + matches(char,i,j)= ( + 1<=i<=width && + 1<=j<=height && + charat(i,j)==char + ) + startingpositions =[ + (i,j) + for (i,j)∈Iterators.product(1:width, 1:height) + if matches('0', i, j) + ] + num_trailheads = 0 + walk(pos) = begin + i,j=pos + level = parse(Int, charat(first(pos), last(pos))) + (level == 9) && return Set([pos]) + nextlevel = first(string(level + 1)) + gonorth = matches(nextlevel,i,j-1) + gosouth = matches(nextlevel,i,j+1) + gowest = matches(nextlevel,i-1,j) + goeast = matches(nextlevel,i+1,j) + res = Set{Tuple{Int,Int}}() + gonorth && (res = res ∪ walk((i,j-1))) + gosouth && (res = res ∪ walk((i,j+1))) + gowest && (res = res ∪ walk((i-1,j))) + goeast && (res = res ∪ walk((i+1,j))) + return res + end + for startingpos in startingpositions + for endingpos in walk(startingpos) + num_trailheads += 1 + end + end + num_trailheads + end + p1(data) +end + +# ╔═╡ b47940a3-c5ea-45e4-ab89-92788086d37f +md"# Problem 2" + +# ╔═╡ 86d6201d-3975-4b74-b4e7-6aa7c537f180 +begin + function p2(data) + width = first(findfirst("\n",data)) - 1 + height = length(data) ÷ (width + 1) + # find character at column i row j + charat(i,j) = data[i + (j - 1) * (width + 1)] + matches(char,i,j)= ( + 1<=i<=width && + 1<=j<=height && + charat(i,j)==char + ) + startingpositions =[ + (i,j) + for (i,j)∈Iterators.product(1:width, 1:height) + if matches('0', i, j) + ] + total_rating = 0 + walk(pos) = begin + i,j=pos + level = parse(Int, charat(first(pos), last(pos))) + (level == 9) && return 1 + nextlevel = first(string(level + 1)) + gonorth = matches(nextlevel,i,j-1) + gosouth = matches(nextlevel,i,j+1) + gowest = matches(nextlevel,i-1,j) + goeast = matches(nextlevel,i+1,j) + res = 0 + gonorth && (res = res + walk((i,j-1))) + gosouth && (res = res + walk((i,j+1))) + gowest && (res = res + walk((i-1,j))) + goeast && (res = res + walk((i+1,j))) + return res + end + for startingpos in startingpositions + total_rating += walk(startingpos) + end + total_rating + end + p2(data) +end + +# ╔═╡ Cell order: +# ╠═207b57d2-bb33-11ef-0e5b-8d2ff4f89af8 +# ╟─af0c8dd7-266e-41d2-a348-309f96bb65fd +# ╠═f57806d4-92ef-4428-82de-76d2fca0ba9a +# ╟─b47940a3-c5ea-45e4-ab89-92788086d37f +# ╠═86d6201d-3975-4b74-b4e7-6aa7c537f180 diff --git a/10.txt b/10.txt new file mode 100644 index 0000000..0b2e3b4 --- /dev/null +++ b/10.txt @@ -0,0 +1,45 @@ +101234653436698943210876543298108967890123211 +900945762567787651028945054167211876965254300 +812876851008632112987632167054340105874367891 +743987945219541003456542108981233234567809892 +657641434349898764565876232190543456452912763 +898530125896701651698965945087612387301103454 +101421056787672340789234876231201293219010989 +012336765410589121470125960140340184678321670 +987449870323431034561076054057653296501234521 +876558901210120345665780143968934187410345438 +034569821025001256676891232877065056309654589 +125678438136789867589872301986174145218789678 +678776569245234798434565432765289036785890541 +589989478910135652329806567874376123896781030 +431276321210023101210715432990145678765432121 +120345290923412302345620121087230109650187656 +098010187874503210989430101256521212341090347 +187623456365694567876543232344560301032181298 +012510589456787655469854741023478912345672107 +143421674307897846988767858910890109852109856 +231256014210978987671056967378765210763296705 +140347123900389034502340191269854307894585014 +051298034891276123216521780350123456765674323 +565434540762345637897435615443210523456001298 +876529651057850136988904306782107610987123367 +985018782348943245077211216790108901278994453 +123427890167654432106310345889299870122185432 +010568921211234501215445432976387761233076541 +129877432100235676301456701265456654394567650 +431066523231145985490389876014012534585658969 +302309012694046796781201078923453421673215678 +213218906783459873212212562112969010984504301 +304567815612163654304323453007878123435643210 +412359854309072345323456894314365036521754965 +563456743218781896210567765101254307810867874 +694105678907690787890698989210345218987980123 +787234563210581654321787876307896210898943321 +894563454671410010987345665456987346787652450 +763674589582323225676298767645459855898101067 +652983673497654134570156958912378764306678998 +341672102108987010987667843003569013215463210 +030501012896109823478766542103454320176354501 +125418923787210734569651033212169010083296101 +654327654654323601234569124349078321190187632 +789210125345434512103678765678767456783276543