Advent of Code - Day 1
It's December again, the time of the Advent Calendars. Eric Wastl made the Advent of Code which is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language.
Today’s puzzle in short: It’s a heart-warming story about elves and their journey. To be able to travel, they need food and calories. Every elf writes a note about the amount of food (calories) they take in their bags.
Technical specification
First of all, we need to read between the lines, and pile off all the fairytale from the pure specification.
We will get the input data:
- every line may contain a number
- every line ends with a new line character (obviously)
- the numbers a grouped
- every group separated with one empty line
- the last row is also an empty line
Part one
The task: summarize the numbers by the groups and return with the highest number. It’s that simple.
The code
The first step is to save the puzzle input into a file: input.txt
. Then let’s think about the most simple solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$actualCalorie = 0;
$maxCalorie = 0;
if ($fn = fopen(__DIR__.'/input.txt', 'r')) {
while (($line = fgets($fn, 1024)) !== false) {
$line = trim($line);
if (empty($line)) {
$maxCalorie = max($actualCalorie, $maxCalorie);
$actualCalorie = 0;
continue;
}
$actualCalorie += (int) $line;
}
fclose($fn);
}
echo $maxCalorie.PHP_EOL;
- initialize the
$actualCalorie
and the$maxCalorie
as zero - read the file line-by-line:
- if not empty line:
- convert the data into a number
- add this number to a puffer variable
$actualCalorie
- if the line is empty:
- check if the
$actualCalorie
if higher than the$maxCalorie
- reset
$actualCalorie
- skip to next line
- check if the
- if not empty line:
- print the
$maxCalorie
Part two
The input is the same, but we need to get the sum of the top three groups.
The code
Instead of saving the $maxCalorie
all the time, we need to save all the groups, sort the array in descending order, and
add together the first three element.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$actualCalorie = 0;
$calories = [];
if ($fn = fopen(__DIR__.'/input.txt', 'r')) {
while (($line = fgets($fn, 1024)) !== false) {
$line = trim($line);
if (empty($line)) {
$calories[] = $actualCalorie;
$actualCalorie = 0;
continue;
}
$actualCalorie += (int) $line;
}
fclose($fn);
}
rsort($calories);
echo ($calories[0] + $calories[1] + $calories[2]).PHP_EOL;
I could go into a more secure and much prettier code, but I didn’t want to, because:
- the result is the important, not the code
- the input is known, so I could skip some array index availability checks