Skip to content

Commit 05e512f

Browse files
authored
dy-tea, day 18 and 19 (#80)
1 parent 11ab753 commit 05e512f

File tree

10 files changed

+231
-0
lines changed

10 files changed

+231
-0
lines changed

2024/18/.gitkeep

Whitespace-only changes.

2024/18/dy-tea.v

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import os
2+
import math
3+
import strconv
4+
5+
const input_file = 'positions.input'
6+
7+
fn print_grid(grid [][]bool) {
8+
for i in 0 .. grid.len {
9+
for j in 0 .. grid[i].len {
10+
print(if grid[i][j] { '#' } else { '.' })
11+
}
12+
print('\n')
13+
}
14+
}
15+
16+
fn solve_maze(grid [][]bool, sx int, sy int, ex int, ey int) int {
17+
dirs := [[1, 0], [0, -1], [-1, 0], [0, 1]]
18+
mut queue := [][]int{}
19+
mut visited := map[string]int{}
20+
mut min := max_int
21+
22+
queue << [sx, sy, 0]
23+
visited['${sx},${sy}'] = 0
24+
25+
for queue.len > 0 {
26+
curr := queue[0]
27+
28+
cx := curr[0]
29+
cy := curr[1]
30+
cc := curr[2]
31+
32+
queue.delete(0)
33+
34+
if cx == ex && cy == ey {
35+
min = math.min(min, cc)
36+
continue
37+
}
38+
39+
for i in 0 .. 4 {
40+
nx, ny := cx + dirs[i][0], cy + dirs[i][1]
41+
if nx >= 0 && ny >= 0 && ny < grid.len && nx < grid[0].len && !grid[nx][ny] {
42+
key := '${nx},${ny}'
43+
nc := cc + 1
44+
if key !in visited || visited[key] > nc {
45+
visited[key] = nc
46+
queue << [nx, ny, nc]
47+
}
48+
}
49+
}
50+
}
51+
52+
return min
53+
}
54+
55+
fn p1(input string) ! {
56+
lines := os.read_lines(input)!
57+
58+
mut grid := [][]bool{len: 71, init: []bool{len: 71, init: false}}
59+
60+
for i, line in lines {
61+
x, y := line.split_once(',') or { '', '' }
62+
63+
if i == 1024 {
64+
break
65+
}
66+
67+
grid[strconv.atoi(x)!][strconv.atoi(y)!] = true
68+
}
69+
70+
println(solve_maze(grid, 0, 0, 70, 70))
71+
}
72+
73+
fn p2(input string) ! {
74+
lines := os.read_lines(input)!
75+
76+
mut grid := [][]bool{len: 71, init: []bool{len: 71, init: false}}
77+
78+
for line in lines {
79+
x, y := line.split_once(',') or { '', '' }
80+
81+
grid[strconv.atoi(x)!][strconv.atoi(y)!] = true
82+
83+
if solve_maze(grid, 0, 0, 70, 70) == max_int {
84+
print_grid(grid)
85+
println(line)
86+
break
87+
}
88+
}
89+
}
90+
91+
fn main() {
92+
p1(input_file)!
93+
94+
// Note that the example input does not block off the exit and therefore does not have an answer
95+
p2(input_file)!
96+
}

2024/18/positions.input

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
5,4
2+
4,2
3+
4,5
4+
3,0
5+
2,1
6+
6,3
7+
2,4
8+
1,5
9+
0,6
10+
3,3
11+
2,6
12+
5,1
13+
1,2
14+
5,5
15+
2,5
16+
6,5
17+
1,4
18+
0,4
19+
6,4
20+
1,1
21+
6,1
22+
1,0
23+
0,5
24+
1,6
25+
2,0

2024/19/.gitkeep

Whitespace-only changes.

2024/19/dy-tea.v

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import os
2+
3+
const input_file := 'towels.input'
4+
5+
fn possible(towels []string, current string, query string, mut visited map[string]bool) bool {
6+
if current == query {
7+
return true
8+
}
9+
10+
if current.len >= query.len {
11+
return false
12+
}
13+
14+
if visited[current] {
15+
return false
16+
}
17+
18+
visited[current] = true
19+
20+
for towel in towels {
21+
n := current + towel
22+
23+
if query == n {
24+
return true
25+
}
26+
27+
if query.starts_with(n) {
28+
if possible(towels, n, query, mut visited) {
29+
return true
30+
}
31+
}
32+
}
33+
34+
return false
35+
}
36+
37+
fn count_combinations(towels []string, current string, query string, mut visited map[string]u64) u64 {
38+
if current == query {
39+
return 1
40+
}
41+
42+
if current.len >= query.len {
43+
return 0
44+
}
45+
46+
if current in visited {
47+
return visited[current]
48+
}
49+
50+
mut sum := u64(0)
51+
52+
for towel in towels {
53+
n := current + towel
54+
55+
if query.starts_with(n) {
56+
sum += count_combinations(towels, n, query, mut visited)
57+
}
58+
}
59+
60+
visited[current] = sum
61+
return sum
62+
}
63+
64+
fn p1(input string) ! {
65+
lines := os.read_lines(input)!
66+
67+
towels := lines[0].split(', ')
68+
queries := lines[2..]
69+
70+
mut sum := 0
71+
for query in queries {
72+
mut visited := map[string]bool{}
73+
sum += if possible(towels, '', query, mut visited) { 1 } else { 0 }
74+
}
75+
76+
println(sum)
77+
}
78+
79+
fn p2(input string) ! {
80+
lines := os.read_lines(input)!
81+
82+
towels := lines[0].split(', ')
83+
queries := lines[2..]
84+
85+
mut sum := u64(0)
86+
for query in queries {
87+
mut visited := map[string]u64{}
88+
sum += count_combinations(towels, '', query, mut visited)
89+
}
90+
91+
println(sum)
92+
}
93+
94+
fn main() {
95+
p1(input_file)!
96+
p2(input_file)!
97+
}

2024/19/towels.input

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
r, wr, b, g, bwu, rb, gb, br
2+
3+
brwrr
4+
bggr
5+
gbbr
6+
rrbgbr
7+
ubwu
8+
bwurrg
9+
brgr
10+
bbrgwb

known/2024/18/.gitkeep

Whitespace-only changes.

known/2024/18/dy-tea.out

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
146

known/2024/19/.gitkeep

Whitespace-only changes.

known/2024/19/dy-tea.out

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
6
2+
16

0 commit comments

Comments
 (0)