Compare commits

...

2 commits

Author SHA1 Message Date
71d885376d Add days 7 and 8 2023-12-25 12:20:34 +01:00
f98cac8651 Clean up day 6 2023-12-25 12:20:24 +01:00
3 changed files with 109 additions and 12 deletions

View file

@ -2,21 +2,25 @@ import fileinput
from math import ceil, floor, sqrt
def race(time, dist):
x1 = (time - sqrt(time * time - 4 * dist)) / 2
x2 = (time + sqrt(time * time - 4 * dist)) / 2
discr = sqrt(time * time - 4 * dist)
x1 = (time - discr) / 2
x2 = (time + discr) / 2
x1, x2 = floor(x1) + 1, ceil(x2) - 1
return x2 - x1 + 1
input = list(fileinput.input())
times = [int(x) for x in input[0][11:].split()]
distances = [int(x) for x in input[1][11:].split()]
def main():
input = list(fileinput.input())
times = [int(x) for x in input[0][11:].split()]
distances = [int(x) for x in input[1][11:].split()]
part1 = 1
for time, dist in zip(times, distances):
part1 *= race(time, dist)
part1 = 1
for time, dist in zip(times, distances):
part1 *= race(time, dist)
time = int(input[0][11:].replace(" ", ""))
dist = int(input[1][11:].replace(" ", ""))
part2 = race(time, dist)
time = int(input[0][11:].replace(" ", ""))
dist = int(input[1][11:].replace(" ", ""))
part2 = race(time, dist)
print(part1, part2)
print(part1, part2)
main()

59
day07.py Normal file
View file

@ -0,0 +1,59 @@
import fileinput
from collections import Counter
STRENGTHS = {
"A": 1,
"K": 2,
"Q": 3,
"T": 5,
"9": 6,
"8": 7,
"7": 8,
"6": 9,
"5": 10,
"4": 11,
"3": 12,
"2": 13,
}
INPUT = []
for line in fileinput.input():
(hand, bid) = line.split()
counter = Counter(hand)
INPUT.append((hand, int(bid), counter["J"], counter.most_common() + [("", 0)]))
def solve(part):
STRENGTHS["J"] = 14 if part == 2 else 4
hands = []
for (hand, bid, jokers, mcs) in INPUT:
if part == 2:
mcs = [(c, i) for (c, i) in mcs if c != "J"]
else:
jokers = 0
mc = mcs[0][1] + jokers
if mc == 5:
strength = 1
elif mc == 4:
strength = 2
elif mc == 3 and mcs[1][1] == 2:
strength = 3
elif mc == 3:
strength = 4
elif mc == 2 and mcs[1][1] == 2:
strength = 5
elif mc == 2:
strength = 6
else:
strength = 7
strengths = [STRENGTHS[c] for c in hand]
hands.append(([strength] + strengths, bid))
hands.sort(key=lambda h: h[0], reverse=True)
return sum((i + 1) * bid for (i, (_, bid)) in enumerate(hands))
part1 = solve(part=1)
part2 = solve(part=2)
print(part1, part2)

34
day08.py Normal file
View file

@ -0,0 +1,34 @@
import fileinput
import math
input = fileinput.input()
instructions = next(input).strip()
next(input)
tree = {}
for line in input:
if line.strip():
(parent, children) = line.split(" = ")
(left, right) = children[1:-2].split(", ")
tree[parent] = (left, right)
nodes = [node for node in tree if node[2] == "A"]
cycles = [0] * len(nodes)
for i in range(len(nodes)):
while nodes[i][2] != "Z":
for instr in instructions:
cycles[i] += 1
if instr == "L":
(nodes[i], _) = tree[nodes[i]]
else:
(_, nodes[i]) = tree[nodes[i]]
if nodes[i][2] == "Z":
break
part1 = cycles[nodes.index("ZZZ")]
part2 = math.lcm(*cycles)
print(part1, part2)