Day 6: Guard Gallivant
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
I’m not proud of it.
I have a conjecture though, that any looping solution, obtained by adding one obstacle, would eventually lead to a rectangular loop. That may lead to a non brute-force solution. It’s quite hard to prove rigorously though. (Maybe proving, that the loop has to be convex, which is an equivalent statement here, is easier? You can also find matrix representations of the guard’s state changes, if that helps.)
Maybe some of the more mathematically inclined people here can try proving or disproving that.
Anyways, here is my current solution in Kotlin:
fun main() { fun part1(input: List<String>): Int { val puzzleMap = PuzzleMap.fromPuzzleInput(input) puzzleMap.simulateGuardPath() return puzzleMap.asIterable().indicesWhere { it is MapObject.Visited }.count() } fun part2(input: List<String>): Int { val puzzleMap = PuzzleMap.fromPuzzleInput(input) puzzleMap.simulateGuardPath() return puzzleMap.asIterable().indicesWhere { it is MapObject.Visited }.count { val alteredPuzzleMap = PuzzleMap.fromPuzzleInput(input) alteredPuzzleMap[VecNReal(it)] = MapObject.Obstacle() alteredPuzzleMap.simulateGuardPath() } } val testInput = readInput("Day06_test") check(part1(testInput) == 41) check(part2(testInput) == 6) val input = readInput("Day06") part1(input).println() part2(input).println() } enum class Orientation { NORTH, SOUTH, WEST, EAST; fun rotateClockwise(): Orientation { return when (this) { NORTH -> EAST EAST -> SOUTH SOUTH -> WEST WEST -> NORTH } } fun asVector(): VecNReal { return when (this) { NORTH -> VecNReal(listOf(0.0, 1.0)) SOUTH -> VecNReal(listOf(0.0, -1.0)) WEST -> VecNReal(listOf(-1.0, 0.0)) EAST -> VecNReal(listOf(1.0, 0.0)) } } } class PuzzleMap(objectElements: List<List<MapObject>>): Grid2D<MapObject>(objectElements) { private val guard = Grid2D(objectElements).asIterable().first { it is MapObject.Guard } as MapObject.Guard companion object { fun fromPuzzleInput(input: List<String>): PuzzleMap = PuzzleMap( input.reversed().mapIndexed { y, row -> row.mapIndexed { x, cell -> MapObject.fromCharAndIndex(cell, x to y) } } ).also { it.transpose() } } fun guardStep() { if (guardScout() is MapObject.Obstacle) guard.orientation = guard.orientation.rotateClockwise() else { guard.position += guard.orientation.asVector() } } fun simulateGuardPath(): Boolean { while (true) { markVisited() val scouted = guardScout() if (scouted is MapObject.Visited && guard.orientation in scouted.inOrientation) return true else if (scouted is MapObject.OutOfBounds) return false guardStep() } } fun guardScout(): MapObject = runCatching { this[guard.position + guard.orientation.asVector()] }.getOrElse { MapObject.OutOfBounds } fun markVisited() { val previousMapObject = this[guard.position] if (previousMapObject is MapObject.Visited) this[guard.position] = previousMapObject.copy(previousMapObject.inOrientation.plus(guard.orientation)) else this[guard.position] = MapObject.Visited(listOf(guard.orientation)) } } sealed class MapObject { class Empty: MapObject() class Obstacle: MapObject() object OutOfBounds: MapObject() data class Visited(val inOrientation: List<Orientation>): MapObject() data class Guard(var position: VecNReal, var orientation: Orientation = Orientation.NORTH): MapObject() companion object { fun fromCharAndIndex(c: Char, index: Pair<Int, Int>): MapObject { return when (c) { '.' -> Empty() '#' -> Obstacle() '^' -> Guard(VecNReal(index)) else -> throw IllegalArgumentException("Unknown map object $c") } } } }
I also have a repo.
Gleam
Late as usual. This one challenged me. Functional programming is a lot of fun, but it’s kicking my ass.
import gleam/dict import gleam/io import gleam/list import gleam/option.{None, Some} import gleam/result import gleam/set.{type Set} import gleam/string import simplifile pub type Point = #(Int, Int) pub type Grid(a) = dict.Dict(Point, a) pub type Direction { North East South West } pub type Loops { DoesLoop DoesNotLoop } pub type Guard { Guard(position: Point, direction: Direction) } fn get_guard(grid: Grid(String)) -> Guard { let pos = dict.filter(grid, fn(_pos, char) { char == "^" }) let assert Ok(pos) = case dict.size(pos) { 1 -> list.first(dict.keys(pos)) 0 -> panic as "No guard found in input!" _ -> panic as "More than one guard found in input!" } Guard(pos, North) } fn move_guard(guard: Guard) -> Guard { let new_pos = case guard.direction { North -> #(-1, 0) East -> #(0, 1) South -> #(1, 0) West -> #(0, -1) } Guard( #(guard.position.0 + new_pos.0, guard.position.1 + new_pos.1), guard.direction, ) } fn turn_guard(guard: Guard) -> Guard { let new_dir = case guard.direction { North -> East East -> South South -> West West -> North } Guard(guard.position, new_dir) } fn get_obstacles(grid: Grid(String)) -> List(Point) { dict.filter(grid, fn(_pos, char) { char == "#" }) |> dict.keys() } fn recurse_grid( grid: Grid(String), guard: Guard, obstacles: List(#(Int, Int)), visited: Set(#(#(Int, Int), Direction)), ) -> #(Set(#(#(Int, Int), Direction)), Loops) { let new_guard = move_guard(guard) let position = new_guard.position let dir = new_guard.direction case dict.has_key(grid, position) { False -> #(visited, DoesNotLoop) True -> { case set.contains(visited, #(position, dir)) { True -> { #(visited, DoesLoop) } False -> { case list.contains(obstacles, position) { True -> recurse_grid(grid, turn_guard(guard), obstacles, visited) False -> recurse_grid( grid, new_guard, obstacles, set.insert(visited, #(position, dir)), ) } } } } } } fn get_grid_input(filename: String) -> Grid(String) { let lines = filename |> simplifile.read() |> result.unwrap("") |> string.trim() |> string.split("\n") use grid, row, row_idx <- list.index_fold(lines, dict.new()) use grid, col, col_idx <- list.index_fold(string.to_graphemes(row), grid) dict.insert(grid, #(row_idx, col_idx), col) } fn part_one( grid: Grid(String), ) -> #(#(Set(#(#(Int, Int), Direction)), Loops), Int) { let guard = get_guard(grid) let obstacles = get_obstacles(grid) let visited = set.new() |> set.insert(#(guard.position, guard.direction)) let visited = recurse_grid(grid, guard, obstacles, visited) let visited_without_dir = set.fold(visited.0, set.new(), fn(acc, x) { set.insert(acc, x.0) }) #(visited, visited_without_dir |> set.size()) } fn check_loop(grid: Grid(String), blocker: Point) -> Loops { let blocked_grid = dict.upsert(grid, blocker, fn(x) { case x { Some("^") -> "^" Some(_) -> "#" None -> "#" } }) let visited = part_one(blocked_grid).0 visited.1 } fn part_two(grid: Grid(String), visited: Set(#(#(Int, Int), Direction))) { let visited = set.fold(visited, set.new(), fn(acc, x) { set.insert(acc, x.0) }) use counter, position <- set.fold(visited, 0) case check_loop(grid, position) { DoesLoop -> counter + 1 DoesNotLoop -> counter } } pub fn main() { let input = "input.in" let p1 = input |> get_grid_input() |> part_one let visited = p1.0.0 io.debug(p1.1) input |> get_grid_input |> part_two(visited) |> io.debug() }
Uiua
Part one was simple enough. Part two nearly made me give up.
Part two has the most ugly and least performant code I’ve made in uiua so far but it gets the job done and that’s all I care about for now.Run with example input here
RotateClock ← ( ⊙⊙(⍉⇌) ⊙(⇌⍜(⊡0)(-⊙(⧻⊡0.)+1)) ↻¯1 ) RotateCounter ← ( ⊙⊙(⇌⍉) ⊙(⍜(⊡0)(-⊙(⧻.)+1)⇌) ↻1 ) NewPos ← ( ⊙⍜(⊙⊡:)(-1+⊙(⊗@#)⟜↘⊙.)⟜°⊟ ⍜(⊡1)⋅ ) MarkPath ← ( RotateClock ⍢( # replace characters up til next '#' ⊙(⊙⍜(↘⊙⊡:)(⍜(↙)(▽:@^⧻)⊗@#.)⟜°⊟ NewPos ) RotateCounter | ⋅(≠0⊡0)) ◌◌ ) PartOne ← ( &rs ∞ &fo "input-6.txt" ⊜∘≠@\n. # maybe make compatible with # non-up facing inputs ♭⊚=@^. [0 1 2 3] MarkPath &fwa "test.txt" json. /+/+=@^ ) PartTwo ← ( &rs ∞ &fo "input-6.txt" ⊜∘≠@\n. # maybe make compatible with # non-up facing inputs ♭⊚=@^. [0 1 2 3] ◡MarkPath ⊙:: # rotate the field to match the intital state ⊙⊙( ⊙(⊚=@#) ⍢(⇌⍉|¬≍⊚=@#) ⊙◌ ) ⊙⊙(⊚=@^.) ⊙⊙⊙¤∩¤ ⊞(⊙⊙(⍜⊡⋅@#) RotateClock ⊙NewPos ¤¯1_¯1_¯1 ⍢(⊙◡(⊂⊢) ⊂ ⊙(RotateCounter ⊙NewPos ) | =1+⊙(∈↘1⇌)◡⋅(≠129⊡2)⊙(⊂⊢)) # 129 = length of input array. Hardcoded because # the condition block doesn't seem to get the # input array passed to it so the length can't # be read dynamically ⊙(⊂⊢) ∈ ⊙◌ ) /+♭ ) &p "Day 6:" &pf "Part 1: " &p PartOne &pf "Part 2: " &p PartTwo
Factor
spoiler
: get-input ( -- rows ) "vocab:aoc-2024/06/input.txt" utf8 file-lines ; : all-locations ( rows -- pairs ) dimension <coordinate-matrix> concat ; : guard-location ( rows -- pair ) [ all-locations ] keep '[ _ matrix-nth "<>^v" in? ] find nip ; TUPLE: state location char ; C: <state> state : guard-state ( rows -- state ) [ guard-location ] [ dupd matrix-nth ] bi <state> ; : faced-location ( state -- pair ) [ char>> H{ { CHAR: > { 0 1 } } { CHAR: v { 1 0 } } { CHAR: < { 0 -1 } } { CHAR: ^ { -1 0 } } } at ] [ location>> ] bi v+ ; : off-grid? ( rows location -- ? ) [ dimension ] dip [ v<= vany? ] keep { 0 0 } v< vany? or ; : turn ( state -- state' ) [ location>> ] [ char>> ] bi H{ { CHAR: > CHAR: v } { CHAR: v CHAR: < } { CHAR: < CHAR: ^ } { CHAR: ^ CHAR: > } } at <state> ; : obstacle? ( rows location -- ? ) swap matrix-nth CHAR: # = ; : guard-step ( rows state -- state' ) swap over faced-location { { [ 2dup off-grid? ] [ 2nip f <state> ] } { [ [ obstacle? ] keep-under ] [ drop turn ] } [ swap char>> <state> ] } cond ; : walk-out ( rows state -- trail ) [ [ 2dup location>> off-grid? ] [ dup location>> , dupd guard-step ] until ] { } make 2nip ; : part1 ( -- n ) get-input dup guard-state walk-out cardinality ; : (walk-loops?) ( visited rows state -- looped? ) dupd guard-step 2dup location>> off-grid? [ 3drop f ] [ pick dupd in? [ 3drop t ] [ pick dupd adjoin (walk-loops?) ] if ] if ; : walk-loops? ( rows -- looped? ) dup guard-state [ HS{ } clone ] 2dip pick dupd adjoin (walk-loops?) ; : obstacle-candidates ( rows -- pairs ) [ guard-location ] [ dup guard-state walk-out members ] bi remove ; : part2 ( -- n ) get-input dup obstacle-candidates [ CHAR: # spin deep-clone [ matrix-set-nth ] keep walk-loops? ] with count ;
J
Today’s the first one where I feel like the choice of language is a disadvantage without compensating advantages. Or, at least, I don’t know J well enough yet to use its compensating advantages for this kind of task, so what I end up with is Python 2 with obscure syntax and no associative data structures.
Also, I can’t post my code, because apparently Lemmy is interpreting some of today’s bizarre line noise as hostile and sanitizing it. It looks more or less like the other imperative solutions here, just with more punctuation.
Rust
This one was the first real think for this year, but I ended up brute forcing it, placing a ‘#’ in every position and checking. part 2 runs in about
380ms78ms (after reducing the amount ‘#’-placements to only where the guard walks) on my 2011 core i-7, so I’m happy, even though it feels like I could have been smarter.I am doing the same principle brute force but it takes ~7 seconds oO
Is using a
HashSet<(Pos, Dir)>
for the loop detection so expensive? My CPU shouldn’t be THAT bad…Part one around 7ms.
Also curious that i have not seen someone mention a more efficient approach, there gotta be one?
I created rows and cols vecs that keep places of blocks. When moving, I binary search the row or col, find the block that stops me. So moving whole sections at once. Otherwise used HashSet of pos and dir like you. Also in part 2, place the new block only on the path I take in part1. Part 2 is 26ms.
The binary search sounds smart, would reduce the pathing quite a bit i guess :)
Part 2 i approached quite the same i think, was only a couple lines of code additionally. But running 5ms 5000 times is also gonna take a while…
I’d like to see your solution in total. I’m not too familiar with the nuts and bolts, but hash set is quite a bit more expensive than a simple vector, there’s a bunch of overhead incurred when executing the hashing and placing of the data, and when repeating a few thousand times it sure adds up. My part one hovers around 600 microseconds.
I’d like to see your solution in total.
I set it up a bit like a game, https://pastebin.com/FGA6E7fA
My part one hovers around 600 microseconds.
Ohhh, that says my part 1 is slow already, i was sure my approach for 2 was the problem. Good to know!