1
Fork 0
advent-of-code/source/year_2020/day_12/extra.rs

56 lines
1.3 KiB
Rust

//! [`Action`] and [`Instruction`] code.
/// The list of actions the ship can take.
#[derive(Debug, Eq, PartialEq)]
pub enum Action {
/// Move North by a given value.
North,
/// Move East by a given value.
East,
/// Move South by a given value.
South,
/// Move West by a given value.
West,
/// Turn left by a given number of degrees.
Left,
/// Turn right by a given number of degrees.
Right,
/// Move forward by a given value in the direction the ship is facing.
Forward,
}
impl From<char> for Action {
fn from(input: char) -> Self {
match input {
'N' => Self::North,
'E' => Self::East,
'S' => Self::South,
'W' => Self::West,
'L' => Self::Left,
'R' => Self::Right,
'F' => Self::Forward,
_ => unreachable!("{input}"),
}
}
}
/// The combination of an [`Action`] and the value for it.
#[derive(Debug)]
pub struct Instruction {
/// Which action to take.
pub action: Action,
/// The amount to use for that action.
pub value: i32,
}
impl From<&str> for Instruction {
fn from(input: &str) -> Self {
let mut chars = input.chars();
let action = chars.next().map(Into::into).unwrap();
let value = chars.collect::<String>().parse().unwrap();
Self { action, value }
}
}