Created
December 4, 2023 18:46
-
-
Save maartenpaauw/3e42eef02355863cd3535d3e45b735b5 to your computer and use it in GitHub Desktop.
Advent of Code 2023 - Day 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Tinker away! | |
use Illuminate\Support\Arr; | |
use Illuminate\Support\Str; | |
$input = "two1nine | |
eightwothree | |
abcone2threexyz | |
xtwone3four | |
4nineeightseven2 | |
zoneight234 | |
7pqrstsixteen"; | |
Str::of($input) | |
->explode("\n") | |
->map(function (string $line): int { | |
preg_match_all('/(?=(one|two|three|four|five|six|seven|eight|nine|\d))/', $line, $matches); | |
$numbers = Arr::map($matches[1], static fn ($value): int => match ($value) { | |
'1', 'one' => 1, | |
'2', 'two' => 2, | |
'3', 'three' => 3, | |
'4', 'four' => 4, | |
'5', 'five' => 5, | |
'6', 'six' => 6, | |
'7', 'seven' => 7, | |
'8', 'eight' => 8, | |
'9', 'nine' => 9, | |
}); | |
return Arr::first($numbers) . Arr::last($numbers); | |
}) | |
->sum(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment