Skip to content

My solutions for the Advent of Code 2021 in Scala, Python, Haskell and Rust.

Notifications You must be signed in to change notification settings

ggzor/advent-of-code-2021

Repository files navigation

Advent of Code 2021

These are my Advent of Code 2021 solutions written in Scala 3, Haskell, Python and Rust.

Day Title L1 L2 L3 L4
01 Sonar Sweep Scala Haskell Python Rust
02 Dive! Scala Haskell Python Rust
03 Binary Diagnostic Scala Haskell Python Rust
04 Giant Squid Scala Haskell Python Rust
05 Hydrotermal Venture Scala Haskell Python Rust
06 Lanternfish Scala Haskell Python Rust
07 The Treachery of Whales Scala Haskell Python Rust
08 Seven Segment Search Scala Haskell Python Rust
09 Smoke Basin Scala Haskell Python Rust
10 Syntax Scoring Scala Haskell Python Rust
11 Dumbo Octopus Scala Haskell Python Rust
12 Passage Pathing Scala Haskell Python Rust
13 Transparent Origami Scala Haskell Python Rust
14 Extended Polymerization Scala Haskell Python Rust
15 Chiton Scala Haskell Python Rust
16 Packet Decoder Scala Haskell Python Rust
17 Trick Shot Scala Haskell Python Rust
18 Snailfish Scala Haskell Python Rust
19 Beacon Scanner Scala Haskell Python Rust
20 Trench Map Scala Haskell Python Rust
21 Dirac Dice Scala Haskell Python Rust
22 Reactor Reboot Scala Haskell Python Rust
23 Amphipod Scala Haskell Python Rust
24 Arithmetic Logic Unit Python
25 Sea Cucumber Scala Haskell Python Rust

Remarks

  • Scala solutions are short and concise, be sure to check them out.
  • The solutions are self contained, they can easily be executed as shown in the next section. This also makes them just a bit cluttered because the parsing and the part selection are inside the same file.
  • Both parts of each problem are solved in the same file, this requires a bit of generalization to solve both parts with almost the same code, and makes the code longer than other solutions.
  • These are not the shortest or fastest solutions, but they are readable and show the key ideas for solving each problem and how to translate them to each different language.
  • The Python solutions are a bit clunky because these are the ones I code first.
  • The solutions in Rust are not so good because this is the first time I use Rust for this kind of problems. It gets better day by day, but don't expect amazing code.

Running the solutions

The solutions are self-contained and should run easily using the appropriate interpreter/compiler for each language, keeping the following considerations in mind:

  • Input is taken from standard input.
  • Either 1 or 2 must be passed as arguments to choose which part to run.

Scala 3

For example, to run the Scala solution for the second part of the first day, you can use the following command:

cat day01/test01.txt | scala day01/Day01.scala 2

Haskell (>= 8.10)

No other libraries apart from base, containers and mtl are used, they should be installed by default.

cat day01/test01.txt | runghc day01/Day01.hs 2

Python (>= 3.8)

Only numpy is used apart from the standard library.

cat day01/test01.txt | python3 day01/Day01.py 2

Rust (nightly >=1.62)

To run a Rust solution you must first compile it and then run the resulting executable:

rustc day01/Day01.rs
cat day01/test01.txt | ./Day01 2