this post was submitted on 06 Dec 2023
23 points (96.0% liked)

Advent Of Code

770 readers
75 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 2023

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 19 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
 

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
[โ€“] pnutzh4x0r@lemmy.ndlug.org 2 points 11 months ago

Language: Python

Part 1

Not much to say... this was pretty straightforward.

def race(charge_time: int, total_time: int) -> int:
    return charge_time * (total_time - charge_time)

def main(stream=sys.stdin) -> None:
    times     = [int(t) for t in stream.readline().split(':')[-1].split()]
    distances = [int(d) for d in stream.readline().split(':')[-1].split()]
    product   = 1

    for time, distance in zip(times, distances):
        ways     = [c for c in range(1, time + 1) if race(c, time) > distance]
        product *= len(ways)

    print(product)

Part 2

Probably could have done some math, but didn't need to :]

def race(charge_time: int, total_time: int):
    return charge_time * (total_time - charge_time)

def main(stream=sys.stdin) -> None:
    time     = int(''.join(stream.readline().split(':')[-1].split()))
    distance = int(''.join(stream.readline().split(':')[-1].split()))
    ways     = [c for c in range(1, time + 1) if race(c, time) > distance]
    print(len(ways))

GitHub Repo