2021 Day 1


Part 1 Problem Take a list of numbers from a file (one per line) and count the occasions where a number is greater than the one before it. General solution Read in each line and store in a variable length array (or similar structure, depending on the language). Iterate over the array and count all instances where a[index] > a[index - 1], excluding index == 0. Alternatively, read in each line and keep a record of the previous line to compare with.…
Read more ⟶

2020 Day 5


Part 1 Problem From a list of seat specifiers, find the seat with the highest ID. General solution Read in each line of the file and covert it to an array. For each seat (one seat per line, so one item in the array), split at the seventh character to get the row and column specifiers. Break these down using binary space partitioning (similar to binary searching, as the input is ordered but in this case we do not know what the target number is) to find the row and column number, then calculate the seat ID.…
Read more ⟶

2015 Day 4


Part 1 Problem COMPLETE General solution COMPLETE Solution: PHP COMPLETE <?php declare(strict_types=1); error_reporting(E_ALL); $secret_key = trim(file_get_contents('input')); $number = 0; $leading_zero_count = 5; $leading_zero_str = str_repeat('0', $leading_zero_count); while (true) { $str = $secret_key . strval($number); $hash = md5($str); if (substr($hash, 0, $leading_zero_count) === $leading_zero_str) { break; } $number++; } print("Lowest number to mine AdventCoin: $number\n"); Part 2 Problem COMPLETE Solution: PHP COMPLETE <?php declare(strict_types=1); error_reporting(E_ALL); $secret_key = trim(file_get_contents('input')); $number = 0; $leading_zero_count = 6; $leading_zero_str = str_repeat('0', $leading_zero_count); while (true) { $str = $secret_key .…
Read more ⟶

2015 Day 3


Part 1 Problem COMPLETE General solution COMPLETE Solution: PHP COMPLETE <?php declare(strict_types=1); error_reporting(E_ALL); // Drop off a present at the starting point $x = 0; $y = 0; $presents[$x][$y] = 1; $houses_visited = 1; $fp = fopen('input', 'r'); while (($move = fgetc($fp)) !== false) { // Move to new location if ($move === '^') { $y++; } elseif ($move === 'v') { $y--; } elseif ($move === '<') { $x--; } elseif ($move === '>') { $x++; } else { die("Invalid move: $move\n"); } // Drop off a present at the new location if (isset($presents[$x][$y])) { $presents[$x][$y]++; } else { $presents[$x][$y] = 1; $houses_visited++; } } fclose($fp); print("Houses visited: $houses_visited\n"); Part 2 Problem COMPLETE…
Read more ⟶

2015 Day 2


Part 1 Problem COMPLETE General solution COMPLETE Solution: PHP COMPLETE <?php declare(strict_types=1); error_reporting(E_ALL); $fp = fopen('input', 'r'); $paper_order = 0; while (($line = fgets($fp)) !== false) { // fgets includes the trailing line break, so remove it $line = trim($line); list($length, $width, $height) = explode('x', $line); $side_areas = [ $length * $width, $width * $height, $height * $length ]; $slack = min($side_areas); // Each side is repeated twice $paper_order += array_sum($side_areas) * 2; $paper_order += $slack; } fclose($fp); print("The elves should order this many square feet of wrapping paper: $paper_order\n"); Part 2 Problem COMPLETE…
Read more ⟶

2015 Day 1


Part 1 Problem Take a list of characters from a file and find the final ‘floor’ that Santa ends up on, based on the characters. General solution Read in each character and increment or decrement the current floor. Solution: PHP Open the file and read in each line, changing the floor based on the character. <?php declare(strict_types=1); error_reporting(E_ALL); $floor = 0; $fp = fopen('input', 'r'); while (($char = fgetc($fp)) !== false) { if ($char === '(') { $floor++; } elseif ($char === ')') { $floor--; } } fclose($fp); print("Final floor: $floor\n"); Part 2 Problem As before, but find the location of the character which causes Santa to enter the basement, i.…
Read more ⟶