From 82272c9761d4d8eab398b87d0535c526de51de9792f6f688ed0600c90d004de0 Mon Sep 17 00:00:00 2001 From: nazrin Date: Fri, 6 Dec 2024 20:10:33 +0000 Subject: [PATCH] Day 6 --- 6.jl | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 6.jl diff --git a/6.jl b/6.jl new file mode 100644 index 0000000..3d16435 --- /dev/null +++ b/6.jl @@ -0,0 +1,60 @@ + +const grid = [ collect(line) for line in readlines("input/6") ] + +const size = length(grid) +const startY = findfirst(x -> '^' in values(x), grid) +const startX = findfirst(x -> x === '^', grid[startY]) + +const Coords = Tuple{Int,Int} + +const index(pos::Coords)::Char = index(pos[1], pos[2]) +const index(x::Int, y::Int)::Char = x > 0 && y > 0 && x <= size && y <= size ? grid[y][x] : '\0' + +mutable struct Guard + Guard() = new(( startX, startY ), ( 0, -1 )) + pos::Coords + vel::Coords +end +const rotate!(g::Guard) = g.vel = ( -g.vel[2], g.vel[1] ) + +begin + local visited::Set{Coords} = Set() + local guard = Guard() + while index(guard.pos) !== '\0' + push!(visited, guard.pos) + while index(guard.pos .+ guard.vel) === '#' + rotate!(guard) + end + guard.pos = guard.pos .+ guard.vel + end + + println("Part one ", length(visited)) +end + +begin + doesLoop = Threads.Atomic{Int}(0) + Threads.@threads for y in 1:size + for x in 1:size + if index(x, y) !== '.' continue end + local function tryPatrol()::Bool + local visited::Set{Pair{Coords,Coords}} = Set() + local guard = Guard() + while true + push!(visited, guard.pos => guard.vel) + while index(guard.pos .+ guard.vel) === '#' || (guard.pos .+ guard.vel) === (x, y) + rotate!(guard) + end + guard.pos = guard.pos .+ guard.vel + if index(guard.pos) === '\0' return false end + if (guard.pos => guard.vel) in visited return true end + end + end + if tryPatrol() + Threads.atomic_add!(doesLoop, 1) + end + end + end + + println("Part two ", doesLoop[]) +end +