Skip to content

Solutions to Advent of Code 2023 in PHP

License

Notifications You must be signed in to change notification settings

leifgehrmann/advent-of-code-2023

Repository files navigation

advent-of-code-2023

My attempts at the Advent of Code 2023 challenges in PHP.

Solutions

Day Name Code Time †
01 Trebuchet?! src/Day01.php 0.002655s
02 Cube Conundrum src/Day02.php 0.001402s
03 Gear Ratios src/Day03.php 0.006805s
04 Scratchcards src/Day04.php 0.006466s
05 If You Give A Seed A Fertilizer src/Day05.php 0.002637s
06 Wait For It src/Day06.php 0.000059s
07 Camel Cards src/Day07.php 0.239075s
08 Haunted Wasteland src/Day08.php 0.066197s
09 Mirage Maintenance src/Day09.php 0.027672s
10 Pipe Maze src/Day10.php 0.023185s
11 Cosmic Expansion src/Day11.php 1.122419s
12 Hot Springs src/Day12.php Part 2 Unsolved
13 Point of Incidence src/Day13.php 0.015861s
14 Parabolic Reflector Dish src/Day14.php 0.996860s
15 Lens Library Not Started
16 The Floor Will Be Lava src/Day16.php 5.479180s

† The measured execution time in GitHub Actions

Installation

Installing on your machine

Installation with vary on each system. For a full environment the following dependencies should be installed:

Installing with Docker

Using Docker avoids the complexity of installing the dependencies listed above.

Once Docker is installed, a Docker image can be built by running:

make docker-build

Once built, an interactive shell with the project directory mounted can be started by running:

make shell

How to run

Before running the code, the PHP dependencies must be installed by running:

composer install

All puzzles can be executed by running:

php src/main.php

Individual puzzles can be executed by adding the day-number:

php src/main.php 01

The solutions can be benchmarked by adding the flag -b:

php src/main.php 01 -b

Why is the input data encrypted?

Note that the input data included in this repository have been encrypted, and exist purely for personal use. This is because the input data is the sole property of Advent of Code, and is not licensed for reproduction or distribution. To test with your own input, replace the contents of the file src/Day**.data with your own inputs from adventofcode.com.

If you somehow are in possession of the encryption key, you can unlock it using git-crypt by running:

echo "${BASE64_ENCODED_KEY}" | base64 -d | git-crypt unlock -

How to develop

As mentioned above, the code for each puzzle can be run individually. But this project has additional tools to help with development.

Tests

Test-driven-development using unit tests can help with solving a puzzle by ensuring individual functions work as expected. Tests can be added in tests/Unit/* and run using PHPUnit. The flags --testdox and --colors can help make the output more legible:

vendor/bin/phpunit tests/Unit/Day01Test.php --testdox --colors

This can be run quickly for all tests from the Makefile:

make tests

Note that some of the test data includes sample data from adventofcode.com, which have been encrypted for the same legal reasons mentioned above in "Why is the input data encrypted?". These tests will be skipped when attempting to run the tests.

Linting & Static Analysis

Following code standards can help make the code more legible and running static analysis tools can spot issues in the code. This project comes with PHP CodeSniffer and Psalm:

vendor/bin/phpcs -p --standard=PSR12 src/ tests/
vendor/bin/psalm --show-info=true

These can be run quickly from the Makefile:

make lint

Attribution