From e31e934454885a6a3a4d214262b71c825d9c08e6 Mon Sep 17 00:00:00 2001 From: Dylan Donnell Date: Thu, 19 Dec 2024 12:01:16 +0000 Subject: [PATCH 1/3] feat: add 2024 day 18 solution --- 2024/18/dy-tea.v | 96 +++++++++++++++++++++++++++++++++++++++++ 2024/18/positions.input | 25 +++++++++++ 2 files changed, 121 insertions(+) create mode 100644 2024/18/dy-tea.v create mode 100644 2024/18/positions.input diff --git a/2024/18/dy-tea.v b/2024/18/dy-tea.v new file mode 100644 index 0000000..14bc53b --- /dev/null +++ b/2024/18/dy-tea.v @@ -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)! +} diff --git a/2024/18/positions.input b/2024/18/positions.input new file mode 100644 index 0000000..79c8583 --- /dev/null +++ b/2024/18/positions.input @@ -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 From 58254700dd718cf3c3e7062781d63757cde73631 Mon Sep 17 00:00:00 2001 From: Dylan Donnell Date: Thu, 19 Dec 2024 12:03:00 +0000 Subject: [PATCH 2/3] feat: add 2024 day 19 solution --- 2024/19/dy-tea.v | 97 ++++++++++++++++++++++++++++++++++++++++++++ 2024/19/towels.input | 10 +++++ 2 files changed, 107 insertions(+) create mode 100644 2024/19/dy-tea.v create mode 100644 2024/19/towels.input diff --git a/2024/19/dy-tea.v b/2024/19/dy-tea.v new file mode 100644 index 0000000..2a5b5e0 --- /dev/null +++ b/2024/19/dy-tea.v @@ -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)! +} diff --git a/2024/19/towels.input b/2024/19/towels.input new file mode 100644 index 0000000..29648be --- /dev/null +++ b/2024/19/towels.input @@ -0,0 +1,10 @@ +r, wr, b, g, bwu, rb, gb, br + +brwrr +bggr +gbbr +rrbgbr +ubwu +bwurrg +brgr +bbrgwb From 3896b6369f26e89ecec67c660e9cd5a8546e8496 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 22 Dec 2024 10:16:31 +0200 Subject: [PATCH 3/3] add outputs --- 2024/18/.gitkeep | 0 2024/19/.gitkeep | 0 known/2024/18/.gitkeep | 0 known/2024/18/dy-tea.out | 1 + known/2024/19/.gitkeep | 0 known/2024/19/dy-tea.out | 2 ++ 6 files changed, 3 insertions(+) delete mode 100644 2024/18/.gitkeep delete mode 100644 2024/19/.gitkeep delete mode 100644 known/2024/18/.gitkeep create mode 100644 known/2024/18/dy-tea.out delete mode 100644 known/2024/19/.gitkeep create mode 100644 known/2024/19/dy-tea.out diff --git a/2024/18/.gitkeep b/2024/18/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/2024/19/.gitkeep b/2024/19/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/known/2024/18/.gitkeep b/known/2024/18/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/known/2024/18/dy-tea.out b/known/2024/18/dy-tea.out new file mode 100644 index 0000000..878d5a0 --- /dev/null +++ b/known/2024/18/dy-tea.out @@ -0,0 +1 @@ +146 diff --git a/known/2024/19/.gitkeep b/known/2024/19/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/known/2024/19/dy-tea.out b/known/2024/19/dy-tea.out new file mode 100644 index 0000000..dc3ed24 --- /dev/null +++ b/known/2024/19/dy-tea.out @@ -0,0 +1,2 @@ +6 +16