[2017-12-02] Solutions for both puzzles

master
Elis Hirwing 5 years ago
parent f2486b60a0
commit 2a7dc87787
Signed by: etu
GPG Key ID: D57EFA625C9A925F
  1. 2
      Makefile
  2. 52
      day2/main.php

@ -4,6 +4,8 @@ day1:
php day1/main.php
day2:
php day2/main.php
day3:
day4:
day5:

@ -0,0 +1,52 @@
<?php
// https://adventofcode.com/2017/day/2
function puzzle1(string $input) : int
{
$rows = explode(PHP_EOL, $input);
$sum = 0;
foreach ($rows as $row) {
$columns = explode("\t", $row);
sort($columns);
$small = array_shift($columns);
$big = array_pop($columns);
$sum += ($big - $small);
}
return $sum;
}
function puzzle2(string $input) : int
{
$rows = explode(PHP_EOL, $input);
$sum = 0;
foreach ($rows as $row) {
$columns = explode("\t", $row);
foreach ($columns as $key1 => $value1) {
foreach ($columns as $key2 => $value2) {
if ($key1 === $key2) {
continue;
}
if ($value1 < $value2) {
continue;
}
// Do the actual division
$div = $value1 / $value2;
if (intval($div) === $div) {
$sum += $div;
}
}
}
}
return $sum;
}
Loading…
Cancel
Save