Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Jan 3, 2025
1 parent feb46fc commit 9ee8777
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 35 deletions.
11 changes: 6 additions & 5 deletions aoc_wim/aoc2016/q10.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
from math import prod

from aocd import data
from aocd import extra
from parse import parse


class Bot:
def __init__(self, name, low_to=None, high_to=None, lh=(17, 61)):
def __init__(self, name, low_to=None, high_to=None):
self.name = name
self.low_to = low_to
self.high_to = high_to
self.chips = []
self.part_b = False
self.lh = lh

def receive(self, chip):
self.chips.append(chip)
Expand All @@ -27,22 +27,23 @@ def send(self):
self.chips = []
self.low_to.receive(low)
self.high_to.receive(high)
if (low, high) == self.lh:
if (low, high) == (lo, hi):
self.part_b = True


receives = []
receivers = {}
lines = data.splitlines()
lh = (2, 5) if len(lines) == 6 else (17, 61)
hi = extra.get("chip1", 61)
lo = extra.get("chip2", 17)
for line in lines:
if line.startswith("value"):
tup = parse("value {:d} goes to {}", line).fixed
receives.append(tup)
else:
result = parse("{} gives low to {} and high to {}", line)
bot_name, low_to, high_to = result.fixed
bot = receivers.get(bot_name, Bot(bot_name, lh=lh))
bot = receivers.get(bot_name, Bot(bot_name))
bot.low_to = receivers.get(low_to, Bot(low_to))
bot.high_to = receivers.get(high_to, Bot(high_to))
for receiver in bot, bot.low_to, bot.high_to:
Expand Down
13 changes: 6 additions & 7 deletions aoc_wim/aoc2017/q16.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
https://adventofcode.com/2017/day/16
"""
from aocd import data
from aocd import extra


def parsed(data, mod=16):
Expand Down Expand Up @@ -49,10 +50,8 @@ def dance(data, d, n=1):
return "".join(d)


if len(data) == 12:
# example data
print("answer_a:", dance(data, [*"abcde"]))
print("answer_b:", dance(data, [*"abcde"], n=2))
else:
print("answer_a:", dance(data, [*"abcdefghijklmnop"]))
print("answer_b:", dance(data, [*"abcdefghijklmnop"], n=1000000000))
iterations = extra.get("iterations", 1000000000)
s0 = "abcdefghijklmnop"[:extra.get("n_programs")]

print("answer_a:", dance(data, list(s0)))
print("answer_b:", dance(data, list(s0), n=iterations))
16 changes: 2 additions & 14 deletions aoc_wim/aoc2019/q12.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import numpy as np
from aocd import data
from aocd import extra


def simulate(data, n=0):
Expand Down Expand Up @@ -36,19 +37,6 @@ def simulate(data, n=0):
return e


test10 = """\
<x=-1, y=0, z=2>
<x=2, y=-10, z=-7>
<x=4, y=-8, z=8>
<x=3, y=5, z=-1>"""

test100 = """\
<x=-8, y=-10, z=0>
<x=5, y=5, z=10>
<x=2, y=-7, z=3>
<x=9, y=-8, z=-3>"""


n = 10 if data == test10 else 100 if data == test100 else 1000
n = extra.get("iterations", 1000)
print("answer_a:", simulate(data, n))
print("answer_b:", simulate(data))
11 changes: 2 additions & 9 deletions aoc_wim/aoc2019/q24.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
https://adventofcode.com/2019/day/24
"""
from aocd import data
from aocd import extra

from aoc_wim.zgrid import ZGrid

Expand Down Expand Up @@ -93,17 +94,9 @@ def evolve_b(grids):
grids.update(new_grids)


example = """\
....#
#..#.
#..##
..#..
#...."""


def part_b(data):
zgrid = ZGrid(data)
t = 10 if data == example else 200 # example data has fewer iterations
t = extra.get("iterations", 200)
del zgrid[2 + 2j]
grids = {0: zgrid}
for i in range(t):
Expand Down

0 comments on commit 9ee8777

Please sign in to comment.