Skip to content

Commit a27e95f

Browse files
committed
day 15 pt 1
1 parent 2172da1 commit a27e95f

File tree

6 files changed

+69
-12
lines changed

6 files changed

+69
-12
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)
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)
88
99
### About
1010
My attempts at tacking the awesome challenges at [Advent of Code 2020](https://adventofcode.com/2020/day/1) using PHP8.

input/day15.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
14,3,1,0,9,5

src/Day14.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,6 @@ public function solvePart2(): int|string|null
7171
}
7272
// now our memory position in binary ($posBin) contains a number of floating values (X)
7373
// these can be either 0 or 1 giving us 2^X possible memory combinations.
74-
75-
// Example: given a $posBin like 000000000000000000000000000000X1101X with 2 X's, we get 4 combinations:
76-
// 000000000000000000000000000000(X)1101(X) becomes:
77-
// 1. 000000000000000000000000000000(0)1101(0)
78-
// 2. 000000000000000000000000000000(0)1101(1)
79-
// 3. 000000000000000000000000000000(1)1101(0)
80-
// 4. 000000000000000000000000000000(1)1101(1)
81-
82-
// with 3 it's 8 combinations.. and so on
83-
8474
// let's do this recursively
8575
$posCombos = $this->maskTraverse($posMask);
8676
foreach ($posCombos as $pos) {
@@ -94,6 +84,12 @@ public function solvePart2(): int|string|null
9484

9585
/**
9686
* Recursively build up a list of all possible positions.
87+
* Example: given a $posBin like 000000000000000000000000000000X1101X with 2 X's, we get 4 combinations:
88+
* 000000000000000000000000000000(X)1101(X) becomes:
89+
* 1. 000000000000000000000000000000(0)1101(0)
90+
* 2. 000000000000000000000000000000(0)1101(1)
91+
* 3. 000000000000000000000000000000(1)1101(0)
92+
* 4. 000000000000000000000000000000(1)1101(1).
9793
*
9894
* @param string $posMask
9995
*

src/Day15.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
use App\Interfaces\DayInterface;
8+
9+
class Day15 extends DayBehaviour implements DayInterface
10+
{
11+
public function solvePart1(): int|string|null
12+
{
13+
//$this->input[0] = '0,3,6';
14+
//$this->input[0] = '3,1,2';
15+
$turns = array_map('intval', str_getcsv(trim($this->input[0])));
16+
$max = 2020;
17+
$i = count($turns);
18+
while ($i < $max) {
19+
$last = $turns[count($turns) - 1];
20+
$seen = array_slice(array_filter($turns, static fn (int $no) => $no === $last, ARRAY_FILTER_USE_BOTH), -2, 2, true);
21+
if (1 === count($seen)) {
22+
$next = 0;
23+
} else {
24+
$last2 = array_map(static fn (int $i) => $i + 1, array_keys($seen));
25+
$next = $last2[1] - $last2[0];
26+
}
27+
$turns[] = $next;
28+
++$i;
29+
}
30+
31+
return $turns[count($turns) - 1];
32+
}
33+
34+
public function solvePart2(): int|string|null
35+
{
36+
// TODO: Implement solvePart2() method.
37+
return null;
38+
}
39+
}

tests/Day14Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717

1818
test('solves part2')
1919
->expect(fn () => $this->day->solvePart2())
20-
->toBe(null)
20+
->toBe(2737766154126)
2121
;

tests/Day15Test.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(614)
16+
;
17+
18+
test('solves part2')
19+
->expect(fn () => $this->day->solvePart2())
20+
->toBe(null)
21+
;

0 commit comments

Comments
 (0)