From d9239bbb82c210c1d3c80720bbb931ccf783aa5b Mon Sep 17 00:00:00 2001 From: HeNine <> Date: Wed, 6 Dec 2023 09:45:04 +0100 Subject: [PATCH] Day 666 --- day06/data/input100.txt | 2 ++ day06/data/input101.txt | 2 ++ day06/task1.py | 54 +++++++++++++++++++++++++++++++++++++++++ day06/task2.py | 47 +++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 day06/data/input100.txt create mode 100644 day06/data/input101.txt create mode 100644 day06/task1.py create mode 100644 day06/task2.py diff --git a/day06/data/input100.txt b/day06/data/input100.txt new file mode 100644 index 0000000..2fae34a --- /dev/null +++ b/day06/data/input100.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file diff --git a/day06/data/input101.txt b/day06/data/input101.txt new file mode 100644 index 0000000..4885bc8 --- /dev/null +++ b/day06/data/input101.txt @@ -0,0 +1,2 @@ +Time: 59 70 78 78 +Distance: 430 1218 1213 1276 \ No newline at end of file diff --git a/day06/task1.py b/day06/task1.py new file mode 100644 index 0000000..946b749 --- /dev/null +++ b/day06/task1.py @@ -0,0 +1,54 @@ +from math import sqrt, floor, ceil + + +def do_task(input): + lines = input.split("\n") + + times = [int(t) for t in lines[0].split()[1:]] + distances = [int(d) for d in lines[1].split()[1:]] + + prod = 1 + + for i in range(len(times)): + a = -1 + b = 1 + times[i] - 1 + c = -distances[i] + + max_h = (-b - sqrt(b**2 - 4 * a * c)) / (2 * a) + min_h = (-b + sqrt(b**2 - 4 * a * c)) / (2 * a) + 1 + + prod *= ceil(max_h) - floor(min_h) + + return prod + +# This actually works +def do_task_the_dumb_way(input): + lines = input.split("\n") + + times = [int(t) for t in lines[0].split()[1:]] + distances = [int(d) for d in lines[1].split()[1:]] + + prod = 1 + + for i in range(len(times)): + ways = 0 + for h in range(times[i]): + d = 0 + for t in range(times[i] - h): + d += h + if d > distances[i]: + ways += 1 + prod *= ways + + return prod + + +with open("day06/data/input100.txt") as infile: + result = do_task(infile.read()) + print(result) + print(result == 288) + +with open("day06/data/input101.txt") as infile: + result = do_task(infile.read()) + print(result) + print(result == 227850) diff --git a/day06/task2.py b/day06/task2.py new file mode 100644 index 0000000..51b6a84 --- /dev/null +++ b/day06/task2.py @@ -0,0 +1,47 @@ +from math import sqrt, floor, ceil + + +def do_task(input): + lines = input.split("\n") + + time = int("".join(lines[0].split()[1:])) + distance = int("".join(lines[1].split()[1:])) + + a = -1 + b = 1 + time - 1 + c = -distance + + max_h = (-b - sqrt(b**2 - 4 * a * c)) / (2 * a) + min_h = (-b + sqrt(b**2 - 4 * a * c)) / (2 * a) + 1 + + return ceil(max_h) - floor(min_h) + +# Kind of slow, but workable +def do_task_the_dumb_way(input): + lines = input.split("\n") + + time = int("".join(lines[0].split()[1:])) + distance = int("".join(lines[1].split()[1:])) + + ways = 0 + for h in range(time): + d = (time - h) * h + + # This is how dumb you have to make it to make it run slow + # for t in range(time - h): + # d+=h + if d > distance: + ways += 1 + + return ways + + +with open("day06/data/input100.txt") as infile: + result = do_task(infile.read()) + print(result) + print(result == 71503) + +with open("day06/data/input101.txt") as infile: + result = do_task(infile.read()) + print(result) + print(result == 42948149)