2015 day 12, 13 & 14.
This commit is contained in:
parent
8d2f61fa65
commit
cd58b7861b
@ -42,7 +42,6 @@ def find_next_password(p: str) -> str:
|
||||
|
||||
line = sys.stdin.read().strip()
|
||||
|
||||
|
||||
answer_1 = find_next_password(line)
|
||||
print(f"answer 1 is {answer_1}")
|
||||
|
||||
|
27
src/holt59/aoc/2015/day12.py
Normal file
27
src/holt59/aoc/2015/day12.py
Normal file
@ -0,0 +1,27 @@
|
||||
import json
|
||||
import sys
|
||||
from typing import TypeAlias
|
||||
|
||||
JsonObject: TypeAlias = dict[str, "JsonObject"] | list["JsonObject"] | int | str
|
||||
|
||||
|
||||
def json_sum(value: JsonObject, ignore: str | None = None) -> int:
|
||||
if isinstance(value, str):
|
||||
return 0
|
||||
elif isinstance(value, int):
|
||||
return value
|
||||
elif isinstance(value, list):
|
||||
return sum(json_sum(v, ignore=ignore) for v in value)
|
||||
elif ignore not in value.values():
|
||||
return sum(json_sum(v, ignore=ignore) for v in value.values())
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
data: JsonObject = json.load(sys.stdin)
|
||||
|
||||
answer_1 = json_sum(data)
|
||||
print(f"answer 1 is {answer_1}")
|
||||
|
||||
answer_2 = json_sum(data, "red")
|
||||
print(f"answer 2 is {answer_2}")
|
41
src/holt59/aoc/2015/day13.py
Normal file
41
src/holt59/aoc/2015/day13.py
Normal file
@ -0,0 +1,41 @@
|
||||
import itertools
|
||||
import sys
|
||||
from collections import defaultdict
|
||||
from typing import Literal, cast
|
||||
|
||||
import parse # type: ignore
|
||||
|
||||
|
||||
def max_change_in_happiness(happiness: dict[str, dict[str, int]]) -> int:
|
||||
guests = list(happiness)
|
||||
return max(
|
||||
sum(
|
||||
happiness[o][d] + happiness[d][o]
|
||||
for o, d in zip((guests[0],) + order, order + (guests[0],))
|
||||
)
|
||||
for order in map(tuple, itertools.permutations(guests[1:]))
|
||||
)
|
||||
|
||||
|
||||
lines = sys.stdin.read().splitlines()
|
||||
|
||||
happiness: dict[str, dict[str, int]] = defaultdict(dict)
|
||||
for line in lines:
|
||||
u1, gain_or_loose, hap, u2 = cast(
|
||||
tuple[str, Literal["gain", "lose"], int, str],
|
||||
parse.parse( # type: ignore
|
||||
"{} would {} {:d} happiness units by sitting next to {}.", line
|
||||
),
|
||||
)
|
||||
happiness[u1][u2] = hap if gain_or_loose == "gain" else -hap
|
||||
|
||||
|
||||
answer_1 = max_change_in_happiness(happiness)
|
||||
print(f"answer 1 is {answer_1}")
|
||||
|
||||
for guest in list(happiness):
|
||||
happiness["me"][guest] = 0
|
||||
happiness[guest]["me"] = 0
|
||||
|
||||
answer_2 = max_change_in_happiness(happiness)
|
||||
print(f"answer 2 is {answer_2}")
|
62
src/holt59/aoc/2015/day14.py
Normal file
62
src/holt59/aoc/2015/day14.py
Normal file
@ -0,0 +1,62 @@
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from typing import Literal, cast
|
||||
|
||||
import parse # type: ignore
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Reindeer:
|
||||
name: str
|
||||
speed: int
|
||||
fly_time: int
|
||||
rest_time: int
|
||||
|
||||
|
||||
lines = sys.stdin.read().splitlines()
|
||||
|
||||
reindeers: list[Reindeer] = []
|
||||
for line in lines:
|
||||
reindeer, speed, speed_time, rest_time = cast(
|
||||
tuple[str, int, int, int],
|
||||
parse.parse( # type: ignore
|
||||
"{} can fly {:d} km/s for {:d} seconds, "
|
||||
"but then must rest for {:d} seconds.",
|
||||
line,
|
||||
),
|
||||
)
|
||||
reindeers.append(
|
||||
Reindeer(name=reindeer, speed=speed, fly_time=speed_time, rest_time=rest_time)
|
||||
)
|
||||
|
||||
target = 1000 if len(reindeers) <= 2 else 2503
|
||||
|
||||
states: dict[Reindeer, tuple[Literal["resting", "flying"], int]] = {
|
||||
reindeer: ("resting", 0) for reindeer in reindeers
|
||||
}
|
||||
distances: dict[Reindeer, int] = {reindeer: 0 for reindeer in reindeers}
|
||||
points: dict[Reindeer, int] = {reindeer: 0 for reindeer in reindeers}
|
||||
|
||||
for time in range(target):
|
||||
for reindeer in reindeers:
|
||||
if states[reindeer][0] == "flying":
|
||||
distances[reindeer] += reindeer.speed
|
||||
|
||||
top_distance = max(distances.values())
|
||||
for reindeer in reindeers:
|
||||
if distances[reindeer] == top_distance:
|
||||
points[reindeer] += 1
|
||||
|
||||
for reindeer in reindeers:
|
||||
if states[reindeer][1] == time:
|
||||
if states[reindeer][0] == "resting":
|
||||
states[reindeer] = ("flying", time + reindeer.fly_time)
|
||||
else:
|
||||
states[reindeer] = ("resting", time + reindeer.rest_time)
|
||||
|
||||
|
||||
answer_1 = max(distances.values())
|
||||
print(f"answer 1 is {answer_1}")
|
||||
|
||||
answer_2 = max(points.values()) - 1
|
||||
print(f"answer 2 is {answer_2}")
|
1
src/holt59/aoc/inputs/holt59/2015/day12.txt
Normal file
1
src/holt59/aoc/inputs/holt59/2015/day12.txt
Normal file
File diff suppressed because one or more lines are too long
56
src/holt59/aoc/inputs/holt59/2015/day13.txt
Normal file
56
src/holt59/aoc/inputs/holt59/2015/day13.txt
Normal file
@ -0,0 +1,56 @@
|
||||
Alice would lose 2 happiness units by sitting next to Bob.
|
||||
Alice would lose 62 happiness units by sitting next to Carol.
|
||||
Alice would gain 65 happiness units by sitting next to David.
|
||||
Alice would gain 21 happiness units by sitting next to Eric.
|
||||
Alice would lose 81 happiness units by sitting next to Frank.
|
||||
Alice would lose 4 happiness units by sitting next to George.
|
||||
Alice would lose 80 happiness units by sitting next to Mallory.
|
||||
Bob would gain 93 happiness units by sitting next to Alice.
|
||||
Bob would gain 19 happiness units by sitting next to Carol.
|
||||
Bob would gain 5 happiness units by sitting next to David.
|
||||
Bob would gain 49 happiness units by sitting next to Eric.
|
||||
Bob would gain 68 happiness units by sitting next to Frank.
|
||||
Bob would gain 23 happiness units by sitting next to George.
|
||||
Bob would gain 29 happiness units by sitting next to Mallory.
|
||||
Carol would lose 54 happiness units by sitting next to Alice.
|
||||
Carol would lose 70 happiness units by sitting next to Bob.
|
||||
Carol would lose 37 happiness units by sitting next to David.
|
||||
Carol would lose 46 happiness units by sitting next to Eric.
|
||||
Carol would gain 33 happiness units by sitting next to Frank.
|
||||
Carol would lose 35 happiness units by sitting next to George.
|
||||
Carol would gain 10 happiness units by sitting next to Mallory.
|
||||
David would gain 43 happiness units by sitting next to Alice.
|
||||
David would lose 96 happiness units by sitting next to Bob.
|
||||
David would lose 53 happiness units by sitting next to Carol.
|
||||
David would lose 30 happiness units by sitting next to Eric.
|
||||
David would lose 12 happiness units by sitting next to Frank.
|
||||
David would gain 75 happiness units by sitting next to George.
|
||||
David would lose 20 happiness units by sitting next to Mallory.
|
||||
Eric would gain 8 happiness units by sitting next to Alice.
|
||||
Eric would lose 89 happiness units by sitting next to Bob.
|
||||
Eric would lose 69 happiness units by sitting next to Carol.
|
||||
Eric would lose 34 happiness units by sitting next to David.
|
||||
Eric would gain 95 happiness units by sitting next to Frank.
|
||||
Eric would gain 34 happiness units by sitting next to George.
|
||||
Eric would lose 99 happiness units by sitting next to Mallory.
|
||||
Frank would lose 97 happiness units by sitting next to Alice.
|
||||
Frank would gain 6 happiness units by sitting next to Bob.
|
||||
Frank would lose 9 happiness units by sitting next to Carol.
|
||||
Frank would gain 56 happiness units by sitting next to David.
|
||||
Frank would lose 17 happiness units by sitting next to Eric.
|
||||
Frank would gain 18 happiness units by sitting next to George.
|
||||
Frank would lose 56 happiness units by sitting next to Mallory.
|
||||
George would gain 45 happiness units by sitting next to Alice.
|
||||
George would gain 76 happiness units by sitting next to Bob.
|
||||
George would gain 63 happiness units by sitting next to Carol.
|
||||
George would gain 54 happiness units by sitting next to David.
|
||||
George would gain 54 happiness units by sitting next to Eric.
|
||||
George would gain 30 happiness units by sitting next to Frank.
|
||||
George would gain 7 happiness units by sitting next to Mallory.
|
||||
Mallory would gain 31 happiness units by sitting next to Alice.
|
||||
Mallory would lose 32 happiness units by sitting next to Bob.
|
||||
Mallory would gain 95 happiness units by sitting next to Carol.
|
||||
Mallory would gain 91 happiness units by sitting next to David.
|
||||
Mallory would lose 66 happiness units by sitting next to Eric.
|
||||
Mallory would lose 75 happiness units by sitting next to Frank.
|
||||
Mallory would lose 99 happiness units by sitting next to George.
|
9
src/holt59/aoc/inputs/holt59/2015/day14.txt
Normal file
9
src/holt59/aoc/inputs/holt59/2015/day14.txt
Normal file
@ -0,0 +1,9 @@
|
||||
Vixen can fly 19 km/s for 7 seconds, but then must rest for 124 seconds.
|
||||
Rudolph can fly 3 km/s for 15 seconds, but then must rest for 28 seconds.
|
||||
Donner can fly 19 km/s for 9 seconds, but then must rest for 164 seconds.
|
||||
Blitzen can fly 19 km/s for 9 seconds, but then must rest for 158 seconds.
|
||||
Comet can fly 13 km/s for 7 seconds, but then must rest for 82 seconds.
|
||||
Cupid can fly 25 km/s for 6 seconds, but then must rest for 145 seconds.
|
||||
Dasher can fly 14 km/s for 3 seconds, but then must rest for 38 seconds.
|
||||
Dancer can fly 3 km/s for 16 seconds, but then must rest for 37 seconds.
|
||||
Prancer can fly 25 km/s for 6 seconds, but then must rest for 143 seconds.
|
12
src/holt59/aoc/inputs/tests/2015/day13.txt
Normal file
12
src/holt59/aoc/inputs/tests/2015/day13.txt
Normal file
@ -0,0 +1,12 @@
|
||||
Alice would gain 54 happiness units by sitting next to Bob.
|
||||
Alice would lose 79 happiness units by sitting next to Carol.
|
||||
Alice would lose 2 happiness units by sitting next to David.
|
||||
Bob would gain 83 happiness units by sitting next to Alice.
|
||||
Bob would lose 7 happiness units by sitting next to Carol.
|
||||
Bob would lose 63 happiness units by sitting next to David.
|
||||
Carol would lose 62 happiness units by sitting next to Alice.
|
||||
Carol would gain 60 happiness units by sitting next to Bob.
|
||||
Carol would gain 55 happiness units by sitting next to David.
|
||||
David would gain 46 happiness units by sitting next to Alice.
|
||||
David would lose 7 happiness units by sitting next to Bob.
|
||||
David would gain 41 happiness units by sitting next to Carol.
|
2
src/holt59/aoc/inputs/tests/2015/day14.txt
Normal file
2
src/holt59/aoc/inputs/tests/2015/day14.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.
|
||||
Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.
|
Loading…
Reference in New Issue
Block a user