-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.php
51 lines (36 loc) · 848 Bytes
/
day1.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
<?php
include(__DIR__."/_loader.php");
$input = InputHelper::loadFile("datasets/day1.txt");
// Part 1
$max = 0;
$items = [];
foreach ($input as $line) {
if (strlen($line)) {
$items[] = $line;
continue;
}
$sum = array_sum($items);
if ($sum > $max) $max = $sum;
$items = [];
}
$sum = array_sum($items);
if ($sum > $max) $max = $sum;
$items = [];
echo "Answer #1: ".$max.PHP_EOL;
// Part 2
$cursor = 0;
$bags = [0 => []];
foreach ($input as $line) {
if (strlen($line)) {
$bags[$cursor][] = $line;
continue;
}
$cursor++;
$bags[$cursor] = [];
}
$sums = [];
foreach ($bags as $bag) $sums[] = array_sum($bag);
sort($sums);
$total = 0;
for ($i=0; $i<3; $i++) $total += array_pop($sums);
echo "Answer #2: ".$total.PHP_EOL;