Skip to content

Commit 1de1d38

Browse files
committed
Temp commit for 2023 Day 3 Part 1. Works on test data but not real input
1 parent ae75ab8 commit 1de1d38

File tree

4 files changed

+289
-0
lines changed

4 files changed

+289
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Advent of Code 2023 - Day 3
2+
3+
## Problem Statement
4+
5+
## Input
6+
7+
## Part 1
8+
9+
## Part 2
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace App\AdventSolutions\Year2023\Day3;
4+
5+
use App\AdventSolutions\AbstractSolution;
6+
7+
class Solution2023Day3 extends AbstractSolution
8+
{
9+
10+
private array $input;
11+
12+
public function solvePart1($input): string
13+
{
14+
$this->input = $input;
15+
16+
$numbers = $this->findNumbers();
17+
18+
$numbers_with_symbols = 0;
19+
20+
foreach ($numbers as $number) {
21+
if ($this->hasAdjacentSymbol($number)) {
22+
$numbers_with_symbols += $number['number'];
23+
print_r('Fant symbol rundt ' . $number['number'] . ' på linje ' . $number['line'] . '. Delsum: ' . $numbers_with_symbols . PHP_EOL);
24+
}
25+
}
26+
27+
return "Sum of all numbers with adjacent symbol: <info>$numbers_with_symbols</info>";
28+
}
29+
30+
public function solvePart2($input): string
31+
{
32+
// Implement the logic for solving part 2 here
33+
34+
return "Part 2 not yet implemented!";
35+
}
36+
37+
private function findNumbers(): array
38+
{
39+
$numbers = [];
40+
$number = '';
41+
$start_position = null;
42+
43+
foreach ($this->input as $index => $line) {
44+
for ($position = 0; $position < strlen($line); $position++) {
45+
if (is_numeric($line[$position])) {
46+
if ($start_position === null) {
47+
$start_position = $position;
48+
}
49+
$number .= $line[$position];
50+
} else {
51+
if ($start_position !== null) {
52+
$numbers[] = [
53+
'number' => $number,
54+
'start_position' => $start_position,
55+
'end_position' => $position - 1,
56+
'line' => $index,
57+
];
58+
}
59+
60+
$number = '';
61+
$start_position = null;
62+
}
63+
}
64+
}
65+
66+
return $numbers;
67+
}
68+
69+
private function isSymbol($index, $position): bool
70+
{
71+
// check if index exists
72+
if (!isset($this->input[$index])) {
73+
return false;
74+
}
75+
76+
// check if position exists
77+
if (!isset($this->input[$index][$position])) {
78+
return false;
79+
}
80+
81+
// check if character is . or a number
82+
if ($this->input[$index][$position] == '.' || is_numeric($this->input[$index][$position])) {
83+
return false;
84+
}
85+
86+
return true;
87+
}
88+
89+
private function hasAdjacentSymbol($number): bool
90+
{
91+
$start_position = $number['start_position'];
92+
$end_position = $number['end_position'];
93+
$line = $number['line'];
94+
95+
// print_r($number['number'] . ' på linje ' . $line . ' fra ' . $start_position . ' til ' . $end_position . PHP_EOL);
96+
97+
// check top
98+
for ($position = $start_position - 1; $position <= $end_position + 1; $position++) {
99+
// print_r('Sjekker over på linje ' . $line - 1 . ' posisjon ' . $position . PHP_EOL);
100+
if ($this->isSymbol($line - 1, $position)) {
101+
// print_r('Fant symbol over' . PHP_EOL);
102+
return true;
103+
}
104+
}
105+
106+
// check bottom
107+
for ($position = $start_position - 1; $position <= $end_position + 1; $position++) {
108+
// print_r('Sjekker under på linje ' . $line + 1 . ' posisjon ' . $position . PHP_EOL);
109+
if ($this->isSymbol($line + 1, $position)) {
110+
// print_r('Fant symbol under' . PHP_EOL);
111+
return true;
112+
}
113+
}
114+
115+
// check left
116+
if ($this->isSymbol($line, $start_position - 1)) {
117+
// print_r('Fant symbol til venstre' . PHP_EOL);
118+
return true;
119+
}
120+
121+
// check right
122+
if ($this->isSymbol($line, $end_position + 1)) {
123+
// print_r('Fant symbol til høyre' . PHP_EOL);
124+
return true;
125+
}
126+
127+
// print_r('Fant ikke symbol rundt' . PHP_EOL);
128+
return false;
129+
}
130+
}

0 commit comments

Comments
 (0)