Solve a couple of 2017 problems.

This commit is contained in:
felixm 2024-05-21 20:53:55 -04:00
parent 6f63a866e1
commit 1a71ab97c3
6 changed files with 240 additions and 3 deletions

64
2017/d10.py Normal file
View File

@ -0,0 +1,64 @@
from lib import str_to_ints
def part_1(data):
ns = list(range(256))
lenghts = str_to_ints(data)
i = 0
skip_size = 0
for length in lenghts:
a, b = i, (i + length) % len(ns)
if a <= b:
ns[a:b] = reversed(ns[a:b])
else:
rev = list(reversed(ns[a:] + ns[:b]))
ns[a:] = rev[:len(ns) - a]
ns[:b] = rev[-b:]
i = (i + length + skip_size) % len(ns)
skip_size += 1
print(ns[0] * ns[1])
def part_2(data):
ns = list(range(256))
lenghts = list(map(ord, data)) + [17, 31, 73, 47, 23]
rounds = 64
i = 0
skip_size = 0
for _ in range(rounds):
for length in lenghts:
assert length < 256
a, b = i, (i + length) % len(ns)
if a <= b:
ns[a:b] = reversed(ns[a:b])
else:
rev = list(reversed(ns[a:] + ns[:b]))
ns[a:] = rev[:len(ns) - a]
if b != 0:
ns[:b] = rev[-b:]
i = (i + length + skip_size) % len(ns)
assert len(ns) == 256
skip_size += 1
out = ""
for i in range(0, len(ns), 16):
r = 0
for j in range(i, i + 16):
r ^= ns[j]
out += f"{r:02x}"
return out
def main():
data = open(0).read().strip()
part_1(data)
assert part_2("") == "a2582a3a0e66e6e86e3812dcb672a272"
assert part_2("AoC 2017") == "33efeb34ea91902bb2f59c9920caa6cd"
assert part_2("1,2,3") == "3efbe78a8d82f29979031a4aa0b16a9d"
assert part_2("1,2,4") =="63960835bcdc130f0b66d7ff4f6a5a8e"
print(part_2(data))
if __name__ == "__main__":
main()

52
2017/d11.py Normal file
View File

@ -0,0 +1,52 @@
from lib import add2
from math import ceil
DIR = {
"n": (-2, 0),
"s": (2, 0),
"e": (0, 1),
"w": (0, -1),
"ne": (-1, 1),
"nw": (-1, -1),
"se": (1, 1),
"sw": (1, -1),
}
def steps_to_zero(pos: tuple[int, int]):
c = list(map(abs, pos))
steps = c[1]
c[0] -= c[1]
if c[0] > 0:
steps += ceil(c[0] / 2)
return steps
def part_1(data):
c = (0, 0)
for d in data.split(","):
c = add2(c, DIR[d])
return steps_to_zero(c)
def part_2(data):
c = (0, 0)
steps_max = 0
for d in data.split(","):
c = add2(c, DIR[d])
steps_max = max(steps_to_zero(c), steps_max)
return steps_max
def main():
data = open(0).read().strip()
print(part_1(data))
print(part_2(data))
assert part_1("ne,ne,ne") == 3
assert part_1("ne,ne,sw,sw") == 0
assert part_1("ne,ne,s,s") == 2
assert part_1("se,sw,se,sw,sw") == 3
if __name__ == "__main__":
main()

56
2017/d12.py Normal file
View File

@ -0,0 +1,56 @@
from lib import str_to_ints
from collections import defaultdict
def get_graph(data):
g = defaultdict(set)
for line in data.splitlines():
fs = str_to_ints(line)
l = fs[0]
for r in fs[1:]:
g[l].add(r)
g[r].add(l)
return g
def part_1(data):
g = get_graph(data)
to_visit = [0]
seen = set()
while to_visit:
c = to_visit.pop()
if c in seen:
continue
seen.add(c)
for nb in g[c]:
to_visit.append(nb)
print(len(seen))
def part_2(data):
g = get_graph(data)
group_count = 0
seen = set()
for c in g.keys():
if c in seen:
continue
group_count += 1
to_visit = [c]
while to_visit:
c = to_visit.pop()
if c in seen:
continue
seen.add(c)
for nb in g[c]:
to_visit.append(nb)
print(group_count)
def main():
data = open(0).read().strip()
part_1(data)
part_2(data)
if __name__ == "__main__":
main()

28
2017/d8.py Normal file
View File

@ -0,0 +1,28 @@
from collections import defaultdict
def part_1(data):
regs = defaultdict(int)
allmax = 0
for line in data.splitlines():
match line.split():
case [reg_a, op, value, "if", reg_c, cmp, val_c]:
value, val_c = int(value), int(val_c)
reg_val = regs[reg_c]
if eval(f"{reg_val} {cmp} {val_c}"):
value = value if op == "inc" else -value
regs[reg_a] += value
case _:
assert False
allmax = max(max(regs.values()), allmax)
print(max(regs.values()))
print(allmax)
def main():
data = open(0).read().strip()
part_1(data)
if __name__ == "__main__":
main()

33
2017/d9.py Normal file
View File

@ -0,0 +1,33 @@
def part_1(data):
i = 0
group_score, garbage_score = 0, 0
in_garbage = False
group_level = 0
while i < len(data):
match data[i]:
case "{" if not in_garbage:
group_level += 1
case "}" if not in_garbage:
assert group_level > 0
group_score += group_level
group_level -= 1
case "<" if not in_garbage:
in_garbage = True
case ">" if in_garbage:
in_garbage = False
case "!" if in_garbage:
i += 1
case _ if in_garbage:
garbage_score += 1
i += 1
print(group_score)
print(garbage_score)
def main():
data = open(0).read().strip()
part_1(data)
if __name__ == "__main__":
main()

View File

@ -57,8 +57,7 @@ written in Python.
- Day 22: 90:00
- Day 23: 60:00
- Day 24: 48:00
- Day 25:
- Day 25: 6:45
# 2017
@ -69,4 +68,9 @@ written in Python.
- Day 5: 6:13
- Day 6: 8:37
- Day 7: 19:22
- Day 8:
- Day 8: 8:15
- Day 9: 6:10
- Day 10: 55:00
- Day 11: 66:06
- Day 12: 6:44
- Day 13: