Compare commits
2 Commits
f77a97fd80
...
7a8faae694
Author | SHA1 | Date | |
---|---|---|---|
7a8faae694 | |||
b1a59d3cf5 |
BIN
progress.png
BIN
progress.png
Binary file not shown.
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 89 KiB |
@ -1,2 +1,4 @@
|
|||||||
1:
|
1:
|
||||||
stars: 2
|
stars: 2
|
||||||
|
2:
|
||||||
|
stars: 1
|
6
res/examples/day2.txt
Normal file
6
res/examples/day2.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
7 6 4 2 1
|
||||||
|
1 2 7 8 9
|
||||||
|
9 7 6 2 1
|
||||||
|
1 3 2 4 5
|
||||||
|
8 6 4 4 1
|
||||||
|
1 3 6 7 9
|
@ -15,7 +15,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
#show-puzzle(
|
#show-puzzle(
|
||||||
1,
|
1, 1,
|
||||||
solve,
|
solve,
|
||||||
example: 11
|
example: 11
|
||||||
)
|
)
|
@ -1,4 +1,5 @@
|
|||||||
#import "/src/utils.typ": *
|
#import "/src/utils.typ": *
|
||||||
|
#import "@preview/cetz:0.3.1": canvas, draw
|
||||||
|
|
||||||
#let solve(input) = {
|
#let solve(input) = {
|
||||||
let lines = input.split("\n")
|
let lines = input.split("\n")
|
||||||
@ -29,8 +30,57 @@
|
|||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#let visualize(input) = {
|
||||||
|
let lines = input.split("\n")
|
||||||
|
let (l1, l2) = ((), ())
|
||||||
|
let reg = regex("^(\d+)\s+(\d+)$")
|
||||||
|
for line in lines {
|
||||||
|
let digits = line.match(reg)
|
||||||
|
l1.push(digits.captures.first())
|
||||||
|
l2.push(digits.captures.last())
|
||||||
|
}
|
||||||
|
|
||||||
|
let unique = l1.dedup()
|
||||||
|
let colors = (red,green)
|
||||||
|
canvas({
|
||||||
|
for (i, n) in l1.enumerate() {
|
||||||
|
let y = -0.9 * i
|
||||||
|
draw.circle(
|
||||||
|
(0, y),
|
||||||
|
radius: 0.35,
|
||||||
|
stroke: black,
|
||||||
|
name: "l" + str(i)
|
||||||
|
)
|
||||||
|
draw.content((0, y), n)
|
||||||
|
}
|
||||||
|
for (i, n) in l2.enumerate() {
|
||||||
|
let y = -0.9 * i
|
||||||
|
draw.circle(
|
||||||
|
(4, y),
|
||||||
|
radius: 0.35,
|
||||||
|
stroke: black,
|
||||||
|
name: "r" + str(i)
|
||||||
|
)
|
||||||
|
draw.content((4, y), n)
|
||||||
|
}
|
||||||
|
for (i, a) in l1.enumerate() {
|
||||||
|
for (j, b) in l2.enumerate() {
|
||||||
|
if a == b {
|
||||||
|
draw.line(
|
||||||
|
"l" + str(i),
|
||||||
|
"r" + str(j),
|
||||||
|
stroke: colors.at(unique.position(n => n == a)) + 1.5pt,
|
||||||
|
mark: (end: "straight")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#show-puzzle(
|
#show-puzzle(
|
||||||
1,
|
1, 2,
|
||||||
solve,
|
solve,
|
||||||
example: 31
|
example: 31,
|
||||||
|
visualize: visualize
|
||||||
)
|
)
|
35
src/day2/puzzle1.typ
Normal file
35
src/day2/puzzle1.typ
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#import "/src/utils.typ": *
|
||||||
|
|
||||||
|
#let solve(input) = {
|
||||||
|
let safe-cnt = 0
|
||||||
|
for line in input.split("\n") {
|
||||||
|
let nums = line.split(" ").map(n => int(n))
|
||||||
|
|
||||||
|
let increasing
|
||||||
|
let safe = true
|
||||||
|
for i in range(nums.len() - 1) {
|
||||||
|
let d = nums.at(i + 1) - nums.at(i)
|
||||||
|
let abs-d = calc.abs(d)
|
||||||
|
if abs-d < 1 or abs-d > 3 {
|
||||||
|
safe = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if i == 0 {
|
||||||
|
increasing = d > 0
|
||||||
|
} else if (d > 0) != increasing {
|
||||||
|
safe = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if safe {
|
||||||
|
safe-cnt += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return safe-cnt
|
||||||
|
}
|
||||||
|
|
||||||
|
#show-puzzle(
|
||||||
|
2, 1,
|
||||||
|
solve,
|
||||||
|
example: 2
|
||||||
|
)
|
0
src/day2/puzzle2.typ
Normal file
0
src/day2/puzzle2.typ
Normal file
BIN
src/main.pdf
BIN
src/main.pdf
Binary file not shown.
@ -1,5 +1,7 @@
|
|||||||
#let star-state = state("stars", (:))
|
#let star-state = state("stars", (:))
|
||||||
|
|
||||||
|
#let star = text(font: "Twitter Color Emoji", emoji.star)
|
||||||
|
|
||||||
#let get-input-path(day) = {
|
#let get-input-path(day) = {
|
||||||
return "/res/inputs/day" + str(day) + ".txt"
|
return "/res/inputs/day" + str(day) + ".txt"
|
||||||
}
|
}
|
||||||
@ -13,8 +15,15 @@
|
|||||||
return read(get-input-path(day))
|
return read(get-input-path(day))
|
||||||
}
|
}
|
||||||
|
|
||||||
#let check-example(day, func, target-result, suffix: none) = {
|
#let check-example(
|
||||||
let result = (func)(read(get-example-path(day, suffix: suffix)))
|
day,
|
||||||
|
func,
|
||||||
|
target-result,
|
||||||
|
suffix: none,
|
||||||
|
visualize: none
|
||||||
|
) = {
|
||||||
|
let input = read(get-example-path(day, suffix: suffix))
|
||||||
|
let result = (func)(input)
|
||||||
let passes = (result == target-result)
|
let passes = (result == target-result)
|
||||||
let name = if suffix == none [Example] else [Example '#suffix']
|
let name = if suffix == none [Example] else [Example '#suffix']
|
||||||
let badge = box(
|
let badge = box(
|
||||||
@ -38,6 +47,14 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
[#badge #h(0.6em)]
|
[#badge #h(0.6em)]
|
||||||
|
|
||||||
|
if visualize != none {
|
||||||
|
linebreak()
|
||||||
|
figure(
|
||||||
|
visualize(input),
|
||||||
|
caption: [#name (visualization)]
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#let show-result(result) = {
|
#let show-result(result) = {
|
||||||
@ -46,30 +63,35 @@
|
|||||||
radius: 1.2em,
|
radius: 1.2em,
|
||||||
baseline: 35%,
|
baseline: 35%,
|
||||||
fill: blue.lighten(20%),
|
fill: blue.lighten(20%),
|
||||||
text(fill: white)[Result: #result]
|
text(fill: white)[Result: #raw(repr(result))]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#let show-puzzle(puzzle, func, example: none) = {
|
#let show-puzzle(day, puzzle, func, example: none, visualize: none) = {
|
||||||
|
let check-example = check-example.with(visualize: visualize)
|
||||||
if example != none {
|
if example != none {
|
||||||
if type(example) == dictionary {
|
if type(example) == dictionary {
|
||||||
for (suffix, result) in example.pairs() {
|
for (suffix, result) in example.pairs() {
|
||||||
check-example(puzzle, func, result, suffix: suffix)
|
check-example(day, func, result, suffix: suffix)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
check-example(puzzle, func, example)
|
check-example(day, func, example)
|
||||||
}
|
}
|
||||||
linebreak()
|
linebreak()
|
||||||
}
|
}
|
||||||
|
|
||||||
let input = get-input(1)
|
let input = get-input(day)
|
||||||
let result = (func)(input)
|
let result = (func)(input)
|
||||||
show-result(result)
|
show-result(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
#let day-template(day, puzzle1, puzzle2, stars: 0) = {
|
#let day-template(day, puzzle1, puzzle2, stars: 0) = {
|
||||||
pagebreak(weak: true)
|
pagebreak(weak: true)
|
||||||
let title = [Day #day] + ((emoji.star,)*stars).join()
|
let title = [Day #day] + box(
|
||||||
|
baseline: -1pt,
|
||||||
|
((star,)*stars).join()
|
||||||
|
)
|
||||||
|
|
||||||
[
|
[
|
||||||
= #title
|
= #title
|
||||||
#label("day-" + str(day))
|
#label("day-" + str(day))
|
||||||
@ -108,7 +130,7 @@
|
|||||||
dir: ttb,
|
dir: ttb,
|
||||||
spacing: 0.2em,
|
spacing: 0.2em,
|
||||||
cell,
|
cell,
|
||||||
h(3pt) + ((emoji.star,)* stars.at(str(i))).join()
|
((star,)* stars.at(str(i))).join()
|
||||||
)
|
)
|
||||||
if links {
|
if links {
|
||||||
cell = link(label("day-" + str(i)), cell)
|
cell = link(label("day-" + str(i)), cell)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user