Skip to content

Instantly share code, notes, and snippets.

@mrbellek
mrbellek / day2.php
Last active December 7, 2023 15:03
Advent of Code 2023 - day 2
<?php
declare(strict_types=1);
$input = file('input.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$maxRed = 12;
$maxGreen = 13;
$maxBlue = 14;
$validCount = 0;
@mrbellek
mrbellek / day1.php
Last active December 7, 2023 15:02
Advent of Code 2023 - day 1
<?php
declare(strict_types=1);
$input = file('input.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$sumOfNumbers = 0;
foreach ($input as $line) {
printf('turned %s into ', $line);
$line = replaceNumberWordsWithDigits($line);
printf('%s' . PHP_EOL, $line);
$digits = getFirstAndLastDigitsFromLine($line);
@mrbellek
mrbellek / day5.php
Last active December 7, 2023 15:02
Advent of Code day 5 - part 1
<?php
$input = file('input.txt', FILE_IGNORE_NEW_LINES);
//$input = file('sample.txt', FILE_IGNORE_NEW_LINES);
$seeds = explode(' ', str_replace('seeds: ', '', $input[0]));
$seedToSoilMap = getMapByHeader($input, 'seed-to-soil map:');
$soilToFertilizerMap = getMapByHeader($input, 'soil-to-fertilizer map:');
$fertilizerToToWaterMap = getMapByHeader($input, 'fertilizer-to-water map:');
$waterToLightMap = getMapByHeader($input, 'water-to-light map:');
$lightToTemperatureMap = getMapByHeader($input, 'light-to-temperature map:');
<?php
declare(strict_types=1);
$input = file('input.txt');
//$input = file('sample.txt');
$times = array_filter(explode(' ', trim(substr($input[0], strpos($input[0], ':') + 1))));
$distances = array_filter(explode(' ', trim(substr($input[1], strpos($input[1], ':') + 1))));
$races = array_combine($times, $distances);
$part1Answer = 1;
@mrbellek
mrbellek / day8.php
Last active January 6, 2024 13:43
Advent of Code - day 8
<?php
declare(strict_types=1);
//$input = file('sample.txt', FILE_IGNORE_NEW_LINES);
//$input = file('sample-part2.txt', FILE_IGNORE_NEW_LINES);
$input = file('input.txt', FILE_IGNORE_NEW_LINES);
$instructions = $input[0];
printf('Instructions for %d lines: %s' . PHP_EOL, count($input) - 2, $instructions);
$nodes = parseNodes(array_slice($input, 2));
@mrbellek
mrbellek / day9.php
Created January 2, 2024 15:05
Advent of Code - day 9
<?php
declare(strict_types=1);
//$input = file('sample.txt', FILE_IGNORE_NEW_LINES);
$input = file('input.txt', FILE_IGNORE_NEW_LINES);
$totalNextNumbers = 0;
$totalPreviousNumbers = 0;
foreach ($input as $line) {
$numbers = explode(' ', $line);