Day 2 solution.
This commit is contained in:
parent
2b2bc8f134
commit
3f2ac3a266
@ -0,0 +1 @@
|
|||||||
|
851786270-851907437,27-47,577-1044,1184-1872,28214317-28368250,47766-78575,17432-28112,2341-4099,28969-45843,5800356-5971672,6461919174-6461988558,653055-686893,76-117,2626223278-2626301305,54503501-54572133,990997-1015607,710615-802603,829001-953096,529504-621892,8645-12202,3273269-3402555,446265-471330,232-392,179532-201093,233310-439308,95134183-95359858,3232278502-3232401602,25116215-25199250,5489-8293,96654-135484,2-17
|
||||||
@ -1,17 +1,64 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Aoc2025\Days;
|
namespace Aoc2025\Days;
|
||||||
|
|
||||||
use Aoc2025\Day;
|
use Aoc2025\Day;
|
||||||
|
|
||||||
class Day2 extends Day {
|
class Day2 extends Day {
|
||||||
|
|
||||||
|
private function isInvalidId($num) {
|
||||||
|
$str = (string)$num;
|
||||||
|
$len = strlen($str);
|
||||||
|
|
||||||
|
// Try all possible pattern lengths (from 1 to len/2)
|
||||||
|
for ($patternLen = 1; $patternLen <= $len / 2; $patternLen++) {
|
||||||
|
// Only check if the pattern length divides evenly into total length
|
||||||
|
if ($len % $patternLen !== 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pattern = substr($str, 0, $patternLen);
|
||||||
|
$repetitions = $len / $patternLen;
|
||||||
|
|
||||||
|
// Must repeat at least twice
|
||||||
|
if ($repetitions < 2) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the entire string is this pattern repeated
|
||||||
|
$repeated = str_repeat($pattern, $repetitions);
|
||||||
|
if ($repeated === $str) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function run() {
|
public function run() {
|
||||||
$input = $this->getInputFile('day_2_input.txt');
|
$input = $this->getInputFile('day_2_input.txt');
|
||||||
|
|
||||||
|
// Parse ranges
|
||||||
|
$ranges = explode(',', trim($input));
|
||||||
|
$sumPart1 = 0;
|
||||||
|
$sumPart2 = 0;
|
||||||
|
|
||||||
|
foreach ($ranges as $range) {
|
||||||
|
$range = trim($range);
|
||||||
|
list($start, $end) = explode('-', $range);
|
||||||
|
$start = (int)$start;
|
||||||
|
$end = (int)$end;
|
||||||
|
|
||||||
|
// Check each number in range
|
||||||
|
for ($id = $start; $id <= $end; $id++) {
|
||||||
|
if ($this->isInvalidId($id)) {
|
||||||
|
$sumPart2 += $id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'part_1' => 0,
|
'part_1' => $sumPart1,
|
||||||
'part_2' => 0,
|
'part_2' => $sumPart2,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user