-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.php
160 lines (122 loc) · 3.89 KB
/
day11.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
include(__DIR__."/_loader.php");
$input = InputHelper::loadFile("datasets/day11.txt");
$monkeysInit = [];
foreach ($input as $line) {
if (! strlen($line)) continue;
$words = explode(' ', $line);
switch ($words[0]) {
case 'Monkey':
$cursor = str_replace(':', '', $words[1]);
$monkeysInit[$cursor] = new Monkey();
$current = $monkeysInit[$cursor];
break;
case 'Starting':
$current->items = explode(',', implode('', array_slice($words, 2)));
break;
case 'Operation:':
$current->formula = str_replace('new = ', '', implode(' ', array_slice($words, 1)));
break;
case 'Test:':
$current->modulo = array_pop($words);
break;
case 'If':
$cond = str_replace(':', '', $words[1]);
$current->{$cond} = array_pop($words);
break;
}
}
// Part 1
$monkeys = unserialize(serialize($monkeysInit));
$rounds = 20;
while ($rounds > 0) {
foreach ($monkeys as $monkey) $monkey->part1();
$rounds--;
}
$stats = [];
foreach ($monkeys as $monkey) $stats[] = $monkey->tested;
rsort($stats);
echo "Answer #1: ".array_product(array_slice($stats, 0, 2)).PHP_EOL;
// Part 2
// I was WRONG here :)
// I guessed the correct modulo for example data
// and found by luck that it was the product of all monkeys modulos (see after)
// 1. test if modulo can do the trick (it's a pure guess, not sure)
// It takes quite some time to exec.
// Output: [278 => 96577]
/*
$candidates = [];
$expected = [99, 97, 8, 103];
$mod = 1;
while ($mod < 100000) {
$monkeys = unserialize(serialize($monkeysInit));
$rounds = 20;
while ($rounds > 0) {
foreach ($monkeys as $monkey) $monkey->part2($mod);
$rounds--;
}
$stats = [];
foreach ($monkeys as $monkey) $stats[] = $monkey->tested;
if ($stats == $expected) $candidates[] = $mod;
$mod++;
}
$expected = [10419, 9577, 392, 10391];
foreach ($candidates as $k => $mod) {
$monkeys = unserialize(serialize($monkeysInit));
$rounds = 2000;
while ($rounds > 0) {
foreach ($monkeys as $monkey) $monkey->part2($mod);
$rounds--;
}
$stats = [];
foreach ($monkeys as $monkey) $stats[] = $monkey->tested;
if ($stats != $expected) unset($candidates[$k]);
}
print_r($candidates);
exit;
*/
$monkeys = unserialize(serialize($monkeysInit));
// Calculating the correct modulo in the good way !
$mod = 1;
foreach ($monkeys as $monkey) $mod *= $monkey->modulo;
$rounds = 10000;
while ($rounds > 0) {
foreach ($monkeys as $monkey) $monkey->part2($mod);
$rounds--;
}
$stats = [];
foreach ($monkeys as $monkey) $stats[] = $monkey->tested;
rsort($stats);
echo "Answer #2: ".array_product(array_slice($stats, 0, 2)).PHP_EOL;
class Monkey {
public $items = [];
public $tested = 0;
public $formula = "";
public $modulo = 0;
public $true = 0;
public $false = 0;
public function part1() {
global $monkeys;
foreach ($this->items as $k => $item) {
$this->tested++;
// NOT RECOMMENDED TO USE EVAL ! Replace if possible.
eval("\$item = (".str_replace('old', $item, $this->formula).");");
$item = floor($item/3);
$dest = ($item%$this->modulo == 0) ? $this->true : $this->false;
$monkeys[$dest]->items[] = $item;
unset($this->items[$k]);
}
}
public function part2($mod) {
global $monkeys;
foreach ($this->items as $k => $item) {
$this->tested++;
// NOT RECOMMENDED TO USE EVAL ! Replace if possible.
eval("\$item = (".str_replace('old', $item, $this->formula).");");
$item = $item%$mod;
$dest = ($item%$this->modulo == 0) ? $this->true : $this->false;
$monkeys[$dest]->items[] = $item;
unset($this->items[$k]);
}
}
}