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

Advent Of Code

736 readers
1 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 11 months 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

That was so much better than yesterday. Went with algebra but looks like brute force would have worked.

python

import re
import argparse
import math

# i feel doing this with equations is probably the
# "fast" way.

# we can re-arange stuff so we only need to find the point
# the line crosses the 0 line

# distance is speed * (time less time holding the button (which is equal to speed)):
# -> d = v * (t - v)
# -> v^2 -vt +d = 0

# -> y=0 @ v = t +- sqrt( t^2 - 4d) / 2

def get_cross_points(time:int, distance:int) -> list | None:
    pre_root = time**2 - (4 * distance)
    if pre_root < 0:
        # no solutions
        return None
    if pre_root == 0:
        # one solution
        return [(float(time)/2)]
    sqroot = math.sqrt(pre_root)
    v1 = (float(time) + sqroot)/2
    v2 = (float(time) - sqroot)/2
    return [v1,v2]

def float_pair_to_int_pair(a:float,b:float):
    # if floats are equal to int value, then we need to add one to value
    # as we are looking for values above 0 point
    
    if a > b:
        # lower a and up b
        if a == int(a):
            a -= 1
        if b == int(b):
            b += 1

        return [math.floor(a),math.ceil(b)]
    if a < b:
        if a == int(a):
            a += 1
        if b == int(b):
            b -= 1
        return [math.floor(b),math.ceil(a)]

def main(line_list: list):
    time_section,distance_section = line_list
    if (args.part == 1):
        time_list = filter(None , re.split(' +',time_section.split(':')[1]))
        distance_list = filter(None ,  re.split(' +',distance_section.split(':')[1]))
        games = list(zip(time_list,distance_list))
    if (args.part == 2):
        games = [ [time_section.replace(' ','').split(':')[1],distance_section.replace(' ','').split(':')[1]] ]
    print (games)
    total = 1
    for t,d in games:
        cross = get_cross_points(int(t),int(d))
        cross_int = float_pair_to_int_pair(*cross)
        print (cross_int)
        total *= cross_int[0] - cross_int[1] +1
    
    print(f"total: {total}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="day 6 solver")
    parser.add_argument("-input",type=str)
    parser.add_argument("-part",type=int)
    args = parser.parse_args()
    filename = args.input
    if filename == None:
        parser.print_help()
        exit(1)
    file = open(filename,'r')
    main([line.rstrip('\n') for line in file.readlines()])
    file.close()