import 2023 aoc

This commit is contained in:
2024-12-03 15:29:02 +01:00
parent a454b7e15a
commit cacfbd5bb5
29 changed files with 4450 additions and 0 deletions

40
2023/day6/day6.py Normal file
View File

@ -0,0 +1,40 @@
def parse_part1(path):
with open(path) as f:
time, distance = f.readlines()
time = [int(x) for x in time.split()[1:]]
distance = [int(x) for x in distance.split()[1:]]
return zip(time, distance)
def calculate_wins(data):
total = 1
for time, record in data:
ways = 0
for n in range(0, time+1):
speed = n
distance = (time-n) * speed
if distance > record:
ways += 1
total *= ways
return total
def parse_part2(path):
with open(path) as f:
time, distance = f.readlines()
time = time.split(":")[1].replace(" ", "").rstrip()
distance = distance.split(":")[1].replace(" ", "").rstrip()
return int(time), int(distance)
if __name__ == "__main__":
assert calculate_wins(zip(*[[7, 15, 30], [9, 40, 200]])) == 288 # part 1 example
assert calculate_wins([[71530, 940200]]) == 71503 # part 2 example
import sys
if len(sys.argv) == 2:
data = parse_part1(sys.argv[1])
res = calculate_wins(data)
print(f"Part 1, res={res}")
data = parse_part2(sys.argv[1])
res = calculate_wins([data])
print(f"Part 2, res={res}")

2
2023/day6/input.txt Normal file
View File

@ -0,0 +1,2 @@
Time: 61 67 75 71
Distance: 430 1036 1307 1150