Skip to content

Commit

Permalink
leodev, day 06 (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0Developer authored Dec 6, 2024
1 parent 0294e37 commit 1c5b290
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 0 deletions.
Empty file removed 2024/06/.gitkeep
Empty file.
162 changes: 162 additions & 0 deletions 2024/06/leodev.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import os

const direction_vectors = [
[-1, 0]!,
[0, 1]!,
[1, 0]!,
[0, -1]!,
]!

fn main() {
lines := os.read_lines('map.input')!

size_y := lines.len
size_x := lines[0].len

mut obstructed := []bool{len: size_y * size_x}

mut guard_y := 0
mut guard_x := 0
mut guard_direction := Direction.up

for y, line in lines {
for x, ch in line {
match ch {
`^` {
guard_y = y
guard_x = x
}
`#` {
obstructed[y * size_y + x] = true
}
else {}
}
}
}

mut traverser := Traverser{
obstructed: obstructed
head: true
size_y: size_y
size_x: size_x
traversed: []Direction{len: size_y * size_x, init: Direction.blank}
guard_y: guard_y
guard_x: guard_x
guard_direction: guard_direction
loops: []bool{len: size_y * size_x}
}
traverser.run()
traverser.print()

travelled := traverser.traversed.count(it != .blank)
println('part1: ${travelled}')
obstructions := traverser.loops.count(it)
println('part2: ${obstructions}')
}

struct Traverser {
head bool
size_y int
size_x int
mut:
obstructed []bool
traversed []Direction
guard_y int
guard_x int
guard_direction Direction
loops []bool
}

fn (mut t Traverser) run() bool {
mut steps := 0
for t.in_bounds(t.guard_y, t.guard_x) {
if t.head {
println('guard at (${t.guard_y}|${t.guard_x}); moving ${t.guard_direction}')
}
steps++
if steps > t.size_y * t.size_x * 10 {
return true
}
t.traversed[t.guard_y * t.size_y + t.guard_x] = t.guard_direction

next_y := t.guard_y + direction_vectors[t.guard_direction][0]
next_x := t.guard_x + direction_vectors[t.guard_direction][1]

if t.in_bounds(next_y, next_x) && t.obstructed[next_y * t.size_y + next_x] {
t.guard_direction = unsafe {
Direction((int(t.guard_direction) + 1) % 4)
}
} else {
if t.head && t.in_bounds(next_y, next_x)
&& t.traversed[next_y * t.size_y + next_x] == .blank {
mut cloned := t.clone()
cloned.traversed[t.guard_y * t.size_y + t.guard_x] = .blank
cloned.obstructed[next_y * t.size_y + next_x] = true
if cloned.run() {
println('obstruction at (${next_y}|${next_x}) worked')
t.loops[next_y * t.size_y + next_x] = true
// cloned.print()
}
}

t.guard_y = next_y
t.guard_x = next_x
}
}

return false
}

fn (t Traverser) in_bounds(y int, x int) bool {
return y >= 0 && y < t.size_y && x >= 0 && x < t.size_x
}

fn (t Traverser) print() {
for y in 0 .. t.size_y {
for x in 0 .. t.size_x {
match t.traversed[y * t.size_y + x] {
.up {
print('^')
}
.right {
print('>')
}
.down {
print('v')
}
.left {
print('<')
}
else {
if t.obstructed[y * t.size_y + x] {
print('#')
} else {
print('.')
}
}
}
}
print('\n')
}
}

fn (t Traverser) clone() Traverser {
return Traverser{
head: false
size_y: t.size_y
size_x: t.size_x
obstructed: t.obstructed.clone()
traversed: t.traversed.clone()
guard_y: t.guard_y
guard_x: t.guard_x
guard_direction: t.guard_direction
}
}

enum Direction {
up
right
down
left
blank
}
10 changes: 10 additions & 0 deletions 2024/06/map.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
Empty file removed known/2024/06/.gitkeep
Empty file.
73 changes: 73 additions & 0 deletions known/2024/06/leodev.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
guard at (6|4); moving up
guard at (5|4); moving up
guard at (4|4); moving up
guard at (3|4); moving up
guard at (2|4); moving up
guard at (1|4); moving up
guard at (1|4); moving right
guard at (1|5); moving right
guard at (1|6); moving right
guard at (1|7); moving right
guard at (1|8); moving right
guard at (1|8); moving down
guard at (2|8); moving down
guard at (3|8); moving down
guard at (4|8); moving down
guard at (5|8); moving down
guard at (6|8); moving down
guard at (6|8); moving left
guard at (6|7); moving left
guard at (6|6); moving left
guard at (6|5); moving left
guard at (6|4); moving left
obstruction at (6|3) worked
guard at (6|3); moving left
guard at (6|2); moving left
guard at (6|2); moving up
guard at (5|2); moving up
guard at (4|2); moving up
guard at (4|2); moving right
guard at (4|3); moving right
guard at (4|4); moving right
guard at (4|5); moving right
guard at (4|6); moving right
guard at (4|6); moving down
guard at (5|6); moving down
guard at (6|6); moving down
obstruction at (7|6) worked
guard at (7|6); moving down
guard at (8|6); moving down
guard at (8|6); moving left
guard at (8|5); moving left
guard at (8|4); moving left
obstruction at (8|3) worked
guard at (8|3); moving left
guard at (8|2); moving left
obstruction at (8|1) worked
guard at (8|1); moving left
guard at (8|1); moving up
guard at (7|1); moving up
guard at (7|1); moving right
guard at (7|2); moving right
guard at (7|3); moving right
guard at (7|4); moving right
guard at (7|5); moving right
guard at (7|6); moving right
obstruction at (7|7) worked
guard at (7|7); moving right
guard at (7|7); moving down
guard at (8|7); moving down
obstruction at (9|7) worked
guard at (9|7); moving down
....#.....
....>>>>v#
....^...v.
..#.^...v.
..>>>>v#v.
..^.^.v.v.
.#^<<<v<<.
.>>>>>>v#.
#^<<<<<v..
......#v..
part1: 41
part2: 6

0 comments on commit 1c5b290

Please sign in to comment.