Skip to content

Commit 87197f7

Browse files
committed
Lazy copypasta d17 p1&2 solution
1 parent 5b2f19d commit 87197f7

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed

17/cube_pocket_dimension.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import logging
2+
3+
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
4+
logging.disable(logging.FATAL)
5+
6+
def load_cubes(filename):
7+
active_cubes = set()
8+
lines = []
9+
with open(filename, "r") as f:
10+
for line in f:
11+
lines.append(line.rstrip())
12+
z = 0
13+
for y, row in enumerate(lines):
14+
for x, col in enumerate(row):
15+
if col == "#":
16+
active_cubes.add((x, y, z))
17+
return active_cubes
18+
19+
# there is no (0, 0, 0) here :)
20+
relative_neighbours = ((-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1),
21+
(-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0),
22+
(-1, 1, 1), ( 0, -1, -1), ( 0, -1, 0), ( 0, -1, 1),
23+
( 0, 0, -1), ( 0, 0, 1), ( 0, 1, -1), ( 0, 1, 0),
24+
( 0, 1, 1), ( 1, -1, -1), ( 1, -1, 0), ( 1, -1, 1),
25+
( 1, 0, -1), ( 1, 0, 0), ( 1, 0, 1), ( 1, 1, -1),
26+
( 1, 1, 0), ( 1, 1, 1))
27+
28+
def get_neighbours(point):
29+
logging.debug(point)
30+
return tuple(map(lambda rel : (rel[0] + point[0],
31+
rel[1] + point[1],
32+
rel[2] + point[2]),
33+
relative_neighbours))
34+
35+
# only neighbours and currently active guys are of intrest
36+
def get_points_of_intrest(active_cubes):
37+
points_of_intrest = active_cubes.copy()
38+
logging.debug("old active cubes: %s", points_of_intrest)
39+
neighs_set = set()
40+
for point in points_of_intrest:
41+
neighs_set.update(get_neighbours(point))
42+
logging.debug("new neighbours: %s", neighs_set)
43+
points_of_intrest.update(neighs_set)
44+
logging.debug("final points of intrest: %s" % points_of_intrest)
45+
return points_of_intrest
46+
47+
def count_neighbours(points_of_intrest, active_cubes):
48+
neighbour_counts = {}
49+
logging.debug("count neighbours POIs: %s" % points_of_intrest)
50+
for point in points_of_intrest:
51+
act_neighs = sum(map(lambda x: x in active_cubes, get_neighbours(point)))
52+
neighbour_counts[point] = act_neighs
53+
return neighbour_counts
54+
55+
def new_cubes(points_of_intrest, active_cubes, neighbour_counts):
56+
new_active = set()
57+
logging.debug("new cubes POIs: %s" % points_of_intrest)
58+
for point in points_of_intrest:
59+
if point in active_cubes:
60+
if neighbour_counts[point] in (2, 3):
61+
new_active.add(point)
62+
else:
63+
if neighbour_counts[point] == 3:
64+
new_active.add(point)
65+
return new_active
66+
67+
68+
active_cubes = load_cubes("test_input")
69+
logging.debug("active cubes: %s" % active_cubes)
70+
71+
for i in range(6):
72+
points_of_intrest = get_points_of_intrest(active_cubes)
73+
neighbour_counts = count_neighbours(points_of_intrest, active_cubes)
74+
active_cubes = new_cubes(points_of_intrest, active_cubes, neighbour_counts)
75+
76+
print(len(active_cubes))
77+
78+
79+

17/cube_pocket_dimension_4d.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import logging
2+
3+
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
4+
logging.disable(logging.FATAL)
5+
6+
def load_cubes(filename):
7+
active_cubes = set()
8+
lines = []
9+
with open(filename, "r") as f:
10+
for line in f:
11+
lines.append(line.rstrip())
12+
z = 0
13+
w = 0
14+
for y, row in enumerate(lines):
15+
for x, col in enumerate(row):
16+
if col == "#":
17+
active_cubes.add((x, y, z, w))
18+
return active_cubes
19+
20+
def get_relative_neigbours():
21+
points = set()
22+
for x in range(-1, 2):
23+
for y in range(-1, 2):
24+
for z in range(-1, 2):
25+
for w in range(-1, 2):
26+
points.add((x, y, z , w))
27+
points.remove((0, 0, 0, 0))
28+
return points
29+
30+
relative_neighbours = get_relative_neigbours()
31+
32+
def get_neighbours(point):
33+
logging.debug(point)
34+
return tuple(map(lambda rel : (rel[0] + point[0],
35+
rel[1] + point[1],
36+
rel[2] + point[2],
37+
rel[3] + point[3]),
38+
relative_neighbours))
39+
40+
# only neighbours and currently active guys are of intrest
41+
def get_points_of_intrest(active_cubes):
42+
points_of_intrest = active_cubes.copy()
43+
logging.debug("old active cubes: %s", points_of_intrest)
44+
neighs_set = set()
45+
for point in points_of_intrest:
46+
neighs_set.update(get_neighbours(point))
47+
logging.debug("new neighbours: %s", neighs_set)
48+
points_of_intrest.update(neighs_set)
49+
logging.debug("final points of intrest: %s" % points_of_intrest)
50+
return points_of_intrest
51+
52+
def count_neighbours(points_of_intrest, active_cubes):
53+
neighbour_counts = {}
54+
logging.debug("count neighbours POIs: %s" % points_of_intrest)
55+
for point in points_of_intrest:
56+
act_neighs = sum(map(lambda x: x in active_cubes, get_neighbours(point)))
57+
neighbour_counts[point] = act_neighs
58+
return neighbour_counts
59+
60+
def new_cubes(points_of_intrest, active_cubes, neighbour_counts):
61+
new_active = set()
62+
logging.debug("new cubes POIs: %s" % points_of_intrest)
63+
for point in points_of_intrest:
64+
if point in active_cubes:
65+
if neighbour_counts[point] in (2, 3):
66+
new_active.add(point)
67+
else:
68+
if neighbour_counts[point] == 3:
69+
new_active.add(point)
70+
return new_active
71+
72+
73+
active_cubes = load_cubes("input")
74+
logging.debug("active cubes: %s" % active_cubes)
75+
76+
for i in range(6):
77+
points_of_intrest = get_points_of_intrest(active_cubes)
78+
neighbour_counts = count_neighbours(points_of_intrest, active_cubes)
79+
active_cubes = new_cubes(points_of_intrest, active_cubes, neighbour_counts)
80+
81+
print(len(active_cubes))
82+
83+
84+

17/input

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#.##....
2+
.#.#.##.
3+
###.....
4+
....##.#
5+
#....###
6+
.#.#.#..
7+
.##...##
8+
#..#.###

17/test_input

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.#.
2+
..#
3+
###

0 commit comments

Comments
 (0)