8000 Day 10, part1 · FailedCode/adventofcode-2022-php@afcf357 · GitHub
[go: up one dir, main page]

Skip to content

Commit afcf357

Browse files
committed
Day 10, part1
1 parent f01444d commit afcf357

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/Days/Day10.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Failedcode\Aoc2022\Days;
4+
5+
class Day10 extends AbstractDay
6+
{
7+
public function solve_part_1(): string
8+
{
9+
$changes = $this->getRegisterChanges();
10+
$cycleSave = [
11+
20 => 0,
12+
60 => 0,
13+
100 => 0,
14+
140 => 0,
15+
180 => 0,
16+
220 => 0,
17+
];
18+
$x = $cycle = 1;
19+
foreach ($changes as $change) {
20+
if (array_key_exists($cycle, $cycleSave)) {
21+
$cycleSave[$cycle] = $x * $cycle;
22+
}
23+
$x += $change;
24+
$cycle += 1;
25+
}
26+
return array_reduce($cycleSave, function($c, $v){ return $c + $v; });
27+
}
28+
29+
public function solve_part_2(): string
30+
{
31+
return "TODO";
32+
}
33+
34+
protected function getRegisterChanges()
35+
{
36+
$register = [];
37+
$inputList = array_filter($this->util->loadInput(10), 'strlen');
38+
foreach ($inputList as $row) {
39+
if (trim($row) === 'noop') {
40+
$register[] = 0;
41+
continue;
42+
}
43+
if (preg_match('~addx (-?\d+)~', $row, $match)) {
44+
$register[] = 0;
45+
$register[] = (int)$match[1];
46+
}
47+
}
48+
return $register;
49+
}
50+
}

0 commit comments

Comments
 (0)
0