From 61e1d8db37ae01c0b8e59a9b5039022440503fb2 Mon Sep 17 00:00:00 2001 From: Seth Etter Date: Sat, 2 Dec 2023 07:48:41 -0600 Subject: [PATCH] 2023 day01: part two is still wrong I've spot checked lines in the input and logged intermediate output and everything checks out, I can't figure out why my answer is still too low! --- 2023/README.md | 3 +++ 2023/base/deno.json | 6 +++++ 2023/base/part1.ts | 4 +++ 2023/day01/deno.json | 6 +++++ 2023/day01/deno.lock | 37 +++++++++++++++++++++++++++ 2023/day01/part1.ts | 25 ++++++++++++++++++ 2023/day01/part2.ts | 40 +++++++++++++++++++++++++++++ 2023/day01/puzzle.md | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2023/day01/test.ts | 41 ++++++++++++++++++++++++++++++ 9 files changed, 222 insertions(+) create mode 100644 2023/README.md create mode 100644 2023/base/deno.json create mode 100644 2023/base/part1.ts create mode 100644 2023/day01/deno.json create mode 100644 2023/day01/deno.lock create mode 100644 2023/day01/part1.ts create mode 100644 2023/day01/part2.ts create mode 100644 2023/day01/puzzle.md create mode 100644 2023/day01/test.ts diff --git a/2023/README.md b/2023/README.md new file mode 100644 index 0000000..53aeda8 --- /dev/null +++ b/2023/README.md @@ -0,0 +1,3 @@ +# Advent of Code, 2023 + + diff --git a/2023/base/deno.json b/2023/base/deno.json new file mode 100644 index 0000000..5881ebc --- /dev/null +++ b/2023/base/deno.json @@ -0,0 +1,6 @@ +{ + "tasks": { + "p1": "deno run -A part1.ts", + "p2": "deno run -A part2.ts" + } +} diff --git a/2023/base/part1.ts b/2023/base/part1.ts new file mode 100644 index 0000000..94d9dc3 --- /dev/null +++ b/2023/base/part1.ts @@ -0,0 +1,4 @@ +const input = await Deno.readFile("input").then((bytes) => + new TextDecoder().decode(bytes), +); +const lines = input.trim().split("\n"); diff --git a/2023/day01/deno.json b/2023/day01/deno.json new file mode 100644 index 0000000..5881ebc --- /dev/null +++ b/2023/day01/deno.json @@ -0,0 +1,6 @@ +{ + "tasks": { + "p1": "deno run -A part1.ts", + "p2": "deno run -A part2.ts" + } +} diff --git a/2023/day01/deno.lock b/2023/day01/deno.lock new file mode 100644 index 0000000..2c1d001 --- /dev/null +++ b/2023/day01/deno.lock @@ -0,0 +1,37 @@ +{ + "version": "3", + "remote": { + "https://deno.land/std@0.208.0/assert/_constants.ts": "8a9da298c26750b28b326b297316cdde860bc237533b07e1337c021379e6b2a9", + "https://deno.land/std@0.208.0/assert/_diff.ts": "58e1461cc61d8eb1eacbf2a010932bf6a05b79344b02ca38095f9b805795dc48", + "https://deno.land/std@0.208.0/assert/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7", + "https://deno.land/std@0.208.0/assert/assert.ts": "9a97dad6d98c238938e7540736b826440ad8c1c1e54430ca4c4e623e585607ee", + "https://deno.land/std@0.208.0/assert/assert_almost_equals.ts": "e15ca1f34d0d5e0afae63b3f5d975cbd18335a132e42b0c747d282f62ad2cd6c", + "https://deno.land/std@0.208.0/assert/assert_array_includes.ts": "6856d7f2c3544bc6e62fb4646dfefa3d1df5ff14744d1bca19f0cbaf3b0d66c9", + "https://deno.land/std@0.208.0/assert/assert_equals.ts": "d8ec8a22447fbaf2fc9d7c3ed2e66790fdb74beae3e482855d75782218d68227", + "https://deno.land/std@0.208.0/assert/assert_exists.ts": "407cb6b9fb23a835cd8d5ad804e2e2edbbbf3870e322d53f79e1c7a512e2efd7", + "https://deno.land/std@0.208.0/assert/assert_false.ts": "0ccbcaae910f52c857192ff16ea08bda40fdc79de80846c206bfc061e8c851c6", + "https://deno.land/std@0.208.0/assert/assert_greater.ts": "ae2158a2d19313bf675bf7251d31c6dc52973edb12ac64ac8fc7064152af3e63", + "https://deno.land/std@0.208.0/assert/assert_greater_or_equal.ts": "1439da5ebbe20855446cac50097ac78b9742abe8e9a43e7de1ce1426d556e89c", + "https://deno.land/std@0.208.0/assert/assert_instance_of.ts": "3aedb3d8186e120812d2b3a5dea66a6e42bf8c57a8bd927645770bd21eea554c", + "https://deno.land/std@0.208.0/assert/assert_is_error.ts": "c21113094a51a296ffaf036767d616a78a2ae5f9f7bbd464cd0197476498b94b", + "https://deno.land/std@0.208.0/assert/assert_less.ts": "aec695db57db42ec3e2b62e97e1e93db0063f5a6ec133326cc290ff4b71b47e4", + "https://deno.land/std@0.208.0/assert/assert_less_or_equal.ts": "5fa8b6a3ffa20fd0a05032fe7257bf985d207b85685fdbcd23651b70f928c848", + "https://deno.land/std@0.208.0/assert/assert_match.ts": "c4083f80600bc190309903c95e397a7c9257ff8b5ae5c7ef91e834704e672e9b", + "https://deno.land/std@0.208.0/assert/assert_not_equals.ts": "9f1acab95bd1f5fc9a1b17b8027d894509a745d91bac1718fdab51dc76831754", + "https://deno.land/std@0.208.0/assert/assert_not_instance_of.ts": "0c14d3dfd9ab7a5276ed8ed0b18c703d79a3d106102077ec437bfe7ed912bd22", + "https://deno.land/std@0.208.0/assert/assert_not_match.ts": "3796a5b0c57a1ce6c1c57883dd4286be13a26f715ea662318ab43a8491a13ab0", + "https://deno.land/std@0.208.0/assert/assert_not_strict_equals.ts": "4cdef83df17488df555c8aac1f7f5ec2b84ad161b6d0645ccdbcc17654e80c99", + "https://deno.land/std@0.208.0/assert/assert_object_match.ts": "d8fc2867cfd92eeacf9cea621e10336b666de1874a6767b5ec48988838370b54", + "https://deno.land/std@0.208.0/assert/assert_rejects.ts": "45c59724de2701e3b1f67c391d6c71c392363635aad3f68a1b3408f9efca0057", + "https://deno.land/std@0.208.0/assert/assert_strict_equals.ts": "b1f538a7ea5f8348aeca261d4f9ca603127c665e0f2bbfeb91fa272787c87265", + "https://deno.land/std@0.208.0/assert/assert_string_includes.ts": "b821d39ebf5cb0200a348863c86d8c4c4b398e02012ce74ad15666fc4b631b0c", + "https://deno.land/std@0.208.0/assert/assert_throws.ts": "63784e951475cb7bdfd59878cd25a0931e18f6dc32a6077c454b2cd94f4f4bcd", + "https://deno.land/std@0.208.0/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56", + "https://deno.land/std@0.208.0/assert/equal.ts": "9f1a46d5993966d2596c44e5858eec821859b45f783a5ee2f7a695dfc12d8ece", + "https://deno.land/std@0.208.0/assert/fail.ts": "c36353d7ae6e1f7933d45f8ea51e358c8c4b67d7e7502028598fe1fea062e278", + "https://deno.land/std@0.208.0/assert/mod.ts": "37c49a26aae2b254bbe25723434dc28cd7532e444cf0b481a97c045d110ec085", + "https://deno.land/std@0.208.0/assert/unimplemented.ts": "d56fbeecb1f108331a380f72e3e010a1f161baa6956fd0f7cf3e095ae1a4c75a", + "https://deno.land/std@0.208.0/assert/unreachable.ts": "4600dc0baf7d9c15a7f7d234f00c23bca8f3eba8b140286aaca7aa998cf9a536", + "https://deno.land/std@0.208.0/fmt/colors.ts": "34b3f77432925eb72cf0bfb351616949746768620b8e5ead66da532f93d10ba2" + } +} diff --git a/2023/day01/part1.ts b/2023/day01/part1.ts new file mode 100644 index 0000000..f6ad0b9 --- /dev/null +++ b/2023/day01/part1.ts @@ -0,0 +1,25 @@ +const codePoint = (c: string): number => { + const cp = c.codePointAt(0); + if (!cp) throw new Error(`no code point at 0: ${c}`); + return cp; +}; +const numCodePoints = "01234567890".split("").map(codePoint); + +export const calibrationValue = (line: string) => { + const ints = line + .split("") + .filter((c) => numCodePoints.includes(codePoint(c))); + const numStr = `${ints[0]}${ints[ints.length - 1]}`; + return parseInt(numStr); +}; + +export const answer = (lines: string[]): number => + lines.map(calibrationValue).reduce((sum, x) => sum + x, 0); + +if (import.meta.main) { + const input = await Deno.readFile("input").then((bytes) => + new TextDecoder().decode(bytes), + ); + const lines = input.trim().split("\n"); + console.log(answer(lines)); +} diff --git a/2023/day01/part2.ts b/2023/day01/part2.ts new file mode 100644 index 0000000..fd0fb14 --- /dev/null +++ b/2023/day01/part2.ts @@ -0,0 +1,40 @@ +const intWords = [ + "one", + "two", + "three", + "four", + "five", + "six", + "seven", + "eight", + "nine", +]; +const numRegex = new RegExp(`(\\d|${intWords.join("|")})`, "g"); + +export const calibrationValue = (line: string): number => { + const ints = line.match(numRegex)?.map((iStr) => { + if (iStr.match(/^\d$/)) return iStr; + + const wordIdx = intWords.indexOf(iStr); + if (wordIdx !== -1) return (wordIdx + 1).toString(); + + throw new Error(`no digit for match: ${iStr}`); + }); + + if (!ints) throw new Error(`no ints in line: ${line}`); + + const numStr = `${ints[0]}${ints[ints.length - 1]}`; + console.log(`${line}: ${ints} -- ${numStr}`); + return parseInt(numStr); +}; + +export const answer = (lines: string[]): number => + lines.map(calibrationValue).reduce((sum, x) => sum + x, 0); + +if (import.meta.main) { + const input = await Deno.readFile("input").then((bytes) => + new TextDecoder().decode(bytes), + ); + const lines = input.trim().split("\n"); + console.log(answer(lines)); +} diff --git a/2023/day01/puzzle.md b/2023/day01/puzzle.md new file mode 100644 index 0000000..2d77389 --- /dev/null +++ b/2023/day01/puzzle.md @@ -0,0 +1,60 @@ +\--- Day 1: Trebuchet?! --- +---------- + +Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems. + +You've been doing this long enough to know that to restore snow operations, you need to check all *fifty stars* by December 25th. + +Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants *one star*. Good luck! + +You try to ask why they can't just use a [weather machine](/2015/day/1) ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a [trebuchet](https://en.wikipedia.org/wiki/Trebuchet) ("please hold still, we need to strap you in"). + +As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been *amended* by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document. + +The newly-improved calibration document consists of lines of text; each line originally contained a specific *calibration value* that the Elves now need to recover. On each line, the calibration value can be found by combining the *first digit* and the *last digit* (in that order) to form a single *two-digit number*. + +For example: + +``` +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet + +``` + +In this example, the calibration values of these four lines are `12`, `38`, `15`, and `77`. Adding these together produces `*142*`. + +Consider your entire calibration document. *What is the sum of all of the calibration values?* + +Your puzzle answer was `55123`. + +The first half of this puzzle is complete! It provides one gold star: \* + +\--- Part Two --- +---------- + +Your calculation isn't quite right. It looks like some of the digits are actually *spelled out with letters*: `one`, `two`, `three`, `four`, `five`, `six`, `seven`, `eight`, and `nine` *also* count as valid "digits". + +Equipped with this new information, you now need to find the real first and last digit on each line. For example: + +``` +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen + +``` + +In this example, the calibration values are `29`, `83`, `13`, `24`, `42`, `14`, and `76`. Adding these together produces `*281*`. + +*What is the sum of all of the calibration values?* + +Answer: + +Although it hasn't changed, you can still [get your puzzle input](1/input). + +You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+Part+One+of+%22Trebuchet%3F%21%22+%2D+Day+1+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F1&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. \ No newline at end of file diff --git a/2023/day01/test.ts b/2023/day01/test.ts new file mode 100644 index 0000000..8c632d9 --- /dev/null +++ b/2023/day01/test.ts @@ -0,0 +1,41 @@ +import { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts"; +import * as p1 from "./part1.ts"; +import * as p2 from "./part2.ts"; + +Deno.test("part 1", () => { + const lines = { + "1abc2": 12, + pqr3stu8vwx: 38, + a1b2c3d4e5f: 15, + treb7uchet: 77, + }; + + assertEquals(p1.answer(Object.keys(lines)), 142); + + Object.entries(lines).forEach(([line, expected]) => { + assertEquals(p1.calibrationValue(line), expected); + }); +}); + +Deno.test("part 2", () => { + const lines = { + two1nine: 29, + eightwothree: 83, + abcone2threexyz: 13, + xtwone3four: 24, + "4nineeightseven2": 42, + zoneight234: 14, + "7pqrstsixteen": 76, + abone40ijone2onefds: 11, + }; + + assertEquals(p2.answer(Object.keys(lines)), 292); + + Object.entries(lines).forEach(([line, expected]) => { + assertEquals(p2.calibrationValue(line), expected); + }); +}); + +// Deno.test("frames", () => { +// assertEquals(p2.frames("abc", 2), ["ab", "bc", "c"]); +// }); -- 2.51.2