Skip to content

Commit

Permalink
extra
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Jan 3, 2025
1 parent 7e008de commit 3da93c0
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 24 deletions.
3 changes: 2 additions & 1 deletion aoc_wim/aoc2015/q14.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
https://adventofcode.com/2015/day/14
"""
from aocd import data
from aocd import extra


class Reindeer:
Expand Down Expand Up @@ -30,7 +31,7 @@ def step(self):


deers = [Reindeer(line) for line in data.splitlines()]
t = 1000 if len(deers) == 2 else 2503
t = extra.get("t", 2503)
for _ in range(t):
for deer in deers:
deer.step()
Expand Down
7 changes: 4 additions & 3 deletions aoc_wim/aoc2015/q17.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
https://adventofcode.com/2015/day/17
"""
from aocd import data
from aocd import extra

from aoc_wim.stuff import subset_sum

vals = [int(n) for n in data.splitlines()]
liters = 25 if len(vals) == 5 else 150
subsets = list(subset_sum(vals, liters))

vals = [*map(int, data.split())]
subsets = list(subset_sum(vals, extra.get("liters", 150)))
print("answer_a:", len(subsets))
fewest = min(subsets, key=len)
print("answer_b:", sum(len(s) == len(fewest) for s in subsets))
6 changes: 4 additions & 2 deletions aoc_wim/aoc2016/q08.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import numpy as np
from aocd import data
from aocd import extra

from aoc_wim.ocr import AOCR

Expand All @@ -20,8 +21,9 @@ def animate(A, line):


lines = data.splitlines()
im = (3, 7) if len(lines) == 4 else (6, 50)
A = np.full(im, ".")
w = extra.get("screen_width", 50)
h = extra.get("screen_height", 6)
A = np.full((h, w), ".")
for line in lines:
A = animate(A, line)
print("answer_a:", (A == "#").sum())
Expand Down
5 changes: 3 additions & 2 deletions aoc_wim/aoc2016/q18.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
https://adventofcode.com/2016/day/18
"""
from aocd import data
from aocd import extra


def generate_rows(row):
"""rule90"""
"""rule90: https://en.wikipedia.org/wiki/Rule_90"""
idx = range(len(row))
while True:
yield row
row = "." + row + "."
row = "".join(["^."[row[i] == row[i + 2]] for i in idx])


n_rows = 3 if len(data) == 5 else 10 if len(data) == 10 else 40
n_rows = extra.get("n_rows", 40)
gen = generate_rows(data)
safe = sum(next(gen).count(".") for _ in range(n_rows))
print("answer_a:", safe)
Expand Down
13 changes: 4 additions & 9 deletions aoc_wim/aoc2016/q20.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
https://adventofcode.com/2016/day/20
"""
from aocd import data
from aocd import extra


intervals = []
for line in data.splitlines():
left, right = line.split("-")
interval = int(left), int(right)
intervals.append(interval)
intervals.sort()

ns = [*map(int, data.replace("-", " ").split())]
intervals = sorted(zip(ns[0::2], ns[1::2]))
while True:
n = len(intervals)
for i in range(n - 1):
Expand All @@ -26,8 +22,7 @@

print("answer_a:", intervals[0][1] + 1)

b = 4294967296 if len(intervals) > 3 else 10
b = extra.get("max_val", 4294967295) + 1
for lo, high in intervals:
b -= high - lo + 1

print("answer_b:", b)
3 changes: 2 additions & 1 deletion aoc_wim/aoc2016/q21.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from itertools import permutations

from aocd import data
from aocd import extra


def swap_position(list_, words):
Expand Down Expand Up @@ -76,6 +77,6 @@ def descramble(data, scrambled):
return "".join(original)


start = "abcde" if data.count("\n") < 10 else "abcdefgh"
start = extra.get("start", "abcdefgh")
print("answer_a:", scramble(data, start))
print("answer_b:", descramble(data, "fbgdceah"))
9 changes: 5 additions & 4 deletions aoc_wim/aoc2017/q10.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
https://adventofcode.com/2017/day/10
"""
from functools import reduce
from operator import xor

from aocd import data
from aocd import extra


def munge(state, lengths, iterations=64, n=None):
Expand All @@ -24,10 +24,10 @@ def munge(state, lengths, iterations=64, n=None):


def knot_hash(data, n=256):
state = list(range(n))
state = [*range(n)]
lengths = [ord(x) for x in data] + [17, 31, 73, 47, 23]
munge(state, lengths, n=n)
reduced = [reduce(xor, state[16 * i : 16 * (i + 1)]) for i in range(16)]
reduced = [reduce(int.__xor__, state[16 * i : 16 * (i + 1)]) for i in range(16)]
return bytes(reduced).hex()


Expand All @@ -36,7 +36,8 @@ def knot_hash(data, n=256):
except ValueError:
a = None
else:
state = list(range(5 if data == "3, 4, 1, 5" else 256))
n = extra.get("n", 256)
state = [*range(n)]
munge(state, lengths, iterations=1)
a = state[0] * state[1]

Expand Down
5 changes: 3 additions & 2 deletions aoc_wim/aoc2019/q08.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"""
import numpy as np
from aocd import data
from aocd import extra

from aoc_wim.ocr import AOCR

hw = {16: (2, 2), 12: (2, 3)}
h, w = hw.get(len(data), (6, 25))
w = extra.get("image_width", 25)
h = extra.get("image_height", 6)
a = np.fromiter(data, int).reshape((-1, h, w))
layer = min(a, key=lambda v: (v == 0).sum())
print("answer_a:", (layer == 1).sum() * (layer == 2).sum())
Expand Down

0 comments on commit 3da93c0

Please sign in to comment.