Skip to content

Latest commit

 

History

History
69 lines (50 loc) · 2.05 KB

6.md

File metadata and controls

69 lines (50 loc) · 2.05 KB

Day 6

I felt this was pretty quick. Most of the slowness came from thinking it would be harder than this.

Part 1 Part 2 Total
Time 5:05 3:41 8:46

Part 1

A simple algorithm for computing the final score, then check every one. I know there's a better way since the algorithm is pretty simple. Maybe I'll do a Part Three for fun.

from helpers.datagetter import aocd_data_in

din, aocd_submit = aocd_data_in(split=True, numbers=True)
ans = 1


def compute_score(hold_time, total_time):
    return (total_time-hold_time) * hold_time


for i in range(len(din[0])):
    print(din[0][i], din[1][i])
    wins = []
    for j in range(din[0][i]):
        if compute_score(j, din[0][i]) > din[1][i]:
            wins.append(j)
    ans *= len(wins)

aocd_submit(ans)

Part 2

Was worried for a minute that the input would be too big to search bruteforce, but it only took about 13 seconds. Took too long to realize that and just make the simple changes.

from helpers.datagetter import aocd_data_in

din, aocd_submit = aocd_data_in(split=True, numbers=False)
din[0] = int(din[0].split(":")[1].replace(" ", ""))
din[1] = int(din[1].split(":")[1].replace(" ", ""))
ans = 1


def compute_score(hold_time, total_time):
    return (total_time-hold_time) * hold_time


wins = []
for j in range(din[0]):
    if compute_score(j, din[0]) > din[1]:
        wins.append(j)
ans *= len(wins)

aocd_submit(ans)

Part 3 (for fun, doing it the smarter way)

Compute the first positive intercept of the function and the best score. The polynomial is symmetric between 0 and Time, so that's all you need to find the other intercept. Compute the difference between them, and add one to include both endpoints.

from helpers.datagetter import aocd_data_in
import math

din, aocd_submit = aocd_data_in(split=True, numbers=False)
din = [int(i.split(":")[1].replace(" ", "")) for i in din]

intercept = math.ceil((-din[0] + math.sqrt(din[0] ** 2 - (4 * din[1]))) / -2)
ans = din[0] - 2 * intercept + 1

aocd_submit(ans)