And day 2 was fun, I decided to make a ‘proper’ set of types of it instead of just churning in place, less efficient then it could be for sure, but fast enough to run it dozens of times in the span of a single eyeblink so meh, lol:
❯ cargo run --release -- -v 2021 2
Finished release [optimized] target(s) in 0.05s
Running `target/release/advent_of_code -v 2021 2`
Step 1: 1250395
Step 2: 1451210346
_Day2 Time Taken: 90.003µs_
_Time Taken: 92.593µs_
And the code (also available on my prior post github link), with still probably waaaay too much error checking but eh, I almost even used checked_add/sub
instead of the default overflowing +/-
but considered even that ‘too’ excessive for this, lol:
use crate::aoc::helpers::*;
use crate::AocApp;
use anyhow::Context;
use clap::Parser;
use std::num::NonZeroU8;
use std::path::PathBuf;
#[derive(Debug, Parser)]
pub struct Day2 {
/// The input file of "commands"
#[clap(default_value = "inputs/2021/day2.input")]
pub input_file: PathBuf,
}
enum Commands {
Forward(NonZeroU8),
Down(NonZeroU8),
Up(NonZeroU8),
}
#[derive(Default)]
struct Pos {
depth: u32,
fore: u32,
aim: u32,
}
impl Pos {
fn solution(&self) -> u32 {
self.depth * self.fore
}
}
impl Day2 {
pub fn run(&self, _app: &AocApp) -> anyhow::Result<()> {
let commands = map_trimmed_nonempty_lines_of_file(&self.input_file, |line| {
match line
.split_once(' ')
.context("input is not a command then space then a number")?
{
("forward", n) => Ok(Commands::Forward(
n.parse().context("input is not a number")?,
)),
("down", n) => Ok(Commands::Down(n.parse().context("input is not a number")?)),
("up", n) => Ok(Commands::Up(n.parse().context("input is not a number")?)),
_ => anyhow::bail!("input is not a valid command of forward|down|up then a number"),
}
})?;
println!(
"Step 1: {}",
commands
.iter()
.fold(Pos::default(), |mut pos, cmd| {
match cmd {
Commands::Forward(n) => pos.fore += n.get() as u32,
Commands::Down(n) => pos.depth += n.get() as u32,
Commands::Up(n) => pos.depth -= n.get() as u32,
}
pos
})
.solution()
);
println!(
"Step 2: {}",
commands
.iter()
.fold(Pos::default(), |mut pos, cmd| {
match cmd {
Commands::Down(n) => pos.aim += n.get() as u32,
Commands::Up(n) => pos.aim -= n.get() as u32,
Commands::Forward(n) => {
pos.fore += n.get() as u32;
pos.depth += pos.aim * n.get() as u32;
}
}
pos
})
.solution()
);
Ok(())
}
}
EDIT: And here’s the full output of run-all
so far:
OvermindDL1’s Advent Of Code
Year2015
Year2015 Time Taken: 40ns
Year2016
Year2016 Time Taken: 30ns
Year2017
Year2017 Time Taken: 30ns
Year2016
Year2016 Time Taken: 40ns
Year2019
Year2019 Time Taken: 30ns
Year2020
Year2020 - Day1
Step 1: 731731
Step 2: 116115990
Day1 Time Taken: 96.843µs
Year2020 - Day2
Step 1: 515
Step 2: 711
Day2 Time Taken: 214.024µs
Year2020 - Day3
Step 1: 250
Step 2: 1592662500
Day3 Time Taken: 134.3µs
Year2020 - Day4
Step 1: 206
Step 2: 123
Day4 Time Taken: 227.134µs
Year2020 Time Taken: 711.038µs
Year2021
Year2021 - Day1
Step 1: 1448
Step 2: 1471
Day1 Time Taken: 110.992µs
Year2021 - Day2
Step 1: 1250395
Step 2: 1451210346
Day2 Time Taken: 80.384µs
Year2021 Time Taken: 207.505µs
All Time Taken: 941.942µs