Example #1
0
def decode(keypad):
    keypad = ZGrid(keypad)
    z = keypad.z("5")
    code = ""
    for line in data.splitlines():
        for direction in line:
            dz = getattr(ZGrid, direction)
            if keypad.get(z + dz, " ").strip():
                z += dz
        code += keypad[z]
    return code
Example #2
0
g = ZGrid(data, transform=int)
a = b = 0
while True:
    b += 1
    n = 0
    flash = []
    for z in g:
        if g[z] == 9:
            g[z] = 0
            flash.append(z)
        else:
            g[z] += 1
    while flash:
        z0 = flash.pop()
        n += 1
        for z in g.near(z0, n=8):
            if g.get(z):
                if g[z] == 9:
                    g[z] = 0
                    flash.append(z)
                else:
                    g[z] += 1
    if b <= 100:
        a += n
    if n == len(g):
        break

print("part a:", a)
print("part b:", b)
def test_n_points_in_10x10():
    grid = ZGrid(minibeam)
    count = 0
    for z in zrange(0, 10 + 10j):
        count += grid.get(z) == "#"
    assert count == 27
Example #4
0
"""
--- Day 19: A Series of Tubes ---
https://adventofcode.com/2017/day/19
"""
from aocd import data
from aoc_wim.zgrid import ZGrid


grid = ZGrid(data)
z = data.splitlines()[0].index("|")
assert grid[z] == "|"
dz = ZGrid.down
letters = ""
n_steps = 0
while True:
    z += dz
    n_steps += 1
    a = grid.get(z, " ")
    if a == "+":
        dz *= ZGrid.turn_left
        if grid.get(z + dz, " ") == " ":
            dz *= ZGrid.turn_around
    elif a not in "-| ":
        letters += a
    elif a == " ":
        break

print("part a:", letters)
print("part b:", n_steps)
Example #5
0
  #.......#.....#.#...#...............#...#  
  #############.#.#.###.###################  
               A O F   N                     
               A A D   M                     """

grid = ZGrid(data, on=".", off="#")
h, w = np.array(grid).shape
dzs = [-1j, 1, 1j, -1]

# parse the warps
outside = {}
inside = {}
for z, glyph in grid.items():
    if glyph in string.ascii_uppercase:
        for dz in dzs:
            if grid.get(z + dz) == ".":
                zp = z + dz  # actual position of portal
                name = glyph + grid.get(z - dz)  # add other letter
                if 3 < z.real < w - 3 and 3 < z.imag < h - 3:
                    side = inside
                else:
                    side = outside
                if zp - z in {1j, 1}:
                    name = name[::-1]  # reverse the label
                side[name] = zp
                break

state0 = outside.pop("AA")
target = outside.pop("ZZ")
assert outside.keys() == inside.keys()
warps = bidict({v: outside[k] for k, v in inside.items()})