Skip to content

Commit aff1e76

Browse files
committed
day11 part1 solved
1 parent c70aa1f commit aff1e76

File tree

3 files changed

+72
-18
lines changed

3 files changed

+72
-18
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ shell: ## Launch a shell into the docker container
9090
$(DOCKER_RUN_PHP) /bin/bash
9191

9292
xdebug: ## Launch a php container with xdebug (port 10000)
93-
@$(DOCKER_RUN_PHP_XDEBUG) php run.php
93+
@$(DOCKER_RUN_PHP_XDEBUG) php run.php $(onlyThisDay)
9494

9595
xdebug-shell: ## Launch a php container with xdebug in a shell (port 10000)
9696
@echo -e "=== Xdebug Launch Instructions ===\nAt the prompt type:\nphp run.php [day]\n\n"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ If you fancy having a go at the challenges yourself feel free to use this repo a
3636
`make run day={N}` e.g. `make run day=1`
3737

3838
**Use XDebug**
39-
`make xdebug` at the shell type: `vendor/bin/pest`
39+
`make xdebug`
4040

4141
IDE settings:
4242
- `10000` - xdebug port

src/Day11.php

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@
88

99
class Day11 extends DayBehaviour implements DayInterface
1010
{
11-
protected function applyRulesToSeats(array $seats): array
12-
{
13-
return [];
14-
}
11+
protected int $i = 0;
1512

1613
/**
1714
* All decisions are based on the number of occupied seats adjacent to a given seat
1815
* (one of the eight positions immediately up, down, left, right, or diagonal from the seat).
1916
* The following rules are applied to every seat simultaneously:.
17+
* Rules:
18+
* If a seat is empty (L) and there are no occupied seats adjacent to it, the seat becomes occupied.
19+
* If a seat is occupied (#) and four or more seats adjacent to it are also occupied, the seat becomes empty.
20+
*
21+
* Simulate your seating area by applying the seating rules repeatedly until no seats change state. How many seats end up occupied?
2022
*
2123
* @return int|null
2224
*/
2325
public function solvePart1(): ?int
2426
{
25-
$this->input = [
27+
/*$this->input = [
2628
'L.LL.LL.LL',
2729
'LLLLLLL.LL',
2830
'L.L.L..L..',
@@ -33,21 +35,73 @@ public function solvePart1(): ?int
3335
'LLLLLLLLLL',
3436
'L.LLLLLL.L',
3537
'L.LLLLL.LL',
36-
];
37-
$this->input = array_map(static fn (string $s): string => trim($s), $this->input);
38-
$i = 0;
39-
/*while(true) {
40-
$seatsChanged = 0;
38+
];*/
39+
// convert into [y][x] array
40+
$this->input = array_map(static fn (string $s): array => str_split(trim($s)), $this->input);
41+
$finalSeatingPlan = $this->seatTraverse();
42+
$occupied = array_filter(array_merge(...$finalSeatingPlan), static fn (string $s) => '#' === $s);
43+
44+
return count($occupied);
45+
}
4146

42-
foreach($this->input as &$row) {
43-
$adjacent = '';
44-
array_walk($row, static function(string $s) use (&$seatsChanged) {
47+
protected function seatTraverse(?array $seatLayout = null): array
48+
{
49+
++$this->i;
50+
// final recursive check
51+
if ($seatLayout === $this->input) {
52+
return $this->input;
53+
}
54+
55+
// if we haven't been seeded then start with input
56+
$this->input = $seatLayout ?? $this->input;
4557

46-
});
58+
$newInput = $this->input; // clone it, we'll make all changes to the new input
59+
for ($y = 0, $yMax = count($this->input); $y < $yMax; ++$y) {
60+
for ($x = 0, $xMax = count($this->input[$y]); $x < $xMax; ++$x) {
61+
$adjacentSeats = [
62+
// above
63+
$this->input[$y - 1][$x - 1] ?? '',
64+
$this->input[$y - 1][$x] ?? '',
65+
$this->input[$y - 1][$x + 1] ?? '',
66+
// below
67+
$this->input[$y + 1][$x - 1] ?? '',
68+
$this->input[$y + 1][$x] ?? '',
69+
$this->input[$y + 1][$x + 1] ?? '',
70+
// left
71+
$this->input[$y][$x - 1] ?? '',
72+
// right
73+
$this->input[$y][$x + 1] ?? '',
74+
];
75+
$occupiedAdjacent = array_filter($adjacentSeats, static fn (string $v) => '#' === $v);
76+
$seat = $this->input[$y][$x];
77+
switch ($seat) { // faster than using match
78+
case '.':
79+
continue 2;
80+
case 'L':
81+
if (empty($occupiedAdjacent)) {
82+
$newInput[$y][$x] = '#';
83+
}
84+
break;
85+
case '#':
86+
if (4 <= count($occupiedAdjacent)) {
87+
$newInput[$y][$x] = 'L';
88+
}
89+
break;
90+
}
4791
}
92+
/*printf("y: %d\n%s\n%s\n",
93+
$y,
94+
implode('', $this->input[$y]),
95+
implode('', $newInput[$y]),
96+
);*/
4897
}
49-
print_r($this->input);*/
50-
return null;
98+
/*printf("%d: new: %d old: %d\n",
99+
$this->i,
100+
count(array_filter(array_merge(...$newInput), static fn (string $s) => '#' === $s)),
101+
count(array_filter(array_merge(...$this->input), static fn (string $s) => '#' === $s)),
102+
);*/
103+
104+
return $this->seatTraverse($newInput);
51105
}
52106

53107
public function solvePart2(): ?int

0 commit comments

Comments
 (0)