Skip to content

Commit

Permalink
dy-tea, day 18 and 19 (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
dy-tea authored Dec 22, 2024
1 parent 11ab753 commit 05e512f
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 0 deletions.
Empty file removed 2024/18/.gitkeep
Empty file.
96 changes: 96 additions & 0 deletions 2024/18/dy-tea.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import os
import math
import strconv

const input_file = 'positions.input'

fn print_grid(grid [][]bool) {
for i in 0 .. grid.len {
for j in 0 .. grid[i].len {
print(if grid[i][j] { '#' } else { '.' })
}
print('\n')
}
}

fn solve_maze(grid [][]bool, sx int, sy int, ex int, ey int) int {
dirs := [[1, 0], [0, -1], [-1, 0], [0, 1]]
mut queue := [][]int{}
mut visited := map[string]int{}
mut min := max_int

queue << [sx, sy, 0]
visited['${sx},${sy}'] = 0

for queue.len > 0 {
curr := queue[0]

cx := curr[0]
cy := curr[1]
cc := curr[2]

queue.delete(0)

if cx == ex && cy == ey {
min = math.min(min, cc)
continue
}

for i in 0 .. 4 {
nx, ny := cx + dirs[i][0], cy + dirs[i][1]
if nx >= 0 && ny >= 0 && ny < grid.len && nx < grid[0].len && !grid[nx][ny] {
key := '${nx},${ny}'
nc := cc + 1
if key !in visited || visited[key] > nc {
visited[key] = nc
queue << [nx, ny, nc]
}
}
}
}

return min
}

fn p1(input string) ! {
lines := os.read_lines(input)!

mut grid := [][]bool{len: 71, init: []bool{len: 71, init: false}}

for i, line in lines {
x, y := line.split_once(',') or { '', '' }

if i == 1024 {
break
}

grid[strconv.atoi(x)!][strconv.atoi(y)!] = true
}

println(solve_maze(grid, 0, 0, 70, 70))
}

fn p2(input string) ! {
lines := os.read_lines(input)!

mut grid := [][]bool{len: 71, init: []bool{len: 71, init: false}}

for line in lines {
x, y := line.split_once(',') or { '', '' }

grid[strconv.atoi(x)!][strconv.atoi(y)!] = true

if solve_maze(grid, 0, 0, 70, 70) == max_int {
print_grid(grid)
println(line)
break
}
}
}

fn main() {
p1(input_file)!

// Note that the example input does not block off the exit and therefore does not have an answer
p2(input_file)!
}
25 changes: 25 additions & 0 deletions 2024/18/positions.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
5,4
4,2
4,5
3,0
2,1
6,3
2,4
1,5
0,6
3,3
2,6
5,1
1,2
5,5
2,5
6,5
1,4
0,4
6,4
1,1
6,1
1,0
0,5
1,6
2,0
Empty file removed 2024/19/.gitkeep
Empty file.
97 changes: 97 additions & 0 deletions 2024/19/dy-tea.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import os

const input_file := 'towels.input'

fn possible(towels []string, current string, query string, mut visited map[string]bool) bool {
if current == query {
return true
}

if current.len >= query.len {
return false
}

if visited[current] {
return false
}

visited[current] = true

for towel in towels {
n := current + towel

if query == n {
return true
}

if query.starts_with(n) {
if possible(towels, n, query, mut visited) {
return true
}
}
}

return false
}

fn count_combinations(towels []string, current string, query string, mut visited map[string]u64) u64 {
if current == query {
return 1
}

if current.len >= query.len {
return 0
}

if current in visited {
return visited[current]
}

mut sum := u64(0)

for towel in towels {
n := current + towel

if query.starts_with(n) {
sum += count_combinations(towels, n, query, mut visited)
}
}

visited[current] = sum
return sum
}

fn p1(input string) ! {
lines := os.read_lines(input)!

towels := lines[0].split(', ')
queries := lines[2..]

mut sum := 0
for query in queries {
mut visited := map[string]bool{}
sum += if possible(towels, '', query, mut visited) { 1 } else { 0 }
}

println(sum)
}

fn p2(input string) ! {
lines := os.read_lines(input)!

towels := lines[0].split(', ')
queries := lines[2..]

mut sum := u64(0)
for query in queries {
mut visited := map[string]u64{}
sum += count_combinations(towels, '', query, mut visited)
}

println(sum)
}

fn main() {
p1(input_file)!
p2(input_file)!
}
10 changes: 10 additions & 0 deletions 2024/19/towels.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
r, wr, b, g, bwu, rb, gb, br

brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb
Empty file removed known/2024/18/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions known/2024/18/dy-tea.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
146
Empty file removed known/2024/19/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions known/2024/19/dy-tea.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
6
16

0 comments on commit 05e512f

Please sign in to comment.