-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.php
65 lines (55 loc) · 2.04 KB
/
13.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
<?php
$input = file("inputs/13.txt");
$pattern = [];
$p1 = 0;
$p2 = 0;
foreach($input as $line){
$line = trim($line);
if(strlen($line) == 0){
$row = findReflection($pattern, 0);
$col = findReflection(transposePattern($pattern), 0);
$p1 += 100 * ($row+1) + ($col+1);
$row = findReflection($pattern, 2);
$col = findReflection(transposePattern($pattern), 2);
$p2 += 100 * ($row+1) + ($col+1);
$pattern = [];
}else{
$pattern[] = $line;
}
}
$row = findReflection($pattern, 0);
$col = findReflection(transposePattern($pattern), 0);
$p1 += 100 * ($row+1) + ($col+1);
$row = findReflection($pattern, 2);
$col = findReflection(transposePattern($pattern), 2);
$p2 += 100 * ($row+1) + ($col+1);
var_dump($p1);
var_dump($p2);
function findReflection($pattern, $numberOfAcceptedMissmatches){
for($a = 0; $a < count($pattern) - 1; $a++){
$missmatches = 0;
for($b = 0; $b < count($pattern); $b++){
if($a+1+($a-$b) >= 0 && $a+1+($a-$b) < count($pattern) && $pattern[$b] != $pattern[$a+1+($a-$b)]){
for($charIndex = 0; $charIndex < strlen($pattern[$b]); $charIndex++){
if($pattern[$b][$charIndex] != $pattern[$a+1+($a-$b)][$charIndex]){
$missmatches++;
}
}
}
}
if($missmatches == $numberOfAcceptedMissmatches){
return $a;
}
}
return -1;
}
function transposePattern($pattern){
$transposedPattern = array_fill(0, strlen($pattern[0]), "");
for($charIndex = 0; $charIndex < strlen($pattern[0]); $charIndex++){
for($lineIndex = 0; $lineIndex < count($pattern); $lineIndex++){
$transposedPattern[$charIndex] .= $pattern[$lineIndex][$charIndex];
}
}
return $transposedPattern;
}
?>