Skip to content

Commit 7d5c216

Browse files
committed
Day 11, part2
1 parent 80799d8 commit 7d5c216

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/Days/Day11.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function solve_part_1(): string
1313
for ($i = 0; $i < $rounds; $i += 1) {
1414
/** @var Monkey $monkey */
1515
foreach ($monkeys as $monkey) {
16-
$monkey->calculateTurn($monkeys);
16+
$monkey->calculateTurn($monkeys, true);
1717
}
1818
}
1919
uasort($monkeys, function ($a, $b) {
@@ -27,7 +27,21 @@ public function solve_part_1(): string
2727

2828
public function solve_part_2(): string
2929
{
30-
return "TODO";
30+
$monkeys = $this->loadMonkeys();
31+
$rounds = 10000;
32+
for ($i = 0; $i < $rounds; $i += 1) {
33+
/** @var Monkey $monkey */
34+
foreach ($monkeys as $monkey) {
35+
$monkey->calculateTurn($monkeys,false);
36+
}
37+
}
38+
uasort($monkeys, function ($a, $b) {
39+
return $a->getInspections() - $b->getInspections();
40+
});
41+
42+
$monkeyFirst = array_pop($monkeys);
43+
$monkeySecond = array_pop($monkeys);
44+
return $monkeyFirst->getInspections() * $monkeySecond->getInspections();
3145
}
3246

3347
protected function loadMonkeys()

src/Days/Support/Monkey.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Monkey
1717
protected int $ifTrueMonkey = 0;
1818
protected int $ifFalseMonkey = 0;
1919
protected int $inspections = 0;
20+
protected int $commonDivisor = 1;
2021

2122
public function __construct(array $data)
2223
{
@@ -49,7 +50,7 @@ public function printInspections()
4950
echo "Monkey {$this->nr}: {$this->inspections}\n";
5051
}
5152

52-
public function calculateTurn($monkeyList)
53+
public function calculateTurn($monkeyList, $dontWorryBeHappy)
5354
{
5455
$itemCount = count($this->items);
5556
for ($i = 0; $i < $itemCount; $i += 1) {
@@ -67,7 +68,16 @@ public function calculateTurn($monkeyList)
6768
echo "\nUNKNOWN OPERATOR '{$this->operationType}'\n";
6869
exit(0);
6970
}
70-
$item = (int)($item / self::WORRY_DIV);
71+
if ($dontWorryBeHappy) {
72+
$item = (int)($item / self::WORRY_DIV);
73+
} else {
74+
if ($this->commonDivisor === 1) {
75+
foreach ($monkeyList as $monkey) {
76+
$this->commonDivisor *= $monkey->getDivisionTestValue();
77+
}
78+
}
79+
$item = (int)($item % $this->commonDivisor);
80+
}
7181
if ($item % $this->divisionTestValue == 0) {
7282
$monkeyList[$this->ifTrueMonkey]->receiveItem($item);
7383
} else {
@@ -85,4 +95,9 @@ public function getInspections(): int
8595
{
8696
return $this->inspections;
8797
}
98+
99+
public function getDivisionTestValue(): int
100+
{
101+
return $this->divisionTestValue;
102+
}
88103
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy