commit 8c90478f7cbf04c3583a5dda6fc4020b069765a4
parent 3cf14a15b169c945789620fac8d231682ef41680
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Sun, 8 Jan 2023 20:02:10 -0800
Solution to day 18, part 1.
Diffstat:
A | src/day_18.jl | | | 67 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 67 insertions(+), 0 deletions(-)
diff --git a/src/day_18.jl b/src/day_18.jl
@@ -0,0 +1,67 @@
+#!/usr/bin/env julia
+# https://adventofcode.com/2022/day/18
+using AdventOfCode
+
+# I've been avoiding list comprehensions because, idk, I'm a nerd who tries to
+# use `map`. But I remembered how expressive comprehensions can be, so it was
+# nice to use them here!
+
+example = readlines(IOBuffer("""
+ 2,2,2
+ 1,2,2
+ 3,2,2
+ 2,1,2
+ 2,3,2
+ 2,2,1
+ 2,2,3
+ 2,2,4
+ 2,2,6
+ 1,2,5
+ 3,2,5
+ 2,1,5
+ 2,3,5"""))
+input = readlines("data/day_18.txt")
+
+Cube = Tuple{Int, Int, Int}
+
+"""
+Read the puzzle input and return a map from cubes to exposed faces.
+
+We initialize each cube with six exposed faces and update them on a subsequent
+step.
+"""
+function parse_input(input::Vector{<:AbstractString})
+ Dict(Tuple(parse.(Int, split(x, ","))) => 6 for x in input)
+end
+
+function neighbors(cube::Cube, cubes::Set{Cube})
+ cubeneighbors = Set{Cube}()
+ for ax = 1:3, Δ = (-1, 1)
+ cube₂ = Tuple(i==ax ? cube[i]+Δ : cube[i] for i in eachindex(cube))
+ cube₂ ∈ cubes && push!(cubeneighbors, cube₂)
+ end
+ cubeneighbors
+end
+
+function updatecubes!(cubemap::Dict{Cube, Int})
+ cubes = Set(keys(cubemap))
+ while !isempty(cubes)
+ cube = pop!(cubes)
+ for neighbor in neighbors(cube, cubes)
+ cubemap[cube] -= 1
+ cubemap[neighbor] -= 1
+ end
+ end
+ cubemap
+end
+
+function part_1(input)
+ input |> parse_input |> updatecubes! |> values |> sum
+end
+@assert part_1(example) == 64
+@info part_1(input)
+
+function part_2(input)
+ nothing
+end
+@info part_2(input)