started day 21 puzzle 1

This commit is contained in:
Louis Heredero 2024-12-22 13:09:23 +01:00
parent 4826c0d21e
commit 47fbeb1ce9
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
5 changed files with 108 additions and 1 deletions

View File

@ -37,4 +37,6 @@
19:
stars: 2
20:
stars: 1
stars: 1
21:
stars: 0

5
res/examples/day21.txt Normal file
View File

@ -0,0 +1,5 @@
029A
980A
179A
456A
379A

100
src/day21/puzzle1.typ Normal file
View File

@ -0,0 +1,100 @@
#import "/src/utils.typ": *
#let nums = (
"7": (0, 0),
"8": (1, 0),
"9": (2, 0),
"4": (0, 1),
"5": (1, 1),
"6": (2, 1),
"1": (0, 2),
"2": (1, 2),
"3": (2, 2),
"0": (1, 3),
"A": (2, 3),
)
#let arrows = (
"^": (1, 0),
"A": (2, 0),
"<": (0, 1),
"v": (1, 1),
">": (2, 1),
)
#let get-num-path(code) = {
let path = ""
let (x, y) = nums.at("A")
for char in code {
let (x2, y2) = nums.at(char)
let dy = y2 - y
let dx = x2 - x
let ver = if dy == 0 {
""
} else {
(if dy < 0 {"^"} else {"v"}) * calc.abs(dy)
}
let hor = if dx == 0 {
""
} else {
(if dx < 0 {"<"} else {">"}) * calc.abs(dx)
}
path += if y == 3 and x2 == 0 {
ver + hor
} else {
hor + ver
} + "A"
(x, y) = (x2, y2)
}
return path
}
#let get-dir-path(code) = {
let path = ""
let (x, y) = arrows.at("A")
for char in code {
let (x2, y2) = arrows.at(char)
let dy = y2 - y
let dx = x2 - x
let ver = if dy == 0 {
""
} else {
(if dy < 0 {"^"} else {"v"}) * calc.abs(dy)
}
let hor = if dx == 0 {
""
} else {
(if dx < 0 {"<"} else {">"}) * calc.abs(dx)
}
path += if x == 0 and dy < 0 {
hor + ver
} else {
ver + hor
} + "A"
(x, y) = (x2, y2)
}
return path
}
#let get-path(code) = {
let num-path = get-num-path(code)
let dir-path-1 = get-dir-path(num-path)
let dir-path-2 = get-dir-path(dir-path-1)
return dir-path-2
}
#let solve(input) = {
let codes = input.split("\n")
let total = 0
for code in codes {
let len = get-path(code).len()
let num = int(code.slice(0, code.len() - 1))
total += len * num
}
return total
}
#show-puzzle(
21, 1,
solve,
example: 126384
)

0
src/day21/puzzle2.typ Normal file
View File

Binary file not shown.