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.