diff --git a/08.html b/08.html new file mode 100644 index 0000000..d81f716 --- /dev/null +++ b/08.html @@ -0,0 +1,18 @@ + + + + + + + + +
\ No newline at end of file diff --git a/08.jl b/08.jl new file mode 100644 index 0000000..fb387c8 --- /dev/null +++ b/08.jl @@ -0,0 +1,137 @@ +### A Pluto.jl notebook ### +# v0.20.3 + +using Markdown +using InteractiveUtils + +# โ•”โ•โ•ก eda9d068-b581-11ef-360a-f162f294c9ff +begin + const data=read("08.txt", String) + using IterTools # subsets +end + +# โ•”โ•โ•ก 24de9e8e-a8c2-4371-bff9-8896b2fbf93c +md"# Problem 1 + +- Loop through all pairs $x,y$ of antennas with matching frequencies. +- Calculate displacement vector $v=x-y$. Check if $y+2v$ is in bounds. If so, it is an antinode. Also check $y+\frac{1}{3}v$ if $v$ is divisible by 3. +" + +# โ•”โ•โ•ก 86d872c6-dedd-4988-b5fb-ecc69f764f65 +begin + function p1(data) + width = first(findfirst("\n",data)) - 1 + height = length(data) รท (width + 1) + # util functions + charat(i,j) = data[i + (j - 1) * (width + 1)] + inbounds(i,j)=(1<=i<=width) && (1<=j<=height) + matches(char,i,j)= inbounds(i,j) && (charat(i,j)==char) + # get antenna locations + # represented as map of frequences to set of + # coordinate pairs. + antennas = Dict{Char,Set{Tuple{Int,Int}}}() + for (i,j) in product(1:width,1:height) + matches('.',i,j) && continue + freq = charat(i,j) + locs = get(antennas,freq, Set{Tuple{Int,Int}}()) + push!(locs, (i,j)) + antennas[freq] = locs + end + antinodes = Set{Tuple{Int,Int}}() + for (freq, locs) in pairs(antennas) + for (x,y) in subsets(collect(locs), Val{2}()) + v = (x[1]-y[1],x[2]-y[2]) + #y+2v + yp2v = (y[1]+2v[1],y[2]+2v[2]) + inbounds(yp2v...) && push!(antinodes,yp2v) + #x-2v + xm2v= (x[1]-2v[1],x[2]-2v[2]) + inbounds(xm2v...) && push!(antinodes,xm2v) + end + end + length(antinodes) + end + p1(data) +end + +# โ•”โ•โ•ก 36e2eadf-5175-41d4-9797-36cec3cd7522 +md"# Problem 2" + +# โ•”โ•โ•ก 75a47f06-33f6-468a-8be0-c5339e659b9a +begin + function p2(data) + width = first(findfirst("\n",data)) - 1 + height = length(data) รท (width + 1) + # util functions + charat(i,j) = data[i + (j - 1) * (width + 1)] + inbounds(i,j)=(1<=i<=width) && (1<=j<=height) + matches(char,i,j)= inbounds(i,j) && (charat(i,j)==char) + # get antenna locations + # represented as map of frequences to set of + # coordinate pairs. + antennas = Dict{Char,Set{Tuple{Int,Int}}}() + for (i,j) in product(1:width,1:height) + matches('.',i,j) && continue + freq = charat(i,j) + locs = get(antennas,freq, Set{Tuple{Int,Int}}()) + push!(locs, (i,j)) + antennas[freq] = locs + end + antinodes = Set{Tuple{Int,Int}}() + for (freq, locs) in pairs(antennas) + for (x,y) in subsets(collect(locs), Val{2}()) + v = (x[1]-y[1],x[2]-y[2]) + # forwards + n = 0 + while true + antinode = (y[1]+n*v[1],y[2]+n*v[2]) + !inbounds(antinode...) && break + push!(antinodes, antinode) + n +=1 + end + # backwards + n = -1 + while true + antinode = (y[1]+n*v[1],y[2]+n*v[2]) + !inbounds(antinode...) && break + push!(antinodes, antinode) + n -=1 + end + end + end + length(antinodes) + end + p2(data) +end + +# โ•”โ•โ•ก 00000000-0000-0000-0000-000000000001 +PLUTO_PROJECT_TOML_CONTENTS = """ +[deps] +IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e" + +[compat] +IterTools = "~1.10.0" +""" + +# โ•”โ•โ•ก 00000000-0000-0000-0000-000000000002 +PLUTO_MANIFEST_TOML_CONTENTS = """ +# This file is machine-generated - editing it directly is not advised + +julia_version = "1.11.1" +manifest_format = "2.0" +project_hash = "89e50390f9f4506f588655c92ba43dbfd7ea204f" + +[[deps.IterTools]] +git-tree-sha1 = "42d5f897009e7ff2cf88db414a389e5ed1bdd023" +uuid = "c8e1da08-722c-5040-9ed9-7db0dc04731e" +version = "1.10.0" +""" + +# โ•”โ•โ•ก Cell order: +# โ• โ•eda9d068-b581-11ef-360a-f162f294c9ff +# โ•Ÿโ”€24de9e8e-a8c2-4371-bff9-8896b2fbf93c +# โ• โ•86d872c6-dedd-4988-b5fb-ecc69f764f65 +# โ•Ÿโ”€36e2eadf-5175-41d4-9797-36cec3cd7522 +# โ• โ•75a47f06-33f6-468a-8be0-c5339e659b9a +# โ•Ÿโ”€00000000-0000-0000-0000-000000000001 +# โ•Ÿโ”€00000000-0000-0000-0000-000000000002 diff --git a/08.txt b/08.txt new file mode 100644 index 0000000..0a9bc9d --- /dev/null +++ b/08.txt @@ -0,0 +1,50 @@ +...............e.........................i........ +...............................1.......i.0........ +..............................s.0......d.......... +........................i....B.I............d..... +.............................s.................... +................J................................. +.....................L.....0i.......4...d......... +.N...e...........................s..R.....4.....I. +........e.........v................1......R....I.. +.............G..............0.....1............... +..2...N.............B......................4...R.. +..............2...................N..........4s... +..p...................................1..b..I..... +..................p...........B........b...R...... +....................................b............. +........W.......C.....w........................... +............7....u.............B.................. +...W.................u......................bw.... +.......p.2...........v......................9..... +.E.....C....u................................9.... +E....Y................u.D........9...........J.... +.......2.......................................... +............................J.................c... +.............7...K..D..............J.............. +.....C.Wq........t.................T.............. +............Yt......v............................. +..W......................3...............w........ +..7.....j................T...D.....n......8.....c. +.........E...............nTD...................... +...r....E..........Y............n.......P........c +......K........G......L........................... +......................G.....L....v................ +..............G...t......q.............l.8........ +......................q............l.............. +...6........r.............................w..c.... +..6.........3.......Qk........T................... +......Y...............j.................n......... +..K.....S.....r......j.....U......9.l......8...... +........................U......................P.. +.....................q............................ +.......K......5..N.....j.7.Q...................... +...................p..k...U..........L.Q.......... +.r......3...S.......k........y....8U....Q.......P. +.......S....g..3.................................. +.....S..........gk................................ +................5...................yP............ +.......................g......yV..l............... +.........6.5...............V...................... +..................6..5..V......................... +.............g.......................y..........V. diff --git a/index.html b/index.html index fc7edc0..bcbf56d 100644 --- a/index.html +++ b/index.html @@ -33,7 +33,7 @@

Advent of Code Solutions