Skip to content

Instantly share code, notes, and snippets.

@Nessworthy
Last active December 19, 2017 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nessworthy/50023ec1d798567e159cd6068a6ef44e to your computer and use it in GitHub Desktop.
Save Nessworthy/50023ec1d798567e159cd6068a6ef44e to your computer and use it in GitHub Desktop.
0: 5
1: 2
2: 3
4: 4
6: 6
8: 4
10: 8
12: 6
14: 6
16: 8
18: 6
20: 9
22: 8
24: 10
26: 8
28: 8
30: 12
32: 8
34: 12
36: 10
38: 12
40: 12
42: 12
44: 12
46: 12
48: 14
50: 12
52: 14
54: 12
56: 14
58: 12
60: 14
62: 14
64: 14
66: 14
68: 14
70: 14
72: 14
76: 14
80: 18
84: 14
90: 18
92: 17
const input = document.body.textContent.trim();
const guards = input.split('\n').map(s => s.match(/\d+/g).map(Number));
const caughtByGuard = delay => ([d, r]) => (delay + d) % (2 * (r - 1)) === 0;
let delay = -1;
while (guards.some(caughtByGuard(++delay)));
console.log(delay);
<?php
$input = ''; // puzzle input.
$list = array_map('trim', explode("\n", $input));
$guards = array_map(function (string $line) {
return array_map('\intval', explode(': ', $line));
}, $list);
function caughtByGuard(int $delay): callable
{
return function ($line) use ($delay): bool {
return ($delay + $line[0]) % (2 * ($line[1] - 1)) === 0;
};
}
$isCaught = true;
$delay = -1;
while ($isCaught === true) {
$current = ++$delay;
foreach ($guards as $line) {
$isCaught = caughtByGuard($current)($line);
if ($isCaught === true) {
break;
}
}
}
echo $delay;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment