-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.py
32 lines (27 loc) · 1.04 KB
/
day6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#Advent of Code 2023 Day 6
from functools import reduce
import operator
def day6_1(f):
time, duration = f.read().split("\n")
time = time.split(":")[1]
time = [int(x) for x in time.split()]
duration = duration.split(":")[1]
duration = [int(x) for x in duration.split()]
wins = []
for r, _ in enumerate(time):
wins += [(len([d for d in calculate_distance(time[r]) if d > duration[r]]))]
return reduce(operator.mul, wins, 1)
def day6_2(f):
f.seek(0)
time, duration = f.read().split("\n")
time = time.split(":")[1]
time = int(time.replace(" ", ""))
duration = duration.split(":")[1]
duration = int(duration.replace(" ", ""))
#calculate with brute force. It could be the resolution of a quadratic equation t*i-i^2 -duration = 0, find the integers in between zeros
return len([d for d in calculate_distance(time) if d > duration])
def calculate_distance(t):
return [(t-i)*i for i in range(0, t)]
f = open("resources\day6.txt", "r")
print(f"Day 6 P1: {day6_1(f)} P2: {day6_2(f)}")
f.close()