diff --git a/progress.yaml b/progress.yaml index 385d324..ff71352 100644 --- a/progress.yaml +++ b/progress.yaml @@ -37,4 +37,6 @@ 19: stars: 2 20: - stars: 1 \ No newline at end of file + stars: 1 +21: + stars: 0 \ No newline at end of file diff --git a/res/examples/day21.txt b/res/examples/day21.txt new file mode 100644 index 0000000..dd73dfd --- /dev/null +++ b/res/examples/day21.txt @@ -0,0 +1,5 @@ +029A +980A +179A +456A +379A \ No newline at end of file diff --git a/src/day21/puzzle1.typ b/src/day21/puzzle1.typ new file mode 100644 index 0000000..791bcff --- /dev/null +++ b/src/day21/puzzle1.typ @@ -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 +) diff --git a/src/day21/puzzle2.typ b/src/day21/puzzle2.typ new file mode 100644 index 0000000..e69de29 diff --git a/src/main.pdf b/src/main.pdf index 99bb2c6..808edb3 100644 Binary files a/src/main.pdf and b/src/main.pdf differ