Compare commits
3 Commits
0d89355f86
...
1f0389071c
Author | SHA1 | Date | |
---|---|---|---|
1f0389071c | |||
6374452e24 | |||
da56d30760 |
BIN
progress.png
BIN
progress.png
Binary file not shown.
Before Width: | Height: | Size: 113 KiB After Width: | Height: | Size: 114 KiB |
@ -11,4 +11,6 @@
|
|||||||
6:
|
6:
|
||||||
stars: 2
|
stars: 2
|
||||||
7:
|
7:
|
||||||
stars: 2
|
stars: 2
|
||||||
|
8:
|
||||||
|
stars: 1
|
12
res/examples/day8.txt
Normal file
12
res/examples/day8.txt
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
............
|
||||||
|
........0...
|
||||||
|
.....0......
|
||||||
|
.......0....
|
||||||
|
....0.......
|
||||||
|
......A.....
|
||||||
|
............
|
||||||
|
............
|
||||||
|
........A...
|
||||||
|
.........A..
|
||||||
|
............
|
||||||
|
............
|
@ -1,7 +1,8 @@
|
|||||||
#import "/src/utils.typ": *
|
#import "/src/utils.typ": *
|
||||||
|
|
||||||
|
#let reg = regex("mul\((\d{1,3}),(\d{1,3})\)")
|
||||||
#let solve(input) = {
|
#let solve(input) = {
|
||||||
let matches = input.matches(regex("mul\((\d{1,3}),(\d{1,3})\)"))
|
let matches = input.matches(reg)
|
||||||
let total = matches.map(m => {
|
let total = matches.map(m => {
|
||||||
m.captures.map(int)
|
m.captures.map(int)
|
||||||
.product()
|
.product()
|
||||||
@ -9,8 +10,24 @@
|
|||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#let visualize(input) = {
|
||||||
|
[
|
||||||
|
#set text(size: 1.2em)
|
||||||
|
#show reg: it => {
|
||||||
|
let m = it.text.match(reg)
|
||||||
|
let v = m.captures.map(int).product()
|
||||||
|
math.underbrace(
|
||||||
|
highlight(fill: red, raw(it.text)),
|
||||||
|
text(size: 1.5em, str(v))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
#raw(input)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
#show-puzzle(
|
#show-puzzle(
|
||||||
3, 1,
|
3, 1,
|
||||||
solve,
|
solve,
|
||||||
example: ("1": 161)
|
example: ("1": 161),
|
||||||
|
visualize: visualize
|
||||||
)
|
)
|
@ -1,4 +1,5 @@
|
|||||||
#import "/src/utils.typ": *
|
#import "/src/utils.typ": *
|
||||||
|
#import "@preview/cetz:0.3.1": canvas, draw
|
||||||
|
|
||||||
#let make-rules-dict(rules) = {
|
#let make-rules-dict(rules) = {
|
||||||
let dict = (:)
|
let dict = (:)
|
||||||
@ -45,8 +46,82 @@
|
|||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#let visualize(input) = {
|
||||||
|
let (rules, updates) = input.split("\n\n")
|
||||||
|
rules = rules.split("\n").map(l => l.split("|").map(int))
|
||||||
|
updates = updates.split("\n").map(l => l.split(",").map(int))
|
||||||
|
|
||||||
|
let total = 0
|
||||||
|
//let rules-dict = make-rules-dict(rules)
|
||||||
|
|
||||||
|
let diags = ()
|
||||||
|
for update in updates {
|
||||||
|
let diag = canvas(length: 3em, {
|
||||||
|
for (x, n) in update.enumerate() {
|
||||||
|
draw.circle(
|
||||||
|
(x, 0),
|
||||||
|
radius: 0.4,
|
||||||
|
name: str(x)
|
||||||
|
)
|
||||||
|
draw.content(
|
||||||
|
(x, 0),
|
||||||
|
str(n)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
let flip = false
|
||||||
|
let c = 1
|
||||||
|
for (a, b) in rules {
|
||||||
|
let i = update.position(n => n == a)
|
||||||
|
let j = update.position(n => n == b)
|
||||||
|
if i == none or j == none {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
let anchor = if flip {".south"} else {".north"}
|
||||||
|
let pt-i = str(i) + anchor
|
||||||
|
let pt-j = str(j) + anchor
|
||||||
|
let col = if j < i {red} else {green}
|
||||||
|
|
||||||
|
draw.arc-through(
|
||||||
|
pt-i,
|
||||||
|
(
|
||||||
|
rel: (0, if flip {-c / 10} else {c / 10}),
|
||||||
|
to: (pt-i, 50%, pt-j)
|
||||||
|
),
|
||||||
|
pt-j,
|
||||||
|
mark: (end: ">", fill: col),
|
||||||
|
stroke: col
|
||||||
|
)
|
||||||
|
|
||||||
|
flip = not flip
|
||||||
|
c += 1
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
diags.push(diag)
|
||||||
|
|
||||||
|
/*if is-update-valid(rules-dict, update) {
|
||||||
|
total += update.at(calc.div-euclid(update.len(), 2))
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
|
||||||
|
diags.last() = grid.cell(
|
||||||
|
colspan: 2 - calc.rem(diags.len() - 1, 2),
|
||||||
|
diags.last()
|
||||||
|
)
|
||||||
|
|
||||||
|
grid(
|
||||||
|
columns: 2,
|
||||||
|
stroke: (paint: black, dash: "dashed"),
|
||||||
|
align: center + horizon,
|
||||||
|
inset: 0.4em,
|
||||||
|
..diags
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#show-puzzle(
|
#show-puzzle(
|
||||||
5, 1,
|
5, 1,
|
||||||
solve,
|
solve,
|
||||||
example: 143
|
example: 143,
|
||||||
|
visualize: visualize
|
||||||
)
|
)
|
51
src/day8/puzzle1.typ
Normal file
51
src/day8/puzzle1.typ
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#import "/src/utils.typ": *
|
||||||
|
|
||||||
|
#let in-grid(w, h, x, y) = {
|
||||||
|
return 0 <= x and x < w and 0 <= y and y < h
|
||||||
|
}
|
||||||
|
|
||||||
|
#let solve(input) = {
|
||||||
|
let by-freq = (:)
|
||||||
|
let antinodes = ()
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
for y in range(h) {
|
||||||
|
for x in range(w) {
|
||||||
|
let c = grid.at(y).at(x)
|
||||||
|
if c == "." {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if c not in by-freq {
|
||||||
|
by-freq.insert(c, ())
|
||||||
|
}
|
||||||
|
|
||||||
|
for (x2, y2) in by-freq.at(c) {
|
||||||
|
let (dx, dy) = (x2 - x, y2 - y)
|
||||||
|
let node1 = (x - dx, y - dy)
|
||||||
|
let node2 = (x2 + dx, y2 + dy)
|
||||||
|
|
||||||
|
if in-grid(..node1) and node1 not in antinodes {
|
||||||
|
antinodes.push(node1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if in-grid(..node2) and node2 not in antinodes {
|
||||||
|
antinodes.push(node2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
by-freq.at(c).push((x, y))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return antinodes.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
#show-puzzle(
|
||||||
|
8, 1,
|
||||||
|
solve,
|
||||||
|
example: 14
|
||||||
|
)
|
0
src/day8/puzzle2.typ
Normal file
0
src/day8/puzzle2.typ
Normal file
BIN
src/main.pdf
BIN
src/main.pdf
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user