-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd21_1_2.php
142 lines (110 loc) · 3.01 KB
/
d21_1_2.php
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
function join_matrix(array $matrix, bool $print = false)
{
$out = '';
foreach ($matrix as $row) {
$out .= implode('', $row);
if ($print === true) $out .= PHP_EOL;
}
return $out;
}
function expand_matrix(&$matrix, $rules)
{
$subs = divide_matrix($matrix);
$new_subs = [];
// Check each of the divided matrices
foreach ($subs as $sub) {
$str = join_matrix($sub);
// rotate 3 times. if still not match then flip
// by its nature, 2x2 matrix will NOT need to flip
$rotate = 0;
while (!array_key_exists($str, $rules)) {
if ($rotate === 3) {
// Need to flip
$sub = flip_matrix3($sub);
$rotate = 0;
} else {
$sub = rotate_matrix($sub);
$rotate++;
}
$str = join_matrix($sub);
}
array_push($new_subs, $rules[$str]);
}
// Join the new sub matrices
$matrix = join_sub_matrices($new_subs);
}
function divide_matrix($matrix)
{
$size = count($matrix);
$subs = [];
$sub_size = $size % 2 === 0 ? 2 : 3;
for ($x = 0; $x < ($size/$sub_size); $x++) {
for ($y = 0; $y < ($size/$sub_size); $y++) {
$sub = [];
for ($i = 0; $i < $sub_size; $i++) {
for ($j = 0; $j < $sub_size; $j++) {
$sub[$i][$j] = $matrix[$i + $x*$sub_size][$j + $y*$sub_size];
}
}
array_push($subs, $sub);
}
}
return $subs;
}
function join_sub_matrices($subs)
{
$ma = [];
if (count($subs) === 1) return $subs[0];
$size = sqrt(count($subs));
$sub_size = count($subs[0]);
$x = $y = 0;
for ($i = 0; $i < $size*$sub_size; $i++) {
for ($j = 0; $j < $size * $sub_size; $j++) {
$idx = intval(floor($i/$sub_size))*$size + intval(floor($j/$sub_size));
$ma[$i][$j] = $subs[$idx][$i%$sub_size][$j%$sub_size];
}
}
return $ma;
}
function rotate_matrix($matrix)
{
// https://stackoverflow.com/a/30088789
array_unshift($matrix, null);
$matrix = call_user_func_array('array_map', $matrix);
return array_map('array_reverse', $matrix);
}
/**
* Specific to this one, we only need to swap matrix row 0 and 2
*/
function flip_matrix3($matrix)
{
$tmp = $matrix[0];
$matrix[0] = $matrix[2];
$matrix[2] = $tmp;
return $matrix;
}
$rules = [];
$pat = [
['.', '#', '.'],
['.', '.', '#'],
['#', '#', '#']
];
$lines = file('input_d21.txt', FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
$parts = explode(' => ', $line);
// Output matrix
$ma = explode('/', $parts[1]);
$out = [];
foreach ($ma as $m) {
array_push($out, str_split($m));
}
$rules[str_replace('/', '', $parts[0])] = $out;
}
for ($i = 0; $i < 18; $i++) {
expand_matrix($pat, $rules);
if ($i === 4) {
var_dump(substr_count(join_matrix($pat), '#'));
}
}
var_dump(substr_count(join_matrix($pat), '#'));