Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

MarcusCemes/advent-of-code-2023

Repository files navigation

🎄 Advent Of Code 2023

rust logo solutions

Hi! These are my Rust solutions for the Advent of Code 2023.

Day Name Source Part 1 Part 2 Time 1 Time 2
1 Trebuchet?! 01.rs 33.1 µs 42.1 µs
2 Cube Conundrum 02.rs 26.5 µs 37.2 µs
3 Gear Ratios 03.rs 39.1 µs 30.7 µs
4 Scratchcards 04.rs 82.4 µs 84.0 µs
5 If You Give A Seed A Fertilizer 05.rs 18.7 µs 23.5 µs
6 Wait For It 06.rs 174 ns 180 ns
7 Camel Cards 07.rs 150 µs 157 µs
8 Haunted Wasteland 08.rs 193 µs 1.2 ms
9 Mirage Maintenance 09.rs 99.5 µs 96.5 µs
10 Pipe Maze 10.rs 87.6 µs¹ 138 µs¹
11 Cosmic Expansion 11.rs 3.1 ms 3.1 ms
12 Hot Springs 12.rs 1.7 ms 21.3 ms
13 Point of Incidence 13.rs 46.0 µs 48.5 µs
14 Parabolic Reflector Dish 14.rs 48.8 µs 25.6 ms
15 Lens Library 15.rs 54.8 µs 266 µs
16 The Floor Will Be Lava 16.rs 664 µs 162 ms
17 Clumsy Crucible 17.rs 18.1 ms 58.3 ms
18 Lavaduct Lagoon 18.rs 19.2 µs 26.1 µs
19 Aplenty 19.rs 229 µs 195 µs
...

Key: ⭐ Completed    🎁 In progress    😔 Gave up

Benchmarked on Intel i7-11800H @ 2.30 GHz (over many samples).

¹ I noticed after some refactoring that benchmark times got ~2x slower, with the simpler part taking significantly longer. A ~2x speed increase (relative to initial benchmark) was obtained by only testing one part at a time (with the other commented out for dead-code removal)! This may be an extreme sensitivity to the layout of the linked binary and how this is loaded into the instruction cache? Or bad branch prediction?

Verdict

Doing this year in Rust was fun. As expected, the result is fast and efficient. Seeing solutions complete in microseconds, or even nanoseconds, was satisfying to say the least. I was surprised by how much I enjoyed using iterators, maybe a little too much. However, it's easy to get lost in premature optimization, static dispatch and avoiding heap allocation. Knowing your data structures, and choosing the right one for the job, makes a much more significant difference than the language itself. Unit tests were brilliant for testing solutions, before running them on the input data. The language is great for expressing the problem domain, although I do miss the conciseness of solutions in other languages (such as Python) that are often a fraction of the number of lines. The time saved writing such a solution is a valid trade off, despite taking orders of magnitude longer to run (i.e. a few seconds...).

Usage

Install Rust.

Run the following commands to run the project:

# Test all solutions on example data
cargo test

# Run all solutions with optimisations
cargo all --release

# Benchmark solutions for day 1
cargo solve 1 --release --time

The Rust compiler will automatically download the required dependencies and compile each solution into its own binary that can be found in the target/debug or target/release directory, depending on whether the --release flag was used.

To read the problem input from stdin, instead of a hardcoded file path, make a small modification to the solution macro in day.rs that steps up the main function of each binary:

- let input = advent_of_code::template::read_input(DAY);
+ let input = advent_of_code::template::read_stdin();

Acknowledgments

This repository uses a modified version of this template. Thanks Felix!

License

Distributed under the MIT Licence. See LICENCE for more information.

About

My solutions for the Advent of Code 2023.

Topics

Resources

License

Stars

Watchers

Forks

Languages