Day 06
This commit is contained in:
2
day06/input.txt
Normal file
2
day06/input.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Time: 42 68 69 85
|
||||
Distance: 284 1005 1122 1341
|
||||
21
day06/part1.py
Normal file
21
day06/part1.py
Normal file
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
import math
|
||||
import re
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
times = []
|
||||
records = []
|
||||
|
||||
for line in lines:
|
||||
if line.startswith("Time"):
|
||||
times = tuple(map(int, re.findall("\d+", line)))
|
||||
else:
|
||||
records = tuple(map(int, re.findall("\d+", line)))
|
||||
|
||||
result = 1
|
||||
for t, r in zip(times, records):
|
||||
pmin = (t-(t**2-4*r)**0.5)/2
|
||||
pmax = (t+(t**2-4*r)**0.5)/2
|
||||
result *= math.floor(pmax) - math.ceil(pmin) + 1
|
||||
|
||||
print(result)
|
||||
17
day06/part2.py
Normal file
17
day06/part2.py
Normal file
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python3
|
||||
import math
|
||||
import re
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
t = 0
|
||||
r = 0
|
||||
|
||||
for line in lines:
|
||||
if line.startswith("Time"):
|
||||
t = int(re.sub("[^\d]", "", line))
|
||||
else:
|
||||
r = int(re.sub("[^\d]", "", line))
|
||||
|
||||
pmin = (t-(t**2-4*r)**0.5)/2
|
||||
pmax = (t+(t**2-4*r)**0.5)/2
|
||||
print(math.floor(pmax) - math.ceil(pmin) + 1)
|
||||
Reference in New Issue
Block a user