Advent of code(2022)(php): Day one
Posted by
edit: I thought this was 2024, IT IS NOT
Day one we are given a set of data grouped by blank lines.
The task is too parse the data, add the arrays and find the top three values.
1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
<?php $puzzle_input = file("day_1.txt"); $elves = array(); $elves_total = array(); $blank = 0; foreach ($puzzle_input as $line){ $line = str_replace("\n", "", $line); if(empty($line)){ $blank++; }else{ $schema = "elf_".$blank; if(empty($elves[$schema])){ $elves[$schema][0] = $line; }else{ $elves[$schema][] = $line; } } } foreach ($elves as $elf => $calories){ $total = 0; foreach ($calories as $calorie){ $total = $total + $calorie; } $elves_total[$elf] = $total; } function sortElves($a, $b) { return $b <=> $a; } uasort($elves_total, "sortElves"); print("part one: ".$elves_total[array_keys($elves_total)[0]]); print("\n"); $top_three_total = $elves_total[array_keys($elves_total)[0]] + $elves_total[array_keys($elves_total)[1]] + $elves_total[array_keys($elves_total)[2]]; print("part two: ".$top_three_total); print("\n");
Leave a Reply