Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

day 10 #70

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed 2024/10/.gitkeep
Empty file.
73 changes: 73 additions & 0 deletions 2024/10/leodev.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import os

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

mut trailheads := [][2]int{}
for y, line in lines {
for x, c in line {
if c == `0` {
trailheads << [y, x]!
}
}
}

mut state := State{
lines: lines
}

mut total1 := 0
mut total2 := 0
for th in trailheads {
total1 += state.traverse(th[0], th[1])
total2 += state.part2()
}

println('part1: ${total1}')
println('part2: ${total2}')
}

struct State {
lines []string
mut:
trailends map[string]int
}

fn (mut s State) traverse(y int, x int) int {
s.trailends = map[string]int{}
s.t(y, x, `0`)
return s.trailends.len
}

fn (s State) part2() int {
mut total := 0
for _, v in s.trailends {
total += v
}
return total
}

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

fn (mut s State) t(y int, x int, current u8) {
for off in offsets {
ny := y + off[0]
nx := x + off[1]
if ny < 0 || ny >= s.lines.len || nx < 0 || nx >= s.lines[0].len {
continue
}
if s.lines[ny][nx] != current + 1 {
continue
}
if current == `8` {
s.trailends['${ny},${nx}']++
continue
}
s.t(ny, nx, s.lines[ny][nx])
}
}
8 changes: 8 additions & 0 deletions 2024/10/map.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
Empty file removed known/2024/10/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions known/2024/10/leodev.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
part1: 36
part2: 81
Loading