Advent of Code 2021 (Rust)

Overview

aoc-2021

Advent of Code 2021 with Rust.

To build and run the project

This project uses cargo-aoc. More detailed instructions can be found at that project's README file.

  1. Create an account at adventofcode.com
  2. Get the value for your session cookie and configure cargo-aoc:
cargo aoc credentials -s TOKEN
  1. Build and run the code with:
cargo aoc

Log

Day 1

I found out about the slice::windows method in Rust, which enables both parts of today's puzzle to be solved with functional programming.

For part 1, it's as simple as iterating the entries with a window of size 2, and then folding the result:

measurements.windows(2).fold(0, |total, window| {
  total + if window[1] > window[0] { 1 } else { 0 }
})

For part2 we need to first create triplets (which are windows of size 3), and then iterate over the list of triplets as if they were windows of size 2, to be able to access both the current triplet and the previous one:

let triplets: Vec<&[u64]> = measurements.windows(3).collect();
triplets.windows(2).fold(0, |total, window| {
  // ...
})

Day 2

I created an enum type to handle the different commands. This implements the trait std::convert::From to parse a string into a command. Overengineering? Yes, but… 🤷🏻‍♀️

pub enum Command {
  Forward(i64),
  Up(i64),
  Down(i64),
}

impl From<&str> for Command {
  fn from(raw: &str) -> Self {
    // ...
  }
}

For part 1, running the commands is as simple as do a match over that enum:

fn exec(&mut self, cmd: &Command) {
  match cmd {
    Command::Forward(delta) => self.x += delta,
    Command::Up(delta) => self.y -= delta,
    Command::Down(delta) => self.y += delta,
  }
}

For part 2, a new logic for handling the commands is introduced, plus a new variable into the state of the submarine (aim). So I decided to play with Rust traits a bit and have two different types for the different "submarines", each one implementing a trait that provides the exec method:

pub trait Submarine {
  fn exec(&mut self, cmd: &Command);
  fn run(&mut self, input: &[Command]) {
    // ...
  }
}

pub struct SubmarineV2 {
  x: i64,
  y: i64,
  aim: i64,
}

impl Submarine for SubmarineV2 {
  fn exec(&mut self, cmd: &Command) {
    // ...
  }
}

Day 3

Today's puzzle was a good opportunity to refresh bit shifts and masking in Rust.

For part 1, the key of my solution is this loop:

for i in 0..n_bits {
  let mask = 2_u32.pow(i);
  let ones = report.iter().filter(|x| *x & mask == mask).count();
  if ones > report.len() / 2 {
      gamma_rate += mask
  }
}

Let's say we want to get the value of the second bit (counting from the left) for a number, for instance 01110. We would need to build a mask that is 01000 so when we do and AND operation with those two we can get whichever value was at the position of the masking bit.

For part 2, we use the same masking idea and keep filtering down the numbers until we have only one left.

You might also like...
Solutions to Advent of Code 2021, coded in rust

Advent of Code 2021 (aoc-2021-rust) Solutions to Advent of Code 2021 (https://adventofcode.com/2021), coded as part of my efforts to learn Rust Run co

Repository with my Advent of Code 2021 puzzle solutions 🎄
Repository with my Advent of Code 2021 puzzle solutions 🎄

🎄 Advent of Code 2021 🎄 I decided to stick with Rust this year and try to improve a bit on it, I basically haven't used it since last year's AoC, so

Quick'n dirty macro set for advent of code 2021

AOC quick'n dirty macro set Those are implemented using quick'n dirty procedural macros and libraries. Running the program Use cargo run --release --

🦀 Advent of Code 2021

Advent of Code 2021 License Licensed under either of Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) MIT li

Advent of Code - 2021

Advent of Code - 2021 Solutions to the Advent of Code 2021 written in Rust using cargo-aoc. Instructions Install cargo-aoc with cargo install cargo-ao

Soluciones a los retos del Advent of Code 2021.

Advent of Code 2021 Soluciones a Advent of Code 2021. Repos de otros miembros de AoC Canarias: Ricardo Cárdenes Víctor Ruiz José Rodríguez Juan Ignaci

☃️ Learning Rust with AoC 2021 🎄https://adventofcode.com/2021/

🎄 Andrei's 2021 Advent of Code 🎄 Learning Goals Rust basics (vectors, arrays, math, etc.) Rust basic CLI Rust linear algebra and ndarrays (e.g., htt

Rust-advent - Learning Rust by solving advent of code challenges (Streaming live on Twitch every Monday)
Rust-advent - Learning Rust by solving advent of code challenges (Streaming live on Twitch every Monday)

Rust advent 🦀 🐚 Learning Rust by implementing solutions for Advent of Code problems. 🎥 HEY, we are live-streaming our attempts to solve the exercis

Code and Development environment for adventofcode.com - 2021 edition

aoc-2021 Warning Spoiler Alert! If you want to solve the aoc problems on your own, do not read any further. This repository contains solutions for the

Owner
Belén Albeza
Software engineer and game developer
Belén Albeza
Advent of Code 2021 (Rust)

aoc-2021 Advent of Code 2021 with Rust. To build and run the project This project uses cargo-aoc. More detailed instructions can be found at that proj

Belén Albeza 13 Dec 23, 2021
Advent of Code 2021, also an attempt to practice a bit of Rust.

Advent of Code 2021 Advent of Code 2021 (my first one!), also an attempt to practice a bit of Rust. Running (Assuming that the respective inputs are i

Edoardo Debenedetti 1 Dec 3, 2021
My solutions to Advent of Code 2021 (mostly in rust)

Advent of Code 2021 Small code to solve problems at https://adventofcode.com/2021. Most of the code are written in Rust. How to run solutions For exam

“Plane” Abhabongse  Janthong 3 Apr 22, 2022
Learning Rust through Advent of Code 2021 - probably not very clean!

AoC 2021 ======== I'm using AoC2021 as an excuse to learn Rust (and maybe some other languages). Please do *not* use this repository as a good source

Andrew Zhu 0 Dec 8, 2021
🎄My Advent of Code 2021 solutions in the Rust programming language

Advent of Code 2021 in Rust My Advent of Code 2021 solutions in the Rust programming language. This repository holds a separate Rust project for each

Tim Visée 227 Dec 16, 2022
My solutions for the Advent of Code 2021 in Scala, Python, Haskell and Rust.

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 Scal

Axel Suárez 2 Oct 14, 2022
Advent of Code 2021, in rust this year

Advent of Code 2021 ?? Solutions for the 2021 edition of Advent of Code. This year, I try to do this in Rust ?? . I discover the language, so do not t

Florian Gaudin-Delrieu 1 Aug 12, 2022
My solutions for Advent of Code 2021, in Rust

Advent of Code 2021 These are my solutions. I have decided to use Rust for now. I'm new to Rust, so it might be some of the worst Rust code you've see

Samyak Sarnayak 1 Jan 1, 2022
My solution for the advent of code 2021, mainly written in Rust

Advent-of-Code-2021 My solution for the advent of code 2021, written in Rust Error Handle NOPE!!! unwrap() everything everywhere Use To run all of the

Nguyen Le Duy 0 Jan 8, 2022
My solutions for the 2021 edition of the Advent of Code, using Rust and SOM (Simple Object Machine)

Advent of Code 2021 These are my solutions for the 2021 edition of the Advent of Code. The solutions are all implemented using both Rust and SOM (Simp

Nicolas Polomack 1 Dec 23, 2021