Skip to content

Commit b70b005

Browse files
committed
Add 2017
1 parent e6bbc3c commit b70b005

File tree

25 files changed

+1014
-0
lines changed

25 files changed

+1014
-0
lines changed

2017/day01/solution.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
with open("input") as f:
2+
inp = f.read().strip()
3+
4+
# Part 1
5+
print(sum(int(x) for x, y in zip(inp, inp[1:] + inp[0]) if x == y))
6+
7+
# Part 2
8+
ans = 0
9+
for i in range(len(inp)):
10+
if inp[i] == inp[(i + len(inp)//2) % len(inp)]:
11+
ans += int(inp[i])
12+
13+
print(ans)

2017/day02/solution.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from itertools import permutations
2+
import re
3+
4+
with open("input") as f:
5+
inp = f.read().strip().split("\n")
6+
7+
rows = [list(map(int, re.findall("\d+", line))) for line in inp]
8+
9+
# Part 1
10+
print(sum(max(row) - min(row) for row in rows))
11+
12+
# Part 2
13+
ans = 0
14+
for row in rows:
15+
for x, y in permutations(row, 2):
16+
if x % y == 0:
17+
ans += x // y
18+
break
19+
print(ans)

2017/day03/solution.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from itertools import count
2+
from collections import defaultdict
3+
4+
inp = 368078
5+
6+
# Part 1
7+
def solve_pt1():
8+
dp = [1, -1j, -1, 1j]
9+
points = {1: 0}
10+
p, c = 0, 2
11+
for n in count():
12+
dist = int(((n + 2)/2) // 1)
13+
for _ in range(dist):
14+
new_p = p + dp[n % 4]
15+
points[c] = new_p
16+
p = new_p
17+
if c == inp:
18+
return int(abs(p.imag) + abs(p.real))
19+
c += 1
20+
21+
print(solve_pt1())
22+
23+
# Part 2
24+
def solve_pt2():
25+
dp = [1, -1j, -1, 1j]
26+
points = defaultdict(int)
27+
points[0] = 1
28+
p = 0
29+
for n in count():
30+
dist = int(((n + 2)/2) // 1)
31+
for _ in range(dist):
32+
new_p = p + dp[n % 4]
33+
points[new_p] = sum(points[new_p + dp2] for dp2 in (1, -1, 1j, -1j, 1+1j, 1-1j, -1+1j, -1-1j))
34+
if points[new_p] >= inp:
35+
return points[new_p]
36+
p = new_p
37+
38+
print(solve_pt2())

2017/day04/solution.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from itertools import combinations
2+
3+
with open("input") as f:
4+
inp = f.read().strip().split("\n")
5+
6+
passphrases = [line.split(" ") for line in inp]
7+
8+
# Part 1
9+
print(sum(1 for passphrase in passphrases if len(passphrase) == len(set(passphrase))))
10+
11+
# Part 2
12+
ans = 0
13+
for passphrase in passphrases:
14+
if any(set(word1) == set(word2) for word1, word2 in combinations(passphrase, 2)):
15+
continue
16+
ans += 1
17+
18+
print(ans)

2017/day05/solution.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from itertools import count
2+
3+
with open("input") as f:
4+
inp = f.read().strip().split("\n")
5+
6+
7+
def solve(part2 = False):
8+
jumps = list(map(int, inp))
9+
p = 0
10+
for c in count():
11+
if p >= len(jumps):
12+
return c
13+
jump = jumps[p]
14+
jumps[p] += 1
15+
if part2 and jump >= 3:
16+
jumps[p] -= 2
17+
p += jump
18+
19+
# Part 1
20+
print(solve())
21+
22+
# Part 2
23+
print(solve(True))

2017/day06/solution.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from itertools import count
2+
3+
with open("input") as f:
4+
inp = f.read().strip()
5+
6+
banks = tuple(map(int, inp.split(" ")))
7+
8+
9+
def redestribute(banks):
10+
banks = list(banks)
11+
n = len(banks)
12+
i = min(i for i, x in enumerate(banks) if x == max(banks))
13+
blocks = banks[i]
14+
banks[i] = 0
15+
for j in range(blocks):
16+
banks[(i + 1 + j) % n] += 1
17+
return tuple(banks)
18+
19+
20+
seen = {banks: 0}
21+
for c in count():
22+
banks = redestribute(banks)
23+
if banks in seen:
24+
print(c + 1)
25+
print(c + 1 - seen[banks])
26+
break
27+
seen[banks] = c + 1

2017/day07/solution.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
with open("input") as f:
2+
inp = f.read().strip().split("\n")
3+
4+
lookup = {}
5+
weight = {}
6+
for line in inp:
7+
first, *rest = line.split(" -> ")
8+
if rest:
9+
rest = rest[0].split(", ")
10+
first, w = first.split(" ")
11+
lookup[first] = rest
12+
weight[first] = int(w.strip("(").strip(")"))
13+
14+
# Part 1
15+
subtowers = {x for u in lookup.values() for x in u}
16+
print((set(lookup.keys()) - subtowers).pop())
17+
18+
# Part 2
19+
def total_weight(node):
20+
if not lookup[node]:
21+
return weight[node]
22+
return sum(total_weight(subnode) for subnode in lookup[node]) + weight[node]
23+
24+
25+
bottom = (set(lookup.keys()) - subtowers).pop()
26+
q = [bottom]
27+
while q:
28+
node = q.pop(0)
29+
weights = {subnode: total_weight(subnode) for subnode in lookup[node]}
30+
for subnode, w in weights.items():
31+
if list(weights.values()).count(w) == 1:
32+
unbl_node = subnode
33+
q.append(subnode)
34+
35+
for node, subnodes in lookup.items():
36+
if unbl_node in subnodes:
37+
target_weight = [total_weight(subnode) for subnode in subnodes if subnode != unbl_node][0]
38+
39+
print(weight[unbl_node] - (total_weight(unbl_node) - target_weight))

2017/day08/solution.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from collections import defaultdict
2+
3+
with open("input") as f:
4+
inp = f.read().strip()
5+
6+
reg = defaultdict(int)
7+
reg_max = 0
8+
for instr in inp.split("\n"):
9+
first, last = instr.split(" if ")
10+
target, op, val = first.split(" ")
11+
op = "+=" if op == "inc" else "-="
12+
rel_reg, rel_op, rel_val = last.split(" ")
13+
string = f"if reg[\"{rel_reg}\"] {rel_op} {rel_val}: reg[\"{target}\"] {op} {val}"
14+
exec(string)
15+
reg_max= max(max(reg.values()), reg_max)
16+
17+
# Part 1
18+
print(max(reg.values()))
19+
20+
# Part 2
21+
print(reg_max)

2017/day09/solution.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
with open("input") as f:
2+
inp = f.read().strip()
3+
4+
i = 0
5+
group_score = 0
6+
garbage = False
7+
groups = []
8+
c_garbage = 0
9+
while i < len(inp):
10+
c = inp[i]
11+
if c == "<" and not garbage:
12+
garbage = True
13+
c_garbage -= 1
14+
elif c == ">" and garbage:
15+
garbage = False
16+
elif c == "!" and garbage:
17+
i += 2
18+
continue
19+
elif c == "{" and not garbage:
20+
group_score += 1
21+
elif c == "}" and not garbage:
22+
groups.append(group_score)
23+
group_score -= 1
24+
if garbage:
25+
c_garbage += 1
26+
i += 1
27+
28+
# Part 1
29+
print(sum(groups))
30+
31+
# Part 2
32+
print(c_garbage)

2017/day10/solution.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from functools import reduce
2+
3+
with open("input") as f:
4+
inp = f.read().strip()
5+
6+
lengths = list(map(int, inp.split(",")))
7+
8+
# Part 1
9+
ls = list(range(256))
10+
n = len(ls)
11+
p = 0
12+
skip_size = 0
13+
for l in lengths:
14+
if p + l <= n:
15+
sub_ls = ls[p:(p+l)]
16+
ls = ls[:p] + sub_ls[::-1] + ls[p+l:]
17+
else:
18+
rev = ls[p:] + ls[:(l-(n-p))]
19+
rev = rev[::-1]
20+
rest = ls[(l-(n-p)):p]
21+
ls = rev[(n-p):] + rest + rev[:(n-p)]
22+
p += l + skip_size
23+
p %= n
24+
skip_size += 1
25+
26+
27+
print(ls[0]*ls[1])
28+
29+
# Part 2
30+
ascii_lengths = [ord(c) for c in inp]
31+
ascii_lengths += [17, 31, 73, 47, 23]
32+
33+
ls = list(range(256))
34+
n = len(ls)
35+
p = 0
36+
skip_size = 0
37+
for _ in range(64):
38+
for l in ascii_lengths:
39+
if p + l <= n:
40+
sub_ls = ls[p:(p+l)]
41+
ls = ls[:p] + sub_ls[::-1] + ls[p+l:]
42+
else:
43+
rev = ls[p:] + ls[:(l-(n-p))]
44+
rev = rev[::-1]
45+
rest = ls[(l-(n-p)):p]
46+
ls = rev[(n-p):] + rest + rev[:(n-p)]
47+
p += l + skip_size
48+
p %= n
49+
skip_size += 1
50+
51+
res = ""
52+
for i in range(0, 16):
53+
block = reduce(lambda x, y: x ^ y, ls[16*i:16*(i+1)])
54+
h = hex(block)[2:]
55+
if len(h) == 1:
56+
h = "0" + h
57+
res += h
58+
59+
print(res)

0 commit comments

Comments
 (0)