|
1 | 1 | from knot_hash import create_knot_hash
|
2 | 2 |
|
3 | 3 | puzzle_input = 'hxtvlmkl'
|
4 |
| -num = 8 |
5 |
| -array = [puzzle_input + '-' + str(i) for i in xrange(num)] |
6 |
| -output = [] |
7 |
| -regions = 0 |
| 4 | + |
| 5 | +output = [ |
| 6 | + [1, 1, 0, 1, 0, 1, 0, 0], |
| 7 | + [0, 1, 0, 1, 0, 1, 0, 1], |
| 8 | + [0, 0, 0, 0, 1, 0, 1, 0], |
| 9 | + [1, 0, 1, 0, 1, 1, 0, 1], |
| 10 | + [0, 1, 1, 0, 1, 0, 0, 0], |
| 11 | + [1, 1, 0, 0, 1, 0, 0, 1], |
| 12 | + [0, 1, 0, 0, 0, 1, 0, 0], |
| 13 | + [1, 1, 0, 1, 0, 1, 1, 0] |
| 14 | +] |
| 15 | + # [1, 1, 0, 2, 0, 3, 0, 0], |
| 16 | + # [0, 1, 0, 2, 0, 3, 0, 4], |
| 17 | + # [0, 0, 0, 0, 5, 0, 6, 0], |
| 18 | + # [10, 0, 11, 0, 5, 5, 0, 7], |
| 19 | + # [0, 11, 11, 0, 5, 0, 0, 0], |
| 20 | + # [11, 11, 0, 0, 5, 0, 0, 8], |
| 21 | + # [0, 11, 0, 0, 0, 9, 0, 0], |
| 22 | + # [11, 11, 0, 12, 0, 9, 9, 0] |
8 | 23 |
|
9 | 24 | hex_to_bit = {
|
10 | 25 | '0': [0, 0, 0, 0],
|
|
26 | 41 | }
|
27 | 42 |
|
28 | 43 |
|
29 |
| -def is_edge(i, j): |
30 |
| - return i == 0 or j == 0 or i == (num-1) or j == (num-1) |
31 |
| - |
32 |
| - |
33 |
| -def add_regions(i, j): |
34 |
| - global regions |
35 |
| - regions += 1 |
36 |
| - |
37 |
| -# for inp in array: |
38 |
| -# hexadecimal = create_knot_hash(inp) |
39 |
| -# result = [] |
40 |
| -# for char in hexadecimal: |
41 |
| -# for num in hex_to_bit[char]: |
42 |
| -# result.append(num) |
43 |
| -# output.append(result) |
44 |
| - |
45 |
| -output = [ |
46 |
| - [1, 1, 0, 1, 0, 1, 0, 0], |
47 |
| - [0, 1, 0, 1, 0, 1, 0, 1], |
48 |
| - [0, 0, 0, 0, 1, 0, 1, 0], |
49 |
| - [1, 0, 1, 0, 1, 1, 0, 1], |
50 |
| - [0, 1, 1, 0, 1, 0, 0, 0], |
51 |
| - [1, 1, 0, 0, 1, 0, 0, 1], |
52 |
| - [0, 1, 0, 0, 0, 1, 0, 0], |
53 |
| - [1, 1, 0, 1, 0, 1, 1, 0] |
54 |
| -] |
55 |
| - |
56 |
| -for i, array in enumerate(output): |
57 |
| - for j, num in enumerate(array): |
58 |
| - |
59 |
| - if num == 1: |
60 |
| - print i, j |
61 |
| - |
62 |
| - # if is_edge(i, j): |
63 |
| - |
64 |
| - if i == 0: |
65 |
| - down = output[i][j+1] |
66 |
| - if j == 0: |
67 |
| - right = output[i+1][j] |
68 |
| - if right == 0 and down == 0: |
69 |
| - add_regions(i, j) |
70 |
| - |
71 |
| - elif j == (num-1): |
72 |
| - left = output[i][j-1] |
73 |
| - if left == 0 and down == 0: |
74 |
| - add_regions(i, j) |
75 |
| - |
76 |
| - else: |
77 |
| - left = output[i][j-1] |
78 |
| - right = output[i+1][j] |
79 |
| - if left == 0 and down == 0 and right == 0: |
80 |
| - add_regions(i, j) |
81 |
| - |
82 |
| - elif i == (num-1): |
83 |
| - up = output[i-1][j] |
84 |
| - if j == 0: |
85 |
| - right = output[i][j+1] |
86 |
| - if right == 0 and up == 0: |
87 |
| - add_regions(i, j) |
88 |
| - |
89 |
| - elif j == (num-1): |
90 |
| - left = output[i][j-1] |
91 |
| - if left == 0 and up == 0: |
92 |
| - add_regions(i, j) |
93 |
| - |
94 |
| - else: |
95 |
| - left = output[i][j+1] |
96 |
| - right = output[i][j-1] |
97 |
| - if left == 0 and right == 0 and up == 0: |
98 |
| - add_regions(i, j) |
99 |
| - |
100 |
| - elif j == 0: |
101 |
| - down = output[i][j+1] |
102 |
| - if i == 0: |
103 |
| - right = output[i+1][j] |
104 |
| - if right == 0 and down == 0: |
105 |
| - add_regions(i, j) |
106 |
| - |
107 |
| - elif i == (num-1): |
108 |
| - left = output[i-1][j] |
109 |
| - if left == 0 and down == 0: |
110 |
| - add_regions(i, j) |
111 |
| - |
112 |
| - else: |
113 |
| - left = output[i-1][j] |
114 |
| - right = output[i+1][j] |
115 |
| - if left == 0 and down == 0 and right == 0: |
116 |
| - add_regions(i, j) |
117 |
| - |
118 |
| - elif j == (num-1): |
119 |
| - up = output[i][j-1] |
120 |
| - if i == 0: |
121 |
| - right = output[i+1][j] |
122 |
| - if right == 0 and up == 0: |
123 |
| - add_regions(i, j) |
124 |
| - |
125 |
| - elif i == (num-1): |
126 |
| - left = output[i-1][j] |
127 |
| - if left == 0 and up == 0: |
128 |
| - add_regions(i, j) |
129 |
| - |
130 |
| - else: |
131 |
| - left = output[i-1][j] |
132 |
| - right = output[i+1][j] |
133 |
| - if left == 0 and right == 0 and up == 0: |
134 |
| - add_regions(i, j) |
135 |
| - |
136 |
| - else: |
137 |
| - left = output[i-1][j] |
138 |
| - right = output[i+1][j] |
139 |
| - up = output[i][j-1] |
140 |
| - down = output[i][j+1] |
141 |
| - if left == 0 and right == 0 and up == 0 and down == 0: |
142 |
| - add_regions(i, j) |
143 |
| - |
144 |
| -print regions |
| 44 | +def create_output(puzzle_input): |
| 45 | + output = [] |
| 46 | + num = 128 |
| 47 | + array = [puzzle_input + '-' + str(i) for i in xrange(num)] |
| 48 | + |
| 49 | + for inp in array: |
| 50 | + hexadecimal = create_knot_hash(inp) |
| 51 | + result = [] |
| 52 | + for char in hexadecimal: |
| 53 | + for num in hex_to_bit[char]: |
| 54 | + result.append(num) |
| 55 | + output.append(result) |
| 56 | + return output |
| 57 | + |
| 58 | + |
| 59 | +def transform(output): |
| 60 | + """ |
| 61 | + 0100 |
| 62 | + 1101 |
| 63 | + 0110 |
| 64 | + becomes |
| 65 | + 1->5 |
| 66 | + 4->5 |
| 67 | + 5->1,4,9 |
| 68 | + 7->7 |
| 69 | + 9->5,10 |
| 70 | + 10->9 |
| 71 | + """ |
| 72 | + num = 0 |
| 73 | + group_to_value = {} |
| 74 | + for x_i, x in enumerate(output): |
| 75 | + for y_i, y in enumerate(x): |
| 76 | + if y == 1: |
| 77 | + group_to_value[num] = find_connections(output, x_i, y_i, num) |
| 78 | + num += 1 |
| 79 | + return group_to_value |
| 80 | + |
| 81 | + |
| 82 | +def find_connections(output, x, y, num): |
| 83 | + connections = [] |
| 84 | + size = len(output) |
| 85 | + try: |
| 86 | + if output[x-1][y] == 1 and x - 1 >= 0: |
| 87 | + connections.append(num-size) |
| 88 | + except IndexError: |
| 89 | + pass |
| 90 | + |
| 91 | + try: |
| 92 | + if output[x+1][y] == 1: |
| 93 | + connections.append(num+size) |
| 94 | + except IndexError: |
| 95 | + pass |
| 96 | + |
| 97 | + try: |
| 98 | + if output[x][y-1] == 1 and y - 1 >= 0: |
| 99 | + connections.append(num-1) |
| 100 | + except IndexError: |
| 101 | + pass |
| 102 | + |
| 103 | + try: |
| 104 | + if output[x][y+1]: |
| 105 | + connections.append(num+1) |
| 106 | + except IndexError: |
| 107 | + pass |
| 108 | + |
| 109 | + if connections == []: |
| 110 | + return [num] |
| 111 | + return connections |
| 112 | + |
| 113 | + |
| 114 | +def calculate(dictionary): |
| 115 | + """ |
| 116 | + using hint from reddit, using algorithm from day 12 |
| 117 | + """ |
| 118 | + num_groups = 0 |
| 119 | + contains = [] |
| 120 | + |
| 121 | + for i in range(128*128): |
| 122 | + try: |
| 123 | + dictionary[i] |
| 124 | + if i not in contains: |
| 125 | + num_groups += 1 |
| 126 | + |
| 127 | + contains_num = [i] |
| 128 | + for j in range(50): |
| 129 | + for group, array in dictionary.iteritems(): |
| 130 | + if i != group: |
| 131 | + if i in array: |
| 132 | + contains_num.append(group) |
| 133 | + if group in contains_num: |
| 134 | + for value in array: |
| 135 | + contains_num.append(value) |
| 136 | + for num in set(contains_num): |
| 137 | + contains.append(num) |
| 138 | + except KeyError: |
| 139 | + pass |
| 140 | + |
| 141 | + return num_groups |
| 142 | + |
| 143 | +print calculate(transform(create_output(puzzle_input))) |
0 commit comments