2018 day 20 wip

This commit is contained in:
2024-09-02 09:24:55 -04:00
parent 5fae7c0366
commit 381f186a59
2 changed files with 158 additions and 34 deletions

View File

@@ -15,7 +15,60 @@ DIRS = {
def part_1(data):
data = data[1:]
g = defaultdict(set)
stack: list[tuple[tuple, int, list]] = [((0, 0), 0, [])]
while len(stack) > 0:
pos, i, i_outs = stack.pop()
assert i is not None
while i < len(data):
c = data[i]
if c in DIRS.keys():
npos = add2(pos, DIRS[c])
g[pos].add(npos)
g[npos].add(pos)
pos = npos
i += 1
elif c == "(":
to_continue = [i + 1]
open_count = 0
j_out = None
for j in range(i + 1, len(data)):
c = data[j]
if c == "|" and open_count == 0:
to_continue.append(j + 1)
elif c == "(":
open_count += 1
elif c == ")" and open_count != 0:
open_count -= 1
elif c == ")" and open_count == 0:
j_out = j
break
assert j_out is not None
for new_i in to_continue:
new_i_outs = list(i_outs)
new_i_outs.append(j_out)
stack.append((pos, new_i, new_i_outs))
break
elif c == "$":
break
elif c == ")" and len(i_outs) == 0:
assert False, "Encountered | without i_out"
elif c == ")":
i_new = i_outs.pop()
assert i == i_new
i += 1
elif c == "^":
i += 1
elif c == "|" and len(i_outs) == 0:
assert False, "Encountered | without i_out"
elif c == "|":
i = i_outs.pop()
i += 1
else:
assert False
# def parse(i):
# max_len = 0
@@ -45,39 +98,39 @@ def part_1(data):
#
# print(parse(0)[0])
g = defaultdict(set)
def parse(xs, i):
xs_orig = xs.copy()
xs_done = []
while i < len(data):
c = data[i]
if c in DIRS.keys():
for xi in range(len(xs)):
pos = xs[xi]
npos = add2(pos, DIRS[c])
g[pos].add(npos)
g[npos].add(pos)
xs[xi] = npos
i += 1
elif c == "(":
xs, i = parse(xs, i + 1)
elif c == "|":
xs_done += xs
xs = xs_orig
i += 1
elif c == ")":
xs_done += xs
return xs_done, i + 1
elif c == "$":
xs_done += xs
i += 1
else:
assert False
return xs_done, i
parse([(0, 0)], 0)
# g = defaultdict(set)
# def parse(xs, i):
# xs_orig = xs.copy()
# xs_done = []
#
# while i < len(data):
# c = data[i]
# if c in DIRS.keys():
# for xi in range(len(xs)):
# pos = xs[xi]
# npos = add2(pos, DIRS[c])
# g[pos].add(npos)
# g[npos].add(pos)
# xs[xi] = npos
# i += 1
# elif c == "(":
# xs, i = parse(xs, i + 1)
# elif c == "|":
# xs_done += xs
# xs = xs_orig
# i += 1
# elif c == ")":
# xs_done += xs
# return xs_done, i + 1
# elif c == "$":
# xs_done += xs
# i += 1
# else:
# assert False
# return xs_done, i
#
# parse([(0, 0)], 0)
seen = set()
dists = {}
xs = [(0, 0)]

71
2020/d8.py Normal file
View File

@@ -0,0 +1,71 @@
from lib import get_data, str_to_ints
def part_1(data):
insts = data.splitlines()
acc = 0
i = 0
seen = set()
while i < len(insts) and i not in seen:
inst = insts[i]
seen.add(i)
if inst.startswith("acc"):
(v,) = str_to_ints(inst)
acc += v
i += 1
elif inst.startswith("jmp"):
(v,) = str_to_ints(inst)
i += v
elif inst.startswith("nop"):
i += 1
else:
assert False
print(acc)
for j in range(len(insts)):
if "jmp" in insts[j]:
insts[j] = insts[j].replace("jmp", "nop")
elif "nop" in insts[j]:
insts[j] = insts[j].replace("nop", "jmp")
else:
continue
i = 0
acc = 0
seen = set()
while i < len(insts) and i not in seen:
inst = insts[i]
seen.add(i)
if inst.startswith("acc"):
(v,) = str_to_ints(inst)
acc += v
i += 1
elif inst.startswith("jmp"):
(v,) = str_to_ints(inst)
i += v
elif inst.startswith("nop"):
i += 1
else:
assert False
if not i in seen:
print(acc)
if "jmp" in insts[j]:
insts[j] = insts[j].replace("jmp", "nop")
elif "nop" in insts[j]:
insts[j] = insts[j].replace("nop", "jmp")
else:
continue
def main():
data = get_data(__file__)
part_1(data)
if __name__ == "__main__":
main()