Skip to content

Commit 1d08fc9

Browse files
committed
Day16 Part1 done
1 parent 1059406 commit 1d08fc9

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The solutions to [advent of code 2020](https://adventofcode.com/2020), solved us
44
### Solutions 🥳🎉
55
> 🎄 [Day 1](/src/Day1.php) 🎅 [Day 2](/src/Day2.php) ☃️ [Day 3](/src/Day3.php) 🦌 [Day 4](/src/Day4.php) 🍪 [Day 5](/src/Day5.php)
66
> 🥛 [Day 6](/src/Day6.php) 🧦 [Day 7](/src/Day7.php) 🎁 [Day 8](/src/Day8.php)
7-
> [Day 9](/src/Day9.php) 🛐 [Day 10](/src/Day10.php)[Day 11](/src/Day11.php) 🍪 [Day 12](/src/Day12.php) ☃️ [Day 13](/src/Day13.php) 🎅 [Day 14](/src/Day14.php) 🎄 [Day 15](/src/Day15.php)
7+
> [Day 9](/src/Day9.php) 🛐 [Day 10](/src/Day10.php)[Day 11](/src/Day11.php) 🍪 [Day 12](/src/Day12.php) ☃️ [Day 13](/src/Day13.php) 🎅 [Day 14](/src/Day14.php) 🎄 [Day 15](/src/Day15.php) 🧦 [Day 16](/src/Day16.php)
88
### About
99
My attempts at tacking the awesome challenges at [Advent of Code 2020](https://adventofcode.com/2020/day/1) using PHP8.
1010

src/Day16.php

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,84 @@
88

99
class Day16 extends DayBehaviour implements DayInterface
1010
{
11-
public function solvePart1(): int|string|null
11+
public function solvePart1(): ?int
1212
{
13-
// TODO: Implement solvePart1() method.
14-
return null;
13+
/*$this->input = array_map(static fn(string $l) => $l . "\n", explode("\n",<<<INPUT
14+
class: 1-3 or 5-7
15+
row: 6-11 or 33-44
16+
seat: 13-40 or 45-50
17+
18+
your ticket:
19+
7,1,14
20+
21+
nearby tickets:
22+
7,3,47
23+
40,4,50
24+
55,2,20
25+
38,6,12
26+
INPUT));*/
27+
28+
[$rules,, $nearby] = $this->getTicketDataFromInput($this->input);
29+
30+
$invalid = [];
31+
foreach ($nearby as $ticket) {
32+
foreach ($ticket as $n) {
33+
$foundValidRule = false;
34+
$ticketInvalid = [];
35+
foreach ($rules as [$a, $b]) {
36+
if (($n < $a[0] || $n > $a[1]) && ($n < $b[0] || $n > $b[1])) {
37+
$ticketInvalid[] = $n;
38+
} else {
39+
$foundValidRule = true;
40+
}
41+
}
42+
if (!$foundValidRule) {
43+
/** @noinspection SlowArrayOperationsInLoopInspection */
44+
$invalid = array_merge($invalid, array_unique($ticketInvalid));
45+
}
46+
}
47+
}
48+
49+
return (int) array_sum($invalid);
1550
}
1651

17-
public function solvePart2(): int|string|null
52+
public function solvePart2(): ?int
1853
{
1954
// TODO: Implement solvePart2() method.
2055
return null;
2156
}
57+
58+
protected function getTicketDataFromInput(array $input): array
59+
{
60+
$ticket = [
61+
[], // rules
62+
[], // my
63+
[], // nearby
64+
];
65+
$inputPositions = array_keys($ticket);
66+
// start the pointer at rules
67+
$p = array_shift($inputPositions);
68+
// loop over each input, when we encounter a blank new line, update our pointer ($p) to the next position
69+
foreach ($input as $line) {
70+
if (PHP_EOL === $line) {
71+
$p = array_shift($inputPositions);
72+
}
73+
$line = trim($line);
74+
75+
// parse a rule e.g.: departure location: 29-458 or 484-956
76+
if (0 === $p) {
77+
[$ruleName, $numbers] = explode(':', $line, 2);
78+
if (1 <= preg_match_all('/(\d+)/', $numbers, $matches)) {
79+
$ticket[$p][$ruleName] = array_map(static fn (array $a) => array_map('intval', $a), array_chunk($matches[1], 2));
80+
}
81+
} elseif (str_contains($line, ',')) { // parse the comma separated list of numbers
82+
$nums = explode(',', $line);
83+
if (!empty($nums)) {
84+
$ticket[$p][] = array_map('intval', $nums);
85+
}
86+
}
87+
}
88+
89+
return $ticket;
90+
}
2291
}

tests/Day16Test.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use App\DayFactory;
6+
use App\Interfaces\DayInterface;
7+
8+
uses()->beforeEach(function (): void {
9+
/* @var DayInterface day */
10+
$this->day = DayFactory::create(getDayFromFile(__FILE__));
11+
});
12+
13+
test('solves part1')
14+
->expect(fn () => $this->day->solvePart1())
15+
->toBe(20048)
16+
;
17+
18+
test('solves part2')
19+
->expect(fn () => $this->day->solvePart2())
20+
->toBe(null)
21+
;

0 commit comments

Comments
 (0)