day 12 puzzle 1
This commit is contained in:
parent
a6edde9139
commit
60a9ee3d2d
BIN
progress.png
BIN
progress.png
Binary file not shown.
Before Width: | Height: | Size: 121 KiB After Width: | Height: | Size: 122 KiB |
@ -19,4 +19,6 @@
|
|||||||
10:
|
10:
|
||||||
stars: 2
|
stars: 2
|
||||||
11:
|
11:
|
||||||
stars: 2
|
stars: 2
|
||||||
|
12:
|
||||||
|
stars: 1
|
4
res/examples/day12_1.txt
Normal file
4
res/examples/day12_1.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
AAAA
|
||||||
|
BBCD
|
||||||
|
BBCC
|
||||||
|
EEEC
|
5
res/examples/day12_2.txt
Normal file
5
res/examples/day12_2.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
OOOOO
|
||||||
|
OXOXO
|
||||||
|
OOOOO
|
||||||
|
OXOXO
|
||||||
|
OOOOO
|
10
res/examples/day12_3.txt
Normal file
10
res/examples/day12_3.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
RRRRIICCFF
|
||||||
|
RRRRIICCCF
|
||||||
|
VVRRRCCFFF
|
||||||
|
VVRCCCJFFF
|
||||||
|
VVVVCJJCFE
|
||||||
|
VVIVCCJJEE
|
||||||
|
VVIIICJJEE
|
||||||
|
MIIIIIJJEE
|
||||||
|
MIIISIJEEE
|
||||||
|
MMMISSJEEE
|
68
src/day12/puzzle1.typ
Normal file
68
src/day12/puzzle1.typ
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#import "/src/utils.typ": *
|
||||||
|
|
||||||
|
#let offsets = (
|
||||||
|
(-1, 0),
|
||||||
|
(0, -1),
|
||||||
|
(1, 0),
|
||||||
|
(0, 1)
|
||||||
|
)
|
||||||
|
|
||||||
|
#let in-grid(w, h, x, y) = {
|
||||||
|
return 0 <= x and x < w and 0 <= y and y < h
|
||||||
|
}
|
||||||
|
|
||||||
|
#let solve(input) = {
|
||||||
|
let grid = input.split("\n").map(l => l.clusters())
|
||||||
|
let w = grid.first().len()
|
||||||
|
let h = grid.len()
|
||||||
|
let in-grid = in-grid.with(w, h)
|
||||||
|
|
||||||
|
let visited = ((false,) * w,) * h
|
||||||
|
let total = 0
|
||||||
|
|
||||||
|
for y in range(h) {
|
||||||
|
for x in range(w) {
|
||||||
|
if visited.at(y).at(x) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
let char = grid.at(y).at(x)
|
||||||
|
let cells = ()
|
||||||
|
let borders = 0
|
||||||
|
let next-cells = ((x, y),)
|
||||||
|
while next-cells.len() != 0 {
|
||||||
|
let (cx, cy) = next-cells.remove(0)
|
||||||
|
cells.push((cx, cy))
|
||||||
|
visited.at(cy).at(cx) = true
|
||||||
|
for (dx, dy) in offsets {
|
||||||
|
let (x2, y2) = (cx + dx, cy + dy)
|
||||||
|
if (x2, y2) in cells or (x2, y2) in next-cells {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if in-grid(x2, y2) {
|
||||||
|
let char2 = grid.at(y2).at(x2)
|
||||||
|
if char2 == char {
|
||||||
|
next-cells.push((x2, y2))
|
||||||
|
} else {
|
||||||
|
borders += 1
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
borders += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
total += borders * cells.len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
|
#show-puzzle(
|
||||||
|
12, 1,
|
||||||
|
solve,
|
||||||
|
example: (
|
||||||
|
"1": 140,
|
||||||
|
"2": 772,
|
||||||
|
"3": 1930
|
||||||
|
)
|
||||||
|
)
|
0
src/day12/puzzle2.typ
Normal file
0
src/day12/puzzle2.typ
Normal file
BIN
src/main.pdf
BIN
src/main.pdf
Binary file not shown.
Loading…
Reference in New Issue
Block a user