Skip to content

Commit ad33d1c

Browse files
committed
Add day 15, 16, 17, 18, 2016
1 parent cfa20a0 commit ad33d1c

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

2016/day15/solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import re
2+
from itertools import count
3+
4+
with open("input") as f:
5+
inp = f.read().strip()
6+
7+
discs = []
8+
for line in inp.split("\n"):
9+
nums = list(map(int, re.findall("\d+", line)))
10+
nums.pop(2)
11+
discs.append(tuple(nums))
12+
13+
14+
def solve(part2 = False):
15+
if part2:
16+
discs.append((len(discs) + 1, 11, 0))
17+
for t in count():
18+
if all((init_pos + t + disc) % n == 0 for disc, n, init_pos in discs):
19+
return t
20+
21+
22+
# Part 1
23+
print(solve())
24+
25+
# Part 2
26+
print(solve(True))

2016/day16/solution.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def process(s):
2+
res = ""
3+
for c in s[::-1]:
4+
bit = "1" if c == "0" else "0"
5+
res += bit
6+
return s + "0" + res
7+
8+
9+
def checksum(s):
10+
pairs = []
11+
res = ""
12+
for i in range(0, len(s), 2):
13+
pairs.append(s[i:(i + 2)])
14+
x = "1" if s[i] == s[i + 1] else "0"
15+
res += x
16+
if len(res) % 2 == 1:
17+
return res
18+
return checksum(res)
19+
20+
21+
def solve(length):
22+
state = "10111100110001111"
23+
while len(state) < length:
24+
state = process(state)
25+
return checksum(state[:length])
26+
27+
28+
# Part 1
29+
print(solve(272))
30+
31+
# Part 2
32+
print(solve(35651584))

2016/day17/solution.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import hashlib
2+
from collections import deque
3+
4+
passcode = "qzthpkfp"
5+
6+
7+
def get_dirs(path):
8+
dirs = [-1j, 1j, -1, 1]
9+
hash_ = hashlib.md5((passcode + path).encode()).hexdigest()
10+
hash_ = hash_[:4]
11+
return [dirs[i] for i, c in enumerate(hash_[:4]) if "b" <= c <= "f"]
12+
13+
14+
dirs_letter = {1: "R", -1: "L", 1j: "D", -1j: "U"}
15+
first = True
16+
longest_path = 0
17+
q = deque([(0, "")])
18+
seen = set()
19+
while q:
20+
p, path = q.popleft()
21+
if (p, path) in seen:
22+
continue
23+
if p == 3+3*1j:
24+
longest_path = max(longest_path, len(path))
25+
if first:
26+
print(path)
27+
first = False
28+
continue
29+
seen.add((p, path))
30+
for dp in get_dirs(path):
31+
new_p = p + dp
32+
if not (0 <= new_p.real <= 3 and 0 <= new_p.imag <= 3):
33+
continue
34+
q.append((new_p, path + dirs_letter[dp]))
35+
36+
print(longest_path)

2016/day18/solution.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
with open("input") as f:
2+
inp = f.read().strip()
3+
4+
5+
def next_row(s):
6+
res = ""
7+
for i in range(len(s)):
8+
if i == 0:
9+
tiles = (".", s[0], s[1])
10+
elif i == len(s) - 1:
11+
tiles = (s[len(s) - 2], s[len(s) - 1], ".")
12+
else:
13+
tiles = (s[i-1], s[i], s[i+1])
14+
left, center, right = tiles
15+
if left == "^" and center == "^" and right == ".":
16+
res += "^"
17+
elif left == "." and center == "^" and right == "^":
18+
res += "^"
19+
elif left == "^" and center == "." and right == ".":
20+
res += "^"
21+
elif left == "." and center == "." and right == "^":
22+
res += "^"
23+
else:
24+
res += "."
25+
return res
26+
27+
28+
rows = [inp]
29+
for i in range(400000 - 1):
30+
rows.append(next_row(rows[-1]))
31+
32+
# Part 1
33+
print(sum(row.count(".") for row in rows[:40]))
34+
35+
# Part 2
36+
print(sum(row.count(".") for row in rows))

0 commit comments

Comments
 (0)