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

56 lines
1.3 KiB
Rust
Raw Normal View History

2024-01-14 21:04:10 +00:00
//! [`Action`] and [`Instruction`] code.
/// The list of actions the ship can take.
2022-10-03 16:02:40 +00:00
#[derive(Debug, Eq, PartialEq)]
pub enum Action {
2024-01-14 21:04:10 +00:00
/// Move North by a given value.
2022-10-03 16:02:40 +00:00
North,
2024-01-14 21:04:10 +00:00
/// Move East by a given value.
2022-10-03 16:02:40 +00:00
East,
2024-01-14 21:04:10 +00:00
/// Move South by a given value.
2022-10-03 16:02:40 +00:00
South,
2024-01-14 21:04:10 +00:00
/// Move West by a given value.
2022-10-03 16:02:40 +00:00
West,
2024-01-14 21:04:10 +00:00
/// Turn left by a given number of degrees.
2022-10-03 16:02:40 +00:00
Left,
2024-01-14 21:04:10 +00:00
/// Turn right by a given number of degrees.
2022-10-03 16:02:40 +00:00
Right,
2024-01-14 21:04:10 +00:00
/// Move forward by a given value in the direction the ship is facing.
2022-10-03 16:02:40 +00:00
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}"),
}
}
}
2024-01-14 21:04:10 +00:00
/// The combination of an [`Action`] and the value for it.
2022-10-03 16:02:40 +00:00
#[derive(Debug)]
pub struct Instruction {
2024-01-14 21:04:10 +00:00
/// Which action to take.
2022-10-03 16:02:40 +00:00
pub action: Action,
2024-01-14 21:04:10 +00:00
/// The amount to use for that action.
2022-10-03 16:02:40 +00:00
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 }
}
}