-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
146 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
6 | ||
16 |