From b9fc2b4ab7e77b1e0afb3216d07d8ea8aad87c99 Mon Sep 17 00:00:00 2001 From: rain Date: Fri, 8 Dec 2023 13:45:46 -0600 Subject: [PATCH] Py lion solve for day 6 --- 2023/day06/README.md | 36 +++++++++++++++++++++++++++++++++ 2023/day06/solutions/python1.py | 18 +++++++++++++++++ 2023/day06/solutions/python2.py | 18 +++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 2023/day06/solutions/python1.py create mode 100644 2023/day06/solutions/python2.py diff --git a/2023/day06/README.md b/2023/day06/README.md index 16b1e2a..c287f8f 100644 --- a/2023/day06/README.md +++ b/2023/day06/README.md @@ -1,3 +1,7 @@ +Part 1: `1083852` + +Part 2: `23501589` + [--- Day 6: Wait For It --- -------------------------- @@ -65,4 +69,36 @@ 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?* +, --- 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?* + + ] \ No newline at end of file diff --git a/2023/day06/solutions/python1.py b/2023/day06/solutions/python1.py new file mode 100644 index 0000000..7667480 --- /dev/null +++ b/2023/day06/solutions/python1.py @@ -0,0 +1,18 @@ +import math + +with open("../input.txt", "r") as fp: + input = fp.read().strip().split("\n") + +races = list(zip( + [int(x) for x in input[0].split()[1:]], + [int(x) for x in input[-1].split()[1:]] +)) + +product = 1 +for time, record in races: + record += 1 + min = math.ceil((time - math.sqrt(time*time - 4 * record))/2) + max = math.floor((time + math.sqrt(time*time - 4 * record))/2) + + product *= (max-min+1) +print(product) \ No newline at end of file diff --git a/2023/day06/solutions/python2.py b/2023/day06/solutions/python2.py new file mode 100644 index 0000000..6ea629b --- /dev/null +++ b/2023/day06/solutions/python2.py @@ -0,0 +1,18 @@ +import math + +with open("../input.txt", "r") as fp: + input = fp.read().strip().split("\n") + +race = ( + int(''.join(input[0].split()[1:])), + int(''.join(input[-1].split()[1:])), +) + +time = race[0] +record = race[-1]+1 + +min = math.ceil((time - math.sqrt(time*time - 4 * record))/2) +max = math.floor((time + math.sqrt(time*time - 4 * record))/2) +product = (max-min+1) + +print(product) \ No newline at end of file