Py lion solve for day 6
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
|
Part 1: `1083852`
|
||||||
|
|
||||||
|
Part 2: `23501589`
|
||||||
|
|
||||||
[--- Day 6: Wait For It ---
|
[--- 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?*
|
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?*
|
||||||
|
|
||||||
|
|
||||||
]
|
]
|
||||||
@@ -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)
|
||||||
@@ -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)
|
||||||
Reference in New Issue
Block a user