Browse Source

Day 6

master
Noëlle Anthony 4 months ago
parent
commit
87fec6290c
3 changed files with 87 additions and 0 deletions
  1. 45
    0
      day06-1.py
  2. 40
    0
      day06-2.py
  3. 2
    0
      day06.input

+ 45
- 0
day06-1.py View File

@@ -0,0 +1,45 @@
from functools import reduce

from helpers import Helper

helper = Helper(debug=True)
debug = helper.debug
load_input = helper.load_input

def main():
lines = load_input(6)

races = []

times = []
distances = []
for line in lines:
header, values_str = line.split(":")
for value in values_str.split():
if header == "Time":
times.append(int(value))
else:
distances.append(int(value))

for item in zip(times, distances):
races.append({"Time": item[0], "Distance": item[1]})
# debug(races)

ways_to_win = []
for i, race in enumerate(races):
win_ways = 0
for held_time in range(race["Time"]):
run_time = race["Time"] - held_time
run_distance = run_time * held_time
if run_distance > race["Distance"]:
debug(f"I can win race {i} with time {held_time}ms (it'll run {run_time}ms at {held_time}mm/ms for {run_distance}mm total.)")
win_ways += 1
ways_to_win.append(win_ways)

total_ways_to_win = reduce(lambda x,y: x*y, ways_to_win)

print(f"Total ways to win all races: {total_ways_to_win} ({'*'.join([str(i) for i in ways_to_win])})")

if __name__ == "__main__":
main()

+ 40
- 0
day06-2.py View File

@@ -0,0 +1,40 @@
from functools import reduce

from helpers import Helper

helper = Helper(debug=True)
debug = helper.debug
load_input = helper.load_input

def main():
lines = load_input(6)

race = {}

times = []
distances = []
for line in lines:
header, values_str = line.split(":")
for value in values_str.split():
if header == "Time":
times.append(value)
else:
distances.append(value)

race["Time"] = int("".join(times))
race["Distance"] = int("".join(distances))
debug(race)

win_ways = 0
for held_time in range(race["Time"]):
run_time = race["Time"] - held_time
run_distance = run_time * held_time
if run_distance > race["Distance"]:
# debug(f"I can win the race with time {held_time}ms (it'll run {run_time}ms at {held_time}mm/ms for {run_distance}mm total.)")
win_ways += 1

print(f"Total ways to win the big race: {win_ways}")

if __name__ == "__main__":
main()

+ 2
- 0
day06.input View File

@@ -0,0 +1,2 @@
Time: 59 79 65 75
Distance: 597 1234 1032 1328

Loading…
Cancel
Save