Skip to content

Instantly share code, notes, and snippets.

@mayoff
Created December 23, 2022 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayoff/f6a22f2c88d96440225063be4f2feef1 to your computer and use it in GitHub Desktop.
Save mayoff/f6a22f2c88d96440225063be4f2feef1 to your computer and use it in GitHub Desktop.
AOC 2022 day 23
// https://adventofcode.com/2022/day/23
import AOC
import Preamble
let input = readInput()
let xinput = """
....#..
..###.#
#...#.#
.#...##
#.###..
##.#.##
.#..#..
"""
var elves = Grid(asciiArt: input).reduce(into: Set<Point>()) {
if $2 == "#" { $0.insert($1) }
}
var dirses: [[Vector]] = [
[.NE, .N, .NW],
[.SE, .S, .SW],
[.NW, .W, .SW],
[.NE, .E, .SE],
]
func step() -> Bool {
var props: [Point: [Point]] = [:]
for e in elves {
guard Vector.all8.contains(where: { elves.contains(e + $0) }) else { continue }
for dirs in dirses {
if dirs.allSatisfy({ !elves.contains(e + $0) }) {
let t = e + dirs[1]
props[t, default: []].append(e)
break
}
}
}
var moved = false
for (t, fs) in props where fs.count == 1 {
elves.remove(fs[0])
elves.insert(t)
moved = true
}
let d = dirses.removeFirst()
dirses.append(d)
return moved
}
var r = 1
while true {
guard step() else { break }
if r == 10 {
let box = elves.boundingBox()!
let p1 = box.sequence.lazy.filter { !elves.contains($0) }.makeArray().count
p1 |> printAndCopy
// 2605 WRONG didn't break after adding a proposal; didn't check elf has a neighbor before allowing a proposal
// 4000
}
r += 1
}
r |> printAndCopy
// 1039 WRONG off-by-one
// 1040
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment