this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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!

+222
+3
2023/README.md
··· 1 + # Advent of Code, 2023 2 + 3 +
+6
2023/base/deno.json
··· 1 + { 2 + "tasks": { 3 + "p1": "deno run -A part1.ts", 4 + "p2": "deno run -A part2.ts" 5 + } 6 + }
+4
2023/base/part1.ts
··· 1 + const input = await Deno.readFile("input").then((bytes) => 2 + new TextDecoder().decode(bytes), 3 + ); 4 + const lines = input.trim().split("\n");
+6
2023/day01/deno.json
··· 1 + { 2 + "tasks": { 3 + "p1": "deno run -A part1.ts", 4 + "p2": "deno run -A part2.ts" 5 + } 6 + }
+37
2023/day01/deno.lock
··· 1 + { 2 + "version": "3", 3 + "remote": { 4 + "https://deno.land/std@0.208.0/assert/_constants.ts": "8a9da298c26750b28b326b297316cdde860bc237533b07e1337c021379e6b2a9", 5 + "https://deno.land/std@0.208.0/assert/_diff.ts": "58e1461cc61d8eb1eacbf2a010932bf6a05b79344b02ca38095f9b805795dc48", 6 + "https://deno.land/std@0.208.0/assert/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7", 7 + "https://deno.land/std@0.208.0/assert/assert.ts": "9a97dad6d98c238938e7540736b826440ad8c1c1e54430ca4c4e623e585607ee", 8 + "https://deno.land/std@0.208.0/assert/assert_almost_equals.ts": "e15ca1f34d0d5e0afae63b3f5d975cbd18335a132e42b0c747d282f62ad2cd6c", 9 + "https://deno.land/std@0.208.0/assert/assert_array_includes.ts": "6856d7f2c3544bc6e62fb4646dfefa3d1df5ff14744d1bca19f0cbaf3b0d66c9", 10 + "https://deno.land/std@0.208.0/assert/assert_equals.ts": "d8ec8a22447fbaf2fc9d7c3ed2e66790fdb74beae3e482855d75782218d68227", 11 + "https://deno.land/std@0.208.0/assert/assert_exists.ts": "407cb6b9fb23a835cd8d5ad804e2e2edbbbf3870e322d53f79e1c7a512e2efd7", 12 + "https://deno.land/std@0.208.0/assert/assert_false.ts": "0ccbcaae910f52c857192ff16ea08bda40fdc79de80846c206bfc061e8c851c6", 13 + "https://deno.land/std@0.208.0/assert/assert_greater.ts": "ae2158a2d19313bf675bf7251d31c6dc52973edb12ac64ac8fc7064152af3e63", 14 + "https://deno.land/std@0.208.0/assert/assert_greater_or_equal.ts": "1439da5ebbe20855446cac50097ac78b9742abe8e9a43e7de1ce1426d556e89c", 15 + "https://deno.land/std@0.208.0/assert/assert_instance_of.ts": "3aedb3d8186e120812d2b3a5dea66a6e42bf8c57a8bd927645770bd21eea554c", 16 + "https://deno.land/std@0.208.0/assert/assert_is_error.ts": "c21113094a51a296ffaf036767d616a78a2ae5f9f7bbd464cd0197476498b94b", 17 + "https://deno.land/std@0.208.0/assert/assert_less.ts": "aec695db57db42ec3e2b62e97e1e93db0063f5a6ec133326cc290ff4b71b47e4", 18 + "https://deno.land/std@0.208.0/assert/assert_less_or_equal.ts": "5fa8b6a3ffa20fd0a05032fe7257bf985d207b85685fdbcd23651b70f928c848", 19 + "https://deno.land/std@0.208.0/assert/assert_match.ts": "c4083f80600bc190309903c95e397a7c9257ff8b5ae5c7ef91e834704e672e9b", 20 + "https://deno.land/std@0.208.0/assert/assert_not_equals.ts": "9f1acab95bd1f5fc9a1b17b8027d894509a745d91bac1718fdab51dc76831754", 21 + "https://deno.land/std@0.208.0/assert/assert_not_instance_of.ts": "0c14d3dfd9ab7a5276ed8ed0b18c703d79a3d106102077ec437bfe7ed912bd22", 22 + "https://deno.land/std@0.208.0/assert/assert_not_match.ts": "3796a5b0c57a1ce6c1c57883dd4286be13a26f715ea662318ab43a8491a13ab0", 23 + "https://deno.land/std@0.208.0/assert/assert_not_strict_equals.ts": "4cdef83df17488df555c8aac1f7f5ec2b84ad161b6d0645ccdbcc17654e80c99", 24 + "https://deno.land/std@0.208.0/assert/assert_object_match.ts": "d8fc2867cfd92eeacf9cea621e10336b666de1874a6767b5ec48988838370b54", 25 + "https://deno.land/std@0.208.0/assert/assert_rejects.ts": "45c59724de2701e3b1f67c391d6c71c392363635aad3f68a1b3408f9efca0057", 26 + "https://deno.land/std@0.208.0/assert/assert_strict_equals.ts": "b1f538a7ea5f8348aeca261d4f9ca603127c665e0f2bbfeb91fa272787c87265", 27 + "https://deno.land/std@0.208.0/assert/assert_string_includes.ts": "b821d39ebf5cb0200a348863c86d8c4c4b398e02012ce74ad15666fc4b631b0c", 28 + "https://deno.land/std@0.208.0/assert/assert_throws.ts": "63784e951475cb7bdfd59878cd25a0931e18f6dc32a6077c454b2cd94f4f4bcd", 29 + "https://deno.land/std@0.208.0/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56", 30 + "https://deno.land/std@0.208.0/assert/equal.ts": "9f1a46d5993966d2596c44e5858eec821859b45f783a5ee2f7a695dfc12d8ece", 31 + "https://deno.land/std@0.208.0/assert/fail.ts": "c36353d7ae6e1f7933d45f8ea51e358c8c4b67d7e7502028598fe1fea062e278", 32 + "https://deno.land/std@0.208.0/assert/mod.ts": "37c49a26aae2b254bbe25723434dc28cd7532e444cf0b481a97c045d110ec085", 33 + "https://deno.land/std@0.208.0/assert/unimplemented.ts": "d56fbeecb1f108331a380f72e3e010a1f161baa6956fd0f7cf3e095ae1a4c75a", 34 + "https://deno.land/std@0.208.0/assert/unreachable.ts": "4600dc0baf7d9c15a7f7d234f00c23bca8f3eba8b140286aaca7aa998cf9a536", 35 + "https://deno.land/std@0.208.0/fmt/colors.ts": "34b3f77432925eb72cf0bfb351616949746768620b8e5ead66da532f93d10ba2" 36 + } 37 + }
+25
2023/day01/part1.ts
··· 1 + const codePoint = (c: string): number => { 2 + const cp = c.codePointAt(0); 3 + if (!cp) throw new Error(`no code point at 0: ${c}`); 4 + return cp; 5 + }; 6 + const numCodePoints = "01234567890".split("").map(codePoint); 7 + 8 + export const calibrationValue = (line: string) => { 9 + const ints = line 10 + .split("") 11 + .filter((c) => numCodePoints.includes(codePoint(c))); 12 + const numStr = `${ints[0]}${ints[ints.length - 1]}`; 13 + return parseInt(numStr); 14 + }; 15 + 16 + export const answer = (lines: string[]): number => 17 + lines.map(calibrationValue).reduce((sum, x) => sum + x, 0); 18 + 19 + if (import.meta.main) { 20 + const input = await Deno.readFile("input").then((bytes) => 21 + new TextDecoder().decode(bytes), 22 + ); 23 + const lines = input.trim().split("\n"); 24 + console.log(answer(lines)); 25 + }
+40
2023/day01/part2.ts
··· 1 + const intWords = [ 2 + "one", 3 + "two", 4 + "three", 5 + "four", 6 + "five", 7 + "six", 8 + "seven", 9 + "eight", 10 + "nine", 11 + ]; 12 + const numRegex = new RegExp(`(\\d|${intWords.join("|")})`, "g"); 13 + 14 + export const calibrationValue = (line: string): number => { 15 + const ints = line.match(numRegex)?.map((iStr) => { 16 + if (iStr.match(/^\d$/)) return iStr; 17 + 18 + const wordIdx = intWords.indexOf(iStr); 19 + if (wordIdx !== -1) return (wordIdx + 1).toString(); 20 + 21 + throw new Error(`no digit for match: ${iStr}`); 22 + }); 23 + 24 + if (!ints) throw new Error(`no ints in line: ${line}`); 25 + 26 + const numStr = `${ints[0]}${ints[ints.length - 1]}`; 27 + console.log(`${line}: ${ints} -- ${numStr}`); 28 + return parseInt(numStr); 29 + }; 30 + 31 + export const answer = (lines: string[]): number => 32 + lines.map(calibrationValue).reduce((sum, x) => sum + x, 0); 33 + 34 + if (import.meta.main) { 35 + const input = await Deno.readFile("input").then((bytes) => 36 + new TextDecoder().decode(bytes), 37 + ); 38 + const lines = input.trim().split("\n"); 39 + console.log(answer(lines)); 40 + }
+60
2023/day01/puzzle.md
··· 1 + \--- Day 1: Trebuchet?! --- 2 + ---------- 3 + 4 + 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. 5 + 6 + You've been doing this long enough to know that to restore snow operations, you need to check all *fifty stars* by December 25th. 7 + 8 + 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! 9 + 10 + 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"). 11 + 12 + 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. 13 + 14 + 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*. 15 + 16 + For example: 17 + 18 + ``` 19 + 1abc2 20 + pqr3stu8vwx 21 + a1b2c3d4e5f 22 + treb7uchet 23 + 24 + ``` 25 + 26 + In this example, the calibration values of these four lines are `12`, `38`, `15`, and `77`. Adding these together produces `*142*`. 27 + 28 + Consider your entire calibration document. *What is the sum of all of the calibration values?* 29 + 30 + Your puzzle answer was `55123`. 31 + 32 + The first half of this puzzle is complete! It provides one gold star: \* 33 + 34 + \--- Part Two --- 35 + ---------- 36 + 37 + 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". 38 + 39 + Equipped with this new information, you now need to find the real first and last digit on each line. For example: 40 + 41 + ``` 42 + two1nine 43 + eightwothree 44 + abcone2threexyz 45 + xtwone3four 46 + 4nineeightseven2 47 + zoneight234 48 + 7pqrstsixteen 49 + 50 + ``` 51 + 52 + In this example, the calibration values are `29`, `83`, `13`, `24`, `42`, `14`, and `76`. Adding these together produces `*281*`. 53 + 54 + *What is the sum of all of the calibration values?* 55 + 56 + Answer: 57 + 58 + Although it hasn't changed, you can still [get your puzzle input](1/input). 59 + 60 + 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.
+41
2023/day01/test.ts
··· 1 + import { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts"; 2 + import * as p1 from "./part1.ts"; 3 + import * as p2 from "./part2.ts"; 4 + 5 + Deno.test("part 1", () => { 6 + const lines = { 7 + "1abc2": 12, 8 + pqr3stu8vwx: 38, 9 + a1b2c3d4e5f: 15, 10 + treb7uchet: 77, 11 + }; 12 + 13 + assertEquals(p1.answer(Object.keys(lines)), 142); 14 + 15 + Object.entries(lines).forEach(([line, expected]) => { 16 + assertEquals(p1.calibrationValue(line), expected); 17 + }); 18 + }); 19 + 20 + Deno.test("part 2", () => { 21 + const lines = { 22 + two1nine: 29, 23 + eightwothree: 83, 24 + abcone2threexyz: 13, 25 + xtwone3four: 24, 26 + "4nineeightseven2": 42, 27 + zoneight234: 14, 28 + "7pqrstsixteen": 76, 29 + abone40ijone2onefds: 11, 30 + }; 31 + 32 + assertEquals(p2.answer(Object.keys(lines)), 292); 33 + 34 + Object.entries(lines).forEach(([line, expected]) => { 35 + assertEquals(p2.calibrationValue(line), expected); 36 + }); 37 + }); 38 + 39 + // Deno.test("frames", () => { 40 + // assertEquals(p2.frames("abc", 2), ["ab", "bc", "c"]); 41 + // });