Advent of Code 2021, in rust this year

Overview

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 take this as an idiomatic implementation.

What I learned:

  • Day01
    • organization of the project
    • using the lib.rs to expose modules
    • referencing them in main.rs.
    • Read a file with fs::read_to_string,
    • iterate the lines with .lines().
    • parse().unwrap() for a quick parsing of the line based on declared type in the function.
  • Day02
    • I used split_whitespace, but split_once(" ") would have been better, because I could unpack the result directly.
    • If you iter a borrowed vector, you get a double reference for arguments. You can unpack them with for &(x, y) to access the value as (here) &str, otherwise you get &&ref and you cannot compare those to string literals. Another solution is to use x.as_ref.
  • Day03
    • HashMap default value: hashmap.get(x).unwrap_or(0)
    • HashMap mutating with default value: hashmap.entry(w).or_insert(0) += 1
  • Day04
    • Converting a Vec into an array with try_into().unwrap().
  • Day08
    • Regex with the regex crate (along with lazy_static to avoid duplicate compilation of regexes).
    • HashSet exists.
  • Day09
    • Mutating a ref in a recursive function (say fn func(&mut input)) can be called internally with fn(input), fn(&mut input) is not necessary.
  • Day10
    • When building in release, integer will overflow with no warnings, u32 was not giving the right answer, u64 does...
  • Day11
    • You can use get_mut on an HashMap to get a mutable reference.
You might also like...
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

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

Owner
Florian Gaudin-Delrieu
Florian Gaudin-Delrieu
⭐ Advent of Code 2021: Мade with Rust

Advent of Code 2021: Мade with Rust When I was solving puzzles, my goal was to practice writing idiomatic Rust. My solutions do not claim to be the fa

Sergey Grishakov 13 Dec 2, 2021
Advent of Code 2021 puzzles & solutions in Rust

Advent of Code 2021 These are puzzles for the Advent of Code 2021 challenge, written and solved in the Rust programming language. The puzzle for each

Chevy Ray Johnston 8 Dec 19, 2021
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
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