Skip to content

Commit b80d76b

Browse files
committed
day 15 pt 2
1 parent a27e95f commit b80d76b

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ ifeq ($(shell docker image inspect $(image-name) > /dev/null 2>&1 || echo not_ex
9090
make run
9191
else
9292
ifneq ("$(wildcard vendor)", "")
93-
@$(DOCKER_RUN_PHP_MY_IMAGE) $(image-name) php -dopcache.enable_cli=1 -dopcache.jit_buffer_size=100M -dopcache.jit=1255 run.php $(onlyThis)
93+
@$(DOCKER_RUN_PHP_MY_IMAGE) $(image-name) php -dmemory_limit=4G -dopcache.enable_cli=1 -dopcache.jit_buffer_size=100M -dopcache.jit=1255 run.php $(onlyThis)
9494
else
9595
@echo -e "\nFirst run detected! No vendor/ folder found, running composer update...\n"
9696
make composer

src/Day15.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ class Day15 extends DayBehaviour implements DayInterface
1010
{
1111
public function solvePart1(): int|string|null
1212
{
13-
//$this->input[0] = '0,3,6';
14-
//$this->input[0] = '3,1,2';
1513
$turns = array_map('intval', str_getcsv(trim($this->input[0])));
1614
$max = 2020;
1715
$i = count($turns);
@@ -33,7 +31,35 @@ public function solvePart1(): int|string|null
3331

3432
public function solvePart2(): int|string|null
3533
{
36-
// TODO: Implement solvePart2() method.
37-
return null;
34+
// this takes 12s and uses 1.42gb of memory!
35+
//return 1065;
36+
$turns = array_map('intval', str_getcsv(trim($this->input[0])));
37+
$max = 30000000;
38+
$i = count($turns);
39+
40+
// <no => [turn2,turn1]>
41+
$mem = [];
42+
$last = 0;
43+
// seed with starting numbers, turn always starts from 1
44+
foreach ($turns as $k => $t) {
45+
$mem[$t] = [$k + 1];
46+
$last = $t;
47+
}
48+
49+
while ($i < $max) {
50+
// loop backwards from end of array
51+
if (1 === count($mem[$last])) {
52+
$next = 0;
53+
} else {
54+
$next = $mem[$last][1] - $mem[$last][0];
55+
$mem[$last] = array_slice($mem[$last], -2);
56+
}
57+
$mem[$next][] = $i + 1;
58+
$mem[$next] = array_slice($mem[$next], -2);
59+
$last = $next;
60+
++$i;
61+
}
62+
63+
return $last;
3864
}
3965
}

tests/Day15Test.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(1065)
2121
;

0 commit comments

Comments
 (0)