Skip to content

Commit

Permalink
Day 7, 8, 9, 10 & 11
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleysigma committed Dec 11, 2020
1 parent 212b598 commit a316576
Show file tree
Hide file tree
Showing 10 changed files with 2,630 additions and 0 deletions.
26 changes: 26 additions & 0 deletions day10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import aoc
from collections import defaultdict
data = aoc.intlist(10, set)

d = defaultdict(int, {0:1})
for i in sorted(data):
d[i] = d[i-1] + d[i-2] + d[i-3]

print((lambda x: (x+1)*(len(data)-x))(sum(j+1 in data for j in data)),
d[max(d.keys())])

aoc.tock("ms")

##import aoc
##data = {0} | aoc.intlist(10, set)
##
##d = {i: 0 for i in range(6)}
##p = max(data)
##while p > 0:
## n = max({p-i for i in range(6)} - data)
## d[p-n] += 1
## p = n - 2
##print((lambda x: x * (len(data)-x))(sum(j+1 in data for j in data)),
## 2**d[3] * 4**d[4] * 7**d[5])
##
##aoc.tock("ms")
52 changes: 52 additions & 0 deletions day11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import aoc
from collections import defaultdict
data = aoc.strlist(11)

h = len(data)
w = len(data[0])
p = defaultdict(lambda: ".")
q = defaultdict(lambda: ".")
for i in range(w):
for j in range(h):
p[(i,j)] = data[j][i]
q[(i,j)] = data[j][i]

while True:
pp = defaultdict(lambda: ".")
for i in range(w):
for j in range(h):
n = [p[(i+u,j+v)] for u,v in ((1,1), (1,0), (1,-1), (0,-1), (-1,-1), (-1,0), (-1,1), (0,1))].count("#")
if p[(i,j)] == "L" and n == 0:
pp[(i,j)] = "#"
elif p[(i,j)] == "#" and n >= 4:
pp[(i,j)] = "L"
else:
pp[(i,j)] = p[(i,j)]
if all(p[k] == pp[k] for k in p.keys()): break
p = pp

while True:
qq = defaultdict(lambda: ".")
for i in range(w):
for j in range(h):
if q[(i,j)] == ".": continue
n = 0
for u,v in ((1,1), (1,0), (1,-1), (0,-1), (-1,-1), (-1,0), (-1,1), (0,1)):
k = 1
while (0 <= i+k*u < w) and (0 <= j+k*v < h):
if q[(i+k*u,j+k*v)] != ".":
n += int(q[(i+k*u,j+k*v)] == "#")
break
k += 1
if q[(i,j)] == "L" and n == 0:
qq[(i,j)] = "#"
elif q[(i,j)] == "#" and n >= 5:
qq[(i,j)] = "L"
else:
qq[(i,j)] = q[(i,j)]
if all(q[k] == qq[k] for k in q.keys()): break
q = qq

print(list(p.values()).count("#"), list(q.values()).count("#"))

aoc.tock()
38 changes: 38 additions & 0 deletions day7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import aoc
data = aoc.strlist(7)

from collections import deque

bags = {}
for i in data:
outer, inner = i.rstrip(".").split(" contain ")
outer = outer.rstrip("s")
bags[outer] = []
if inner == "no other bags":
continue
for j in inner.split(", "):
n, col = j.split(" ", 1)
col = col.rstrip("s")
n = int(n)
bags[outer].append((n, col))

contain = set()
queue = deque(["shiny gold bag"])
while queue:
i = queue.pop()
for outer, inner in bags.items():
for j in inner:
if j[1] == i:
contain.add(outer)
queue.append(outer)

queue = deque([(1, "shiny gold bag")])
count = 0
while queue:
r, i = queue.pop()
for j in bags[i]:
count += r*j[0]
queue.append((r*j[0], j[1]))

print(len(contain), count)
aoc.tock("ms")
55 changes: 55 additions & 0 deletions day8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import aoc
data = aoc.strlist(8)

instruc = [(i.split(" ")[0], int(i.split(" ")[1])) for i in data]

acc = 0
point = 0
seen = set()
while point not in seen:
seen.add(point)
i, j = instruc[point]
if i == "jmp":
point += j
elif i == "acc":
acc += j
point += 1
elif i == "nop":
point += 1
else:
print(i)
point += 1
print(acc)

for k in range(len(instruc)):
if instruc[k][0] == "acc": continue
if instruc[k][1] in [0, 1]: continue
alter = list(instruc)
if alter[k][0] == "jmp":
alter[k] = ("nop", alter[k][1])
elif alter[k][0] == "nop":
alter[k] = ("jmp", alter[k][1])
else:
print(alter[k])

acc = 0
point = 0
seen = set()
while point not in seen:
seen.add(point)
i, j = alter[point]
if i == "jmp":
point += j
elif i == "acc":
acc += j
point += 1
elif i == "nop":
point += 1
else:
print(i)
point += 1
if point >= len(alter):
print(acc)
break

aoc.tock("ms")
21 changes: 21 additions & 0 deletions day9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import aoc
data = aoc.intlist(9)

for i in range(25, len(data)):
for j in range(i-25, i):
if data[i]-data[j] in data[j:i]:
break
else:
n = data[i]
break

for i in range(len(data)):
m = n
j = 0
while m > 0:
m -= data[i+j]
j += 1
if m == 0 and j > 1:
print(n, max(data[i:i+j]) + min(data[i:i+j]))

aoc.tock("ms")
110 changes: 110 additions & 0 deletions input10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
160
34
123
159
148
93
165
56
179
103
171
44
110
170
147
98
25
37
137
71
5
6
121
28
19
134
18
7
66
90
88
181
89
41
156
46
8
61
124
9
161
72
13
172
111
59
105
51
109
27
152
117
52
68
95
164
116
75
78
180
81
47
104
12
133
175
16
149
135
99
112
38
67
53
153
2
136
113
17
145
106
31
45
169
146
168
26
36
118
62
65
142
130
1
140
84
94
141
122
22
48
102
60
178
127
73
74
87
182
35
Loading

0 comments on commit a316576

Please sign in to comment.