1
Fork 0
advent-of-code/source/day_02/mod.rs

99 lines
2.2 KiB
Rust
Raw Normal View History

2021-12-02 11:47:38 +00:00
use std::str::FromStr;
use color_eyre::{
eyre::{eyre, Error},
Result,
};
pub fn solve() -> Result<()> {
let input_data = include_str!("../../data/day_02.txt").trim();
println!("Day 02 Part 1: {}", part_1(input_data)?);
println!("Day 02 Part 2: {}", part_2(input_data)?);
Ok(())
}
#[derive(Debug)]
enum Command {
Forward(i32),
Down(i32),
Up(i32),
}
impl FromStr for Command {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut split = s.split(" ");
let command = split
.next()
.ok_or(eyre!("Command not found in line: {}", s))?;
let amount = split
.next()
.ok_or(eyre!("Amount not found in line: {}", s))?
.parse()?;
match command {
"forward" => Ok(Self::Forward(amount)),
"down" => Ok(Self::Down(amount)),
"up" => Ok(Self::Up(amount)),
2021-12-02 14:02:22 +00:00
_ => Err(eyre!("Unknown command: {}", s)),
2021-12-02 11:47:38 +00:00
}
}
}
#[derive(Debug, Default)]
struct Submarine {
aim: i32,
depth: i32,
horizontal_position: i32,
}
impl Submarine {
fn execute_command_1(&mut self, command: Command) {
match command {
Command::Forward(amount) => self.horizontal_position += amount,
Command::Down(amount) => self.depth += amount,
Command::Up(amount) => self.depth -= amount,
}
}
fn execute_command_2(&mut self, command: Command) {
match command {
Command::Forward(amount) => {
self.horizontal_position += amount;
self.depth += self.aim * amount;
}
Command::Down(amount) => self.aim += amount,
Command::Up(amount) => self.aim -= amount,
}
}
fn final_result(&self) -> i32 {
self.horizontal_position * self.depth
}
}
fn parse_commands(input: &str) -> Result<Vec<Command>> {
input.lines().map(Command::from_str).collect::<Result<_>>()
}
fn part_1(input: &str) -> Result<i32> {
let mut submarine = Submarine::default();
parse_commands(input)?
.into_iter()
.for_each(|command| submarine.execute_command_1(command));
Ok(submarine.final_result())
}
fn part_2(input: &str) -> Result<i32> {
let mut submarine = Submarine::default();
parse_commands(input)?
.into_iter()
.for_each(|command| submarine.execute_command_2(command));
Ok(submarine.final_result())
}