summary refs log tree commit diff stats
path: root/day22.jl
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-02-21 00:11:06 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-02-21 00:11:06 -0800
commit0a4fe70d367f0bf1a78602af600052f58a377c34 (patch)
tree69c2bcd2b54b48968fd52728aaf0e21ca154784c /day22.jl
parent1e2642d8793e6a4fb6cba16cd651d5fdca3e4581 (diff)
downloadAdventOfCode2017-main.tar.gz
solutions to day 25 main
Diffstat (limited to 'day22.jl')
-rw-r--r--day22.jl25
1 files changed, 25 insertions, 0 deletions
diff --git a/day22.jl b/day22.jl
new file mode 100644
index 0000000..6e13236
--- /dev/null
+++ b/day22.jl
@@ -0,0 +1,25 @@
+grid = Int.(hcat(collect.(readlines("day22.txt"))...).=='#')'
+testgrid = [0 0 1; 1 0 0; 0 0 0]
+
+rule1 = reshape([0 -1 1 0 0 1 -1 0], 2, 2, :)
+rule2 = reshape([0 -1 1 0 1 0 0 1 0 1 -1 0 -1 0 0 -1], 2, 2, :)
+
+function virus(grid, steps, dirs)
+    (i, j), n = div.(size(grid).+1, 2), size(dirs, 3)
+    di, dj, inf = -1, 0, 0
+    g = Dict((i,j)=>grid[i,j]*div(n,2) for i=1:size(grid,1), j=1:size(grid,2))
+    for _=1:steps
+        x = get(g, (i,j), 0)
+        g[i,j] = mod(x+1, n)
+        (di, dj) = [di dj] * dirs[:,:,x+1]
+        if x+1==n/2; inf+=1 end
+        i+=di; j+=dj
+    end
+    inf
+end
+
+part1 = virus(grid, 10^4, rule1)
+part2 = virus(grid, 10^7, rule2)
+
+println(part1)
+println(part2)