Browse Source

Day 21

master
Chris Smith 6 years ago
parent
commit
334024fba0
2 changed files with 159 additions and 0 deletions
  1. 51
    0
      21.py
  2. 108
    0
      data/21.txt

+ 51
- 0
21.py View File

@@ -0,0 +1,51 @@
1
+import numpy as np
2
+
3
+with open('data/21.txt') as file:
4
+    mappings = dict()
5
+    for line in file.readlines():
6
+        parts = tuple(map(lambda p: np.array(list(map(lambda r: [c == '#' for c in r], p.split('/')))),
7
+                          line.strip().split(' => ')))
8
+
9
+        # All possible transformations of our mappings:
10
+        #  - 0, 90, 180 and 270 degree rotations
11
+        #  - vertical (up<>down) flip followed by the 4 rotations
12
+        #  - horizontal (left<>right) flip followed by the 4 rotations
13
+        # ...Some of these may be equivalent...
14
+        keys = [
15
+            *(np.rot90(parts[0], k) for k in range(3)),
16
+            *(np.rot90(np.flipud(parts[0]), k) for k in range(3)),
17
+            *(np.rot90(np.fliplr(parts[0]), k) for k in range(3)),
18
+        ]
19
+
20
+        for key in keys:
21
+            mappings[key.tobytes()] = parts[1]
22
+
23
+
24
+    def expand(old_grid, length, oss, nss):
25
+        """
26
+        Expands a grid by replacing all segments with their mapped versions.
27
+
28
+        :param old_grid: The grid to expand
29
+        :param length: The current size of the grid (length in any direction)
30
+        :param oss: The size of the segment to be replaced (old segment size)
31
+        :param nss: The size of the segment that will replace it (new segment size)
32
+        :return: A new grid, with each segment expanded
33
+        """
34
+        new_grid = np.empty((length // oss * nss, length // oss * nss), dtype=bool)
35
+        for x in range(0, length, oss):
36
+            for y in range(0, length, oss):
37
+                new_x = x // oss * nss
38
+                new_y = y // oss * nss
39
+                new_grid[new_x:new_x+nss, new_y:new_y+nss] = mappings[old_grid[x:x + oss, y:y + oss].tobytes()]
40
+        return new_grid
41
+
42
+
43
+    grid = np.array([[False, True, False], [False, False, True], [True, True, True]])
44
+    for i in range(18):
45
+        size = len(grid)
46
+        grid = expand(grid, size, 2, 3) if size % 2 == 0 else expand(grid, size, 3, 4)
47
+
48
+        if i == 4:
49
+            print(f'Part one: {sum(sum(grid))}')
50
+        elif i == 17:
51
+            print(f'Part two: {sum(sum(grid))}')

+ 108
- 0
data/21.txt View File

@@ -0,0 +1,108 @@
1
+../.. => ##./##./.##
2
+#./.. => .../.#./##.
3
+##/.. => .../.##/#.#
4
+.#/#. => ##./#../#..
5
+##/#. => .##/#.#/#..
6
+##/## => ..#/.#./.##
7
+.../.../... => #.../.##./...#/#...
8
+#../.../... => ...#/..../..#./..##
9
+.#./.../... => ..../.##./###./....
10
+##./.../... => ###./#.##/..#./..#.
11
+#.#/.../... => #.../.#../#..#/..#.
12
+###/.../... => ..##/.##./#.../....
13
+.#./#../... => #.##/..../..../#.##
14
+##./#../... => .#.#/.#.#/##../.#..
15
+..#/#../... => .###/####/.###/##..
16
+#.#/#../... => ..../.#.#/..../####
17
+.##/#../... => .##./##.#/.###/#..#
18
+###/#../... => ####/...#/###./.###
19
+.../.#./... => ..##/#..#/###./###.
20
+#../.#./... => ###./..##/.#.#/.#.#
21
+.#./.#./... => ..#./..#./##.#/##..
22
+##./.#./... => #..#/###./..#./#.#.
23
+#.#/.#./... => .###/#.../.#.#/.##.
24
+###/.#./... => #.##/##../#.#./...#
25
+.#./##./... => #.##/#.##/#.##/.###
26
+##./##./... => ..##/#..#/.###/....
27
+..#/##./... => #..#/.##./##../####
28
+#.#/##./... => ###./###./..##/..##
29
+.##/##./... => ###./##.#/.##./###.
30
+###/##./... => ##../#..#/##../....
31
+.../#.#/... => ##.#/..#./..##/##..
32
+#../#.#/... => #..#/.###/.#../#.#.
33
+.#./#.#/... => ####/#.##/.###/###.
34
+##./#.#/... => #.../####/...#/.#.#
35
+#.#/#.#/... => ...#/.#.#/#..#/#.##
36
+###/#.#/... => ###./#.##/##.#/..##
37
+.../###/... => ..../##.#/.#../..##
38
+#../###/... => ####/..##/.##./.###
39
+.#./###/... => #.#./#.#./#.../#..#
40
+##./###/... => #..#/..##/#.##/#.#.
41
+#.#/###/... => .##./##.#/.#../####
42
+###/###/... => ####/##.#/.#../#.#.
43
+..#/.../#.. => #..#/#.##/.###/.###
44
+#.#/.../#.. => .##./#.../.#.#/....
45
+.##/.../#.. => .#.#/.#.#/##../####
46
+###/.../#.. => .#.#/.##./####/##.#
47
+.##/#../#.. => .###/.###/.###/#...
48
+###/#../#.. => ..##/#.../#.#./..#.
49
+..#/.#./#.. => #.#./##../##../####
50
+#.#/.#./#.. => ..../..##/#..#/..#.
51
+.##/.#./#.. => #.##/#..#/##.#/.##.
52
+###/.#./#.. => ...#/#.../#.#./.#..
53
+.##/##./#.. => .##./#..#/.##./...#
54
+###/##./#.. => ##.#/##.#/.##./...#
55
+#../..#/#.. => ##../..#./..#./#.#.
56
+.#./..#/#.. => #.#./##../#..#/#.##
57
+##./..#/#.. => #.##/###./###./.#.#
58
+#.#/..#/#.. => ..../...#/...#/#..#
59
+.##/..#/#.. => #..#/#.#./..##/.##.
60
+###/..#/#.. => ##../.#.#/.#../#.#.
61
+#../#.#/#.. => ####/.##./.##./.##.
62
+.#./#.#/#.. => ...#/.##./..#./.##.
63
+##./#.#/#.. => .#.#/.##./..#./.#.#
64
+..#/#.#/#.. => .#../##.#/##../#...
65
+#.#/#.#/#.. => .#.#/..#./#.../##..
66
+.##/#.#/#.. => ..#./#.#./###./#...
67
+###/#.#/#.. => ..../#.#./..##/##.#
68
+#../.##/#.. => .##./##../.#../..##
69
+.#./.##/#.. => ##../#.#./#.../####
70
+##./.##/#.. => ###./###./#.#./..##
71
+#.#/.##/#.. => ...#/#..#/..#./###.
72
+.##/.##/#.. => ..##/####/..../#.##
73
+###/.##/#.. => .#.#/#.../.##./#...
74
+#../###/#.. => ..#./.#.#/#..#/.##.
75
+.#./###/#.. => ####/..../####/#.##
76
+##./###/#.. => .###/..../#.#./####
77
+..#/###/#.. => ###./#.#./.#.#/#...
78
+#.#/###/#.. => #.#./#.#./..##/.##.
79
+.##/###/#.. => #.##/.###/.##./#.##
80
+###/###/#.. => #..#/.#../.#../.##.
81
+.#./#.#/.#. => .#../.##./##../..##
82
+##./#.#/.#. => .##./#.##/...#/#.#.
83
+#.#/#.#/.#. => ##.#/###./#.#./..#.
84
+###/#.#/.#. => ..../##../.###/###.
85
+.#./###/.#. => .#.#/.###/..../#..#
86
+##./###/.#. => #.../..#./#..#/.#..
87
+#.#/###/.#. => .#../##.#/##.#/.###
88
+###/###/.#. => #..#/.#.#/#.#./..#.
89
+#.#/..#/##. => .#../.###/...#/#.##
90
+###/..#/##. => ...#/...#/..##/...#
91
+.##/#.#/##. => #.#./###./.##./####
92
+###/#.#/##. => #.#./...#/...#/....
93
+#.#/.##/##. => ###./#.../##.#/..#.
94
+###/.##/##. => .#../#.../.###/.#..
95
+.##/###/##. => #.../..#./..#./.###
96
+###/###/##. => .#../.#../####/###.
97
+#.#/.../#.# => ##.#/##../...#/##.#
98
+###/.../#.# => ###./###./#..#/###.
99
+###/#../#.# => .###/..#./.#../#...
100
+#.#/.#./#.# => ##.#/.##./.#.#/##.#
101
+###/.#./#.# => ...#/...#/#.##/.##.
102
+###/##./#.# => #.../##../#.../....
103
+#.#/#.#/#.# => ####/.#../..##/..##
104
+###/#.#/#.# => ##../####/#.##/..##
105
+#.#/###/#.# => ##../..../..../####
106
+###/###/#.# => .#../.#.#/.###/.#.#
107
+###/#.#/### => ##../####/###./...#
108
+###/###/### => ###./#..#/##../.##.

Loading…
Cancel
Save