From 7efacbda5b875b5580e34e4d46d61c7537c60638 Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Thu, 7 Dec 2023 17:00:55 +0100 Subject: [PATCH] Day 06 --- day06/input.txt | 2 ++ day06/part1.py | 21 +++++++++++++++++++++ day06/part2.py | 17 +++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 day06/input.txt create mode 100644 day06/part1.py create mode 100644 day06/part2.py diff --git a/day06/input.txt b/day06/input.txt new file mode 100644 index 0000000..57f1bf3 --- /dev/null +++ b/day06/input.txt @@ -0,0 +1,2 @@ +Time: 42 68 69 85 +Distance: 284 1005 1122 1341 diff --git a/day06/part1.py b/day06/part1.py new file mode 100644 index 0000000..84ce5c6 --- /dev/null +++ b/day06/part1.py @@ -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) diff --git a/day06/part2.py b/day06/part2.py new file mode 100644 index 0000000..366d34e --- /dev/null +++ b/day06/part2.py @@ -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)