diff --git a/2023/day06/part2.ts b/2023/day06/part2.ts index 5658bc9..8bed5e6 100644 --- a/2023/day06/part2.ts +++ b/2023/day06/part2.ts @@ -1,3 +1,5 @@ +import { assert } from "https://deno.land/std@0.208.0/assert/assert.ts"; + if (import.meta.main) { const input = ( await Deno.readFile("input").then((bytes) => @@ -8,6 +10,24 @@ if (import.meta.main) { } export function answer(input: string): number { - console.log(input); - return 42; + const [raceDuration, record] = input + .split("\n") + .map((l) => parseInt(l.split(/\s+/).slice(1).join(""))); + const [min, max] = minMaxHoldsToWin(raceDuration, record); + return max - min + 1; +} + +function minMaxHoldsToWin( + raceDuration: number, + record: number, +): [number, number] { + let min = 0; + let max = 0; + for (let hold = 0; hold < raceDuration; hold++) { + const distance = hold * (raceDuration - hold); + if (min === 0 && distance > record) min = hold; + if (min !== 0 && distance > record) max = hold; + if (min !== 0 && max !== 0 && distance < record) break; + } + return [min, max]; } diff --git a/2023/day06/puzzle.md b/2023/day06/puzzle.md index c24d620..98159fe 100644 --- a/2023/day06/puzzle.md +++ b/2023/day06/puzzle.md @@ -48,8 +48,37 @@ To see how much margin of error you have, determine the *number of ways you can Determine the number of ways you could beat the record in each race. *What do you get if you multiply these numbers together?* -To begin, [get your puzzle input](6/input). +Your puzzle answer was `114400`. + +The first half of this puzzle is complete! It provides one gold star: \* + +\--- Part Two --- +---------- + +As the race is about to start, you realize the piece of paper with race times and record distances you got earlier actually just has very bad [kerning](https://en.wikipedia.org/wiki/Kerning). There's really *only one race* - ignore the spaces between the numbers on each line. + +So, the example from before: + +``` +Time: 7 15 30 +Distance: 9 40 200 + +``` + +...now instead means this: + +``` +Time: 71530 +Distance: 940200 + +``` + +Now, you have to figure out how many ways there are to win this single race. In this example, the race lasts for *`71530` milliseconds* and the record distance you need to beat is *`940200` millimeters*. You could hold the button anywhere from `14` to `71516` milliseconds and beat the record, a total of `*71503*` ways! + +*How many ways can you beat the record in this one much longer race?* Answer: -You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Wait+For+It%22+%2D+Day+6+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F6&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. \ No newline at end of file +Although it hasn't changed, you can still [get your puzzle input](6/input). + +You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+Part+One+of+%22Wait+For+It%22+%2D+Day+6+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F6&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. \ No newline at end of file diff --git a/2023/day06/test.ts b/2023/day06/test.ts index 8454d0a..7f2cc9f 100644 --- a/2023/day06/test.ts +++ b/2023/day06/test.ts @@ -10,6 +10,8 @@ Deno.test("part1", () => { }); Deno.test("part2", () => { - const examples = ["abc", "def"].join("\n"); - assertEquals(p2.answer(examples), 42); + const examples = ["Time: 7 15 30", "Distance: 9 40 200"].join( + "\n", + ); + assertEquals(p2.answer(examples), 71503); });