23
submitted 1 year ago* (last edited 1 year ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

Day 6: Wait for It


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ

you are viewing a single comment's thread
view the rest of the comments
[-] UlrikHD@programming.dev 3 points 1 year ago* (last edited 1 year ago)

A nice change of pace from the previous puzzles, more maths and less parsing :::spoiler Python

import math
import re


def create_table(filename: str) -> list[tuple[int, int]]:
    with open('day6.txt', 'r', encoding='utf-8') as file:
        times: list[str] = re.findall(r'\d+', file.readline())
        distances: list[str] = re.findall(r'\d+', file.readline())
    table: list[tuple[int, int]] = []
    for t, d in zip(times, distances):
        table.append((int(t), int(d)))
    return table


def get_possible_times_num(table_entry: tuple[int, int]) -> int:
    t, d = table_entry
    l_border: int = math.ceil(0.5 * (t - math.sqrt(t**2 -4 * d)) + 0.0000000000001)  # Add small num to ensure you round up on whole numbers
    r_border: int = math.floor(0.5*(math.sqrt(t**2 - 4 * d) + t) - 0.0000000000001)  # Subtract small num to ensure you round down on whole numbers
    return r_border - l_border + 1


def puzzle1() -> int:
    table: list[tuple[int, int]] = create_table('day6.txt')
    possibilities: int = 1
    for e in table:
        possibilities *= get_possible_times_num(e)
    return possibilities


def create_table_2(filename: str) -> tuple[int, int]:
    with open('day6.txt', 'r', encoding='utf-8') as file:
        t: str = re.search(r'\d+', file.readline().replace(' ', '')).group(0)
        d: str = re.search(r'\d+', file.readline().replace(' ', '')).group(0)
    return int(t), int(d)


def puzzle2() -> int:
    t, d = create_table_2('day6.txt')
    return get_possible_times_num((t, d))


if __name__ == '__main__':
    print(puzzle1())
    print(puzzle2())
this post was submitted on 06 Dec 2023
23 points (96.0% liked)

Advent Of Code

920 readers
98 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 18 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS