-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_three_part_two.php
109 lines (66 loc) · 2.61 KB
/
day_three_part_two.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
<?php
const indicator = "*";
function search_for_the_numers($file_buffer, $row,$collumn){
$pivot = $file_buffer[$row][$collumn];
$number = intval($pivot);
$left_offset = 1;
// Search for the left side;
while (true) {
if (!is_numeric($file_buffer[$row][$collumn - $left_offset])){
break;
}
$digit = intval($file_buffer[$row][$collumn - $left_offset]);
$number += $digit * pow(10, $left_offset);
$left_offset++;
}
$right_offset = 1;
// Search for the right side;
while (true) {
if (!is_numeric($file_buffer[$row][$collumn + $right_offset])){
break;
}
$digit = intval($file_buffer[$row][$collumn + $right_offset]);
$number *= 10;
$number += $digit;
$right_offset++;
}
return $number;
}
function search_for_part_numbers($file_buffer){
$gear_ration = 0;
for ($row_id=0; $row_id < count($file_buffer); $row_id++) {
$current_row = $file_buffer[$row_id];
for($index = 0; $index < strlen($current_row); $index++){
if ($current_row[$index] == indicator){
$found_numbers = array();
for ($horizotal_offset=-1; $horizotal_offset <2 ; $horizotal_offset++) {
for ($vertical_offset=-1; $vertical_offset <2 ; $vertical_offset++) {
$currently_checing_box = $file_buffer[$row_id + $vertical_offset][$index + $horizotal_offset];
if (!is_numeric($currently_checing_box) || $currently_checing_box == "."){continue;}
$number_found = search_for_the_numers($file_buffer, $row_id + $vertical_offset, $index + $horizotal_offset);
if(!in_array($number_found, $found_numbers)){
array_push($found_numbers, $number_found);
}
}
}
if (count($found_numbers) == 2){
$gear_ration += $found_numbers[0] * $found_numbers[1];
}
}
}
}
return $gear_ration;
}
function readfile_to_buffer($file) {
$base_array = array();
while (!feof($file)){
$current_line = fgets($file);
array_push($base_array, $current_line);
}
return $base_array;
}
$input_file = fopen("./input/day_three.txt", "r");
$line_buffers = readfile_to_buffer($input_file);
$result = search_for_part_numbers($line_buffers);
echo " The answer is: [[ $result ]] \n";
?>