DEV Community

Maha Bouchiba
Maha Bouchiba

Posted on

Advent Of Code 2022 in PHP

Hi everyone !
First post here for December 1st and not a coincidence because this year I join the annual challenge Advent Of Code 2022 and created by @ericwastl (you can find him on Twitter).

I code in PHP so let me share with you my code if you want to do it with the same language let me know and let's talk about your reflexion ^^

Let's go and I hope I'll find the motivation to complete this challenge until the 25th !!

Get the first kata after log in : https://adventofcode.com/2022/day/1

<?php

$input = file_get_contents('inputs/day01');
$lines= explode("
", $input);

// Part 1 
$sum = array_reduce($lines, function($carry, $item) {
    if (empty($item)) {
        $carry[] = 0;
    } else {
        $carry[count($carry)-1] += $item;
    }
    return $carry;
}, [0]);

// Get the max : 
// var_dump(max($sum));

// Part 2

function getThreeElvesCalories($input){
    $sortSum = rsort($input);
    $array= [];
    for($i=0; $i<3; $i++){
        $array[] = $input[$i];
    }
    return array_sum($array);
}
print_r(getThreeElvesCalories($sum));

Enter fullscreen mode Exit fullscreen mode

Top comments (0)