day 6
This commit is contained in:
parent
1443834f4e
commit
37165b60fd
3 changed files with 62 additions and 0 deletions
2
23/day6/example.txt
Normal file
2
23/day6/example.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Time: 7 15 30
|
||||||
|
Distance: 9 40 200
|
2
23/day6/input.txt
Normal file
2
23/day6/input.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Time: 46 68 98 66
|
||||||
|
Distance: 358 1054 1807 1080
|
58
23/day6/main.py
Normal file
58
23/day6/main.py
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
import tqdm
|
||||||
|
import re
|
||||||
|
|
||||||
|
input = "input.txt"
|
||||||
|
|
||||||
|
times = []
|
||||||
|
distances = []
|
||||||
|
|
||||||
|
with open(input, "r") as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if line.startswith("Time"):
|
||||||
|
times = map(int, re.split("\\s+", line)[1:])
|
||||||
|
else:
|
||||||
|
distances = map(int, re.split("\\s+", line)[1:])
|
||||||
|
|
||||||
|
races = []
|
||||||
|
|
||||||
|
for time, distance in zip(times, distances):
|
||||||
|
races.append((time, distance))
|
||||||
|
|
||||||
|
part1 = 1
|
||||||
|
for time, record_distance in races:
|
||||||
|
record_beaten_times = 0
|
||||||
|
for time_held in range(time):
|
||||||
|
time_left = time - time_held
|
||||||
|
speed = time_held
|
||||||
|
|
||||||
|
distance = speed * time_left
|
||||||
|
|
||||||
|
if distance > record_distance:
|
||||||
|
record_beaten_times += 1
|
||||||
|
part1 *= record_beaten_times
|
||||||
|
|
||||||
|
print("part1", part1)
|
||||||
|
|
||||||
|
time = 0
|
||||||
|
record_distance = 0
|
||||||
|
|
||||||
|
with open(input, "r") as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if line.startswith("Time"):
|
||||||
|
time = int("".join(re.split("\\s+", line)[1:]))
|
||||||
|
else:
|
||||||
|
record_distance = int("".join(re.split("\\s+", line)[1:]))
|
||||||
|
|
||||||
|
print(time)
|
||||||
|
part2 = 0
|
||||||
|
for time_held in tqdm.tqdm(range(time)):
|
||||||
|
time_left = time - time_held
|
||||||
|
speed = time_held
|
||||||
|
|
||||||
|
distance = speed * time_left
|
||||||
|
|
||||||
|
if distance > record_distance:
|
||||||
|
part2 += 1
|
||||||
|
print("part2", part2)
|
Loading…
Reference in a new issue