Compare commits
3 Commits
5fae7c0366
...
ad53396ca6
Author | SHA1 | Date | |
---|---|---|---|
ad53396ca6 | |||
b307e09f2b | |||
381f186a59 |
121
2018/d20.py
121
2018/d20.py
@ -15,7 +15,60 @@ DIRS = {
|
|||||||
|
|
||||||
|
|
||||||
def part_1(data):
|
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):
|
# def parse(i):
|
||||||
# max_len = 0
|
# max_len = 0
|
||||||
@ -45,39 +98,39 @@ def part_1(data):
|
|||||||
#
|
#
|
||||||
# print(parse(0)[0])
|
# print(parse(0)[0])
|
||||||
|
|
||||||
g = defaultdict(set)
|
# g = defaultdict(set)
|
||||||
def parse(xs, i):
|
# def parse(xs, i):
|
||||||
xs_orig = xs.copy()
|
# xs_orig = xs.copy()
|
||||||
xs_done = []
|
# xs_done = []
|
||||||
|
#
|
||||||
while i < len(data):
|
# while i < len(data):
|
||||||
c = data[i]
|
# c = data[i]
|
||||||
if c in DIRS.keys():
|
# if c in DIRS.keys():
|
||||||
for xi in range(len(xs)):
|
# for xi in range(len(xs)):
|
||||||
pos = xs[xi]
|
# pos = xs[xi]
|
||||||
npos = add2(pos, DIRS[c])
|
# npos = add2(pos, DIRS[c])
|
||||||
g[pos].add(npos)
|
# g[pos].add(npos)
|
||||||
g[npos].add(pos)
|
# g[npos].add(pos)
|
||||||
xs[xi] = npos
|
# xs[xi] = npos
|
||||||
i += 1
|
# i += 1
|
||||||
elif c == "(":
|
# elif c == "(":
|
||||||
xs, i = parse(xs, i + 1)
|
# xs, i = parse(xs, i + 1)
|
||||||
elif c == "|":
|
# elif c == "|":
|
||||||
xs_done += xs
|
# xs_done += xs
|
||||||
xs = xs_orig
|
# xs = xs_orig
|
||||||
i += 1
|
# i += 1
|
||||||
elif c == ")":
|
# elif c == ")":
|
||||||
xs_done += xs
|
# xs_done += xs
|
||||||
return xs_done, i + 1
|
# return xs_done, i + 1
|
||||||
elif c == "$":
|
# elif c == "$":
|
||||||
xs_done += xs
|
# xs_done += xs
|
||||||
i += 1
|
# i += 1
|
||||||
else:
|
# else:
|
||||||
assert False
|
# assert False
|
||||||
return xs_done, i
|
# return xs_done, i
|
||||||
|
#
|
||||||
parse([(0, 0)], 0)
|
# parse([(0, 0)], 0)
|
||||||
|
|
||||||
seen = set()
|
seen = set()
|
||||||
dists = {}
|
dists = {}
|
||||||
xs = [(0, 0)]
|
xs = [(0, 0)]
|
||||||
|
71
2020/d8.py
Normal file
71
2020/d8.py
Normal 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()
|
33
2020/d9.py
Normal file
33
2020/d9.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
from lib import get_data
|
||||||
|
|
||||||
|
|
||||||
|
def part_1(data):
|
||||||
|
target = 0
|
||||||
|
xs = list(map(int, data.splitlines()))
|
||||||
|
for i in range(25, len(xs)):
|
||||||
|
x = xs[i]
|
||||||
|
good = False
|
||||||
|
for j in range(i - 25, i):
|
||||||
|
for k in range(j, i):
|
||||||
|
if xs[j] + xs[k] == x:
|
||||||
|
good = True
|
||||||
|
if not good:
|
||||||
|
print(x)
|
||||||
|
target = x
|
||||||
|
break
|
||||||
|
|
||||||
|
for i in range(len(xs)):
|
||||||
|
for j in range(i + 1, len(xs)):
|
||||||
|
if sum(xs[i : j + 1]) == target:
|
||||||
|
r = xs[i : j + 1]
|
||||||
|
print(min(r) + max(r))
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
data = get_data(__file__)
|
||||||
|
part_1(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@ -142,7 +142,9 @@ Solutions and utility script for Advent of Code challenges in Python.
|
|||||||
- Day 5: 11:53 (not competitive)
|
- Day 5: 11:53 (not competitive)
|
||||||
- Day 6: 4:11 (75th, finally on the leaderboard)
|
- Day 6: 4:11 (75th, finally on the leaderboard)
|
||||||
- Day 7: 24:39 (bad)
|
- Day 7: 24:39 (bad)
|
||||||
- Day 8:
|
- Day 8: 6:26 (43th, tied George Hotz :)
|
||||||
|
- Day 9: 7:37 (choked bad)
|
||||||
|
- Day 10:
|
||||||
|
|
||||||
|
|
||||||
## AoC 2022
|
## AoC 2022
|
||||||
|
Loading…
Reference in New Issue
Block a user