DEV Community

Cover image for Advent of Code 2023 - December 6th
Rob van der Leek
Rob van der Leek

Posted on

Advent of Code 2023 - December 6th

In this series, I'll share my progress with the 2023 version of Advent of Code.

Check the first post for a short intro to this series.

You can also follow my progress on GitHub.

December 6th

The puzzle of day 6 took me only half an hour. I'm using a brute-force implementation for the second part, which is a bit disappointing but I need the saved time for the second part of yesterday's puzzle.

My pitfall for this puzzle: The second part of the puzzle deserves something better than the current brute-force implementation. Unfortunately, I could spend no more time on it.

Solution here, do not click if you want to solve the puzzle first yourself
#!/usr/bin/env python3
import math
import re

with open('input-small.txt') as infile:
    lines = infile.readlines()

times = [t for t in re.split('\s+', lines[0].split(':')[1].strip())]
distances = [d for d in re.split('\s+', lines[1].split(':')[1].strip())]

ways = []
for index, t in enumerate([int (t) for t in times]):
    way = 0
    for i in range(1, t + 1):
        if i * (t - i) > int(distances[index]):
            way += 1
    ways.append(way)

print(math.prod(ways))

time = int(''.join(times))
distance = int(''.join(distances))

ways = 0
for i in range(1, time + 1):
    if i * (time - i) > distance:
        ways += 1

print(ways)
Enter fullscreen mode Exit fullscreen mode

That's it! See you again tomorrow!

Top comments (0)