예제 #1
0
import aoc
data = aoc.strlist(11)

h = len(data)
w = len(data[0])
p = {}
q = {}
for i in range(w):
    for j in range(h):
        if data[j][i] == ".": continue
        p[i, j] = data[j][i]
        q[i, j] = data[j][i]

d = {}
for i in range(w):
    for j in range(h):
        d[i, j] = []
        for u, v in ((1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0),
                     (-1, 1), (0, 1)):
            if (i + u, j + v) in p:
                d[i, j].append((i + u, j + v))

c = set(p.keys())
while c:
    pp = dict(p)
    cc = set()
    for i, j in c:
        n = [p[k] for k in d[i, j]].count("#")
        if p[i, j] == "L" and n == 0:
            pp[i, j] = "#"
            cc.update(d[i, j])
예제 #2
0
import aoc
data = aoc.strlist(3)

p = len(data)
q = len(data[0])
n = "".join(i[j % q] for i, j in zip(data, range(0, 3 * p, 3))).count("#")

m = n
for u, v in [(1, 1), (5, 1), (7, 1), (1, 2)]:
    m *= "".join(i[j % q] for i, j in zip(data[::v], range(0, u * p //
                                                           v, u))).count("#")

print(n, m)
aoc.tock("ms")
예제 #3
0
import aoc

data = aoc.strlist(2)

n = 0
m = 0
for i in data:
    u, v, c, p = i.replace(": ", "-").replace(" ", "-").split("-")
    u, v = int(u), int(v)
    if u <= p.count(c) <= v:
        n += 1
    if (p[u-1] == c) != (p[v-1] == c):
        m += 1
print(n, m)

aoc.tock("ms")
예제 #4
0
import aoc
data = aoc.strlist(5)

y = set()
for i in data:
    y.add(int(i.translate("".maketrans("FBLR", "0101")), 2))
print(max(y), (set(range(min(y), max(y) + 1)) - y).pop())

aoc.tock("ms")
예제 #5
0
import aoc
from collections import deque

data = aoc.strlist(7)

bags = {}
for i in data:
    outer, inner = i.rstrip(".").split(" contain ")
    outer = outer.rstrip("s")
    bags[outer] = []
    if inner == "no other bags": continue
    for j in inner.split(", "):
        n, col = j.split(" ", 1)
        bags[outer].append((int(n), col.rstrip("s")))

contain = set()
queue = deque(["shiny gold bag"])
while queue:
    i = queue.pop()
    for outer, inner in bags.items():
        for j in inner:
            if j[1] == i:
                contain.add(outer)
                queue.append(outer)

queue = deque([(1, "shiny gold bag")])
count = 0
while queue:
    r, i = queue.pop()
    for j in bags[i]:
        count += r * j[0]
예제 #6
0
import aoc
data = aoc.strlist(8)

instruc = [(i.split(" ")[0], int(i.split(" ")[1])) for i in data]

accx = 0
point = 0
seen = set()
while point not in seen:
    seen.add(point)
    i, j = instruc[point]
    point += j if i == "jmp" else 1
    accx += j if i == "acc" else 0

for k in range(len(instruc)):
    if instruc[k][0] == "acc": continue
    if instruc[k][1] in [0, 1]: continue
    alter = list(instruc)
    alter[k] = ("nop" if alter[k][0] == "jmp" else "jmp", alter[k][1])

    accy = 0
    point = 0
    seen = set()
    while point not in seen:
        seen.add(point)
        i, j = alter[point]
        point += j if i == "jmp" else 1
        accy += j if i == "acc" else 0
        if point >= len(alter): break
    else:
        continue