Skip to content

Commit 829206b

Browse files
committed
2020 Day 17
1 parent ccfea58 commit 829206b

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

2020/17.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#.##....
2+
.#.#.##.
3+
###.....
4+
....##.#
5+
#....###
6+
.#.#.#..
7+
.##...##
8+
#..#.###

2020/17_test.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.#.
2+
..#
3+
###

2020/17a.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from aocd import submit
2+
from aoc import *
3+
from collections import defaultdict
4+
import re
5+
6+
7+
FILE = "17_test.txt"
8+
FILE = "17.txt"
9+
10+
11+
def print_slice(cube, z, start, size):
12+
for i in range(start, start + size):
13+
for j in range(start, start + size):
14+
print(cube[(j, i, z)], end="")
15+
print()
16+
print()
17+
18+
19+
def around_active(cube, x, y, z):
20+
active = 0
21+
for i in range(y - 1, y + 2):
22+
for j in range(x - 1, x + 2):
23+
for k in range(z - 1, z + 2):
24+
if i == y and j == x and k == z:
25+
continue
26+
if cube[(j, i, k)] == "#":
27+
active += 1
28+
return active
29+
30+
31+
def process(cube, start, size):
32+
new_cube = defaultdict(lambda: ".")
33+
for i in range(start, start + size):
34+
for j in range(start, start + size):
35+
for z in range(start, start + size):
36+
if cube[(j, i, z)] == "#" and around_active(cube, j, i, z) in (2, 3):
37+
new_cube[(j, i, z)] = "#"
38+
elif cube[(j, i, z)] == "." and around_active(cube, j, i, z) == 3:
39+
new_cube[(j, i, z)] = "#"
40+
return (new_cube, start - 1, size + 2)
41+
42+
43+
def main():
44+
inp = load_map(FILE)
45+
cubes = defaultdict(lambda: ".")
46+
for i, y in enumerate(inp):
47+
for j, x in enumerate(y):
48+
cubes[(j, i, 0)] = x
49+
start = -1
50+
size = 5
51+
for x in range(6):
52+
# if x == 1:
53+
# [print_slice(cubes, s, -1, 4) for s in range(-1, 2)]
54+
cubes, start, size = process(cubes, start, size)
55+
out = len(list(x for x in cubes.values() if x == "#"))
56+
print(out)
57+
return
58+
input()
59+
print("submitting")
60+
submit(out)
61+
62+
63+
main()

2020/17b.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from aocd import submit
2+
from aoc import *
3+
from collections import defaultdict
4+
import re
5+
6+
7+
FILE = "17_test.txt"
8+
FILE = "17.txt"
9+
10+
11+
def print_slice(cube, z, start, size):
12+
for i in range(start, start + size):
13+
for j in range(start, start + size):
14+
print(cube[(j, i, z)], end="")
15+
print()
16+
print()
17+
18+
19+
def around_active(cube, x, y, z, w):
20+
active = 0
21+
for i in range(y - 1, y + 2):
22+
for j in range(x - 1, x + 2):
23+
for k in range(z - 1, z + 2):
24+
for l in range(w - 1, w + 2):
25+
if i == y and j == x and k == z and l == w:
26+
continue
27+
if cube[(j, i, k, l)] == "#":
28+
active += 1
29+
return active
30+
31+
32+
def process(cube, start, size):
33+
new_cube = defaultdict(lambda: ".")
34+
for i in range(start, start + size):
35+
for j in range(start, start + size):
36+
for z in range(start, start + size):
37+
for w in range(start, start + size):
38+
if cube[(j, i, z, w)] == "#" and around_active(
39+
cube, j, i, z, w
40+
) in (2, 3):
41+
new_cube[(j, i, z, w)] = "#"
42+
elif (
43+
cube[(j, i, z, w)] == "."
44+
and around_active(cube, j, i, z, w) == 3
45+
):
46+
new_cube[(j, i, z, w)] = "#"
47+
return (new_cube, start - 1, size + 2)
48+
49+
50+
def main():
51+
inp = load_map(FILE)
52+
cubes = defaultdict(lambda: ".")
53+
for i, y in enumerate(inp):
54+
for j, x in enumerate(y):
55+
cubes[(j, i, 0, 0)] = x
56+
start = -1
57+
size = len(inp) + 2
58+
for x in range(6):
59+
cubes, start, size = process(cubes, start, size)
60+
out = len(list(x for x in cubes.values() if x == "#"))
61+
print(out)
62+
return
63+
input()
64+
print("submitting")
65+
submit(out)
66+
67+
68+
main()

0 commit comments

Comments
 (0)