This commit is contained in:
Xavier Morel
2024-11-12 18:33:11 +01:00
parent 0cdf94342f
commit 5778c56d16
2 changed files with 34 additions and 37 deletions

View File

@@ -6,7 +6,6 @@ advent_of_code::solution!(1);
use std::collections::HashSet; use std::collections::HashSet;
use std::hash::Hash; use std::hash::Hash;
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
enum Turn { enum Turn {
Left, Left,
@@ -32,19 +31,19 @@ struct Pos {
impl Pos { impl Pos {
fn walk_north(&mut self, length: u32) { fn walk_north(&mut self, length: u32) {
self.y = self.y.saturating_add_unsigned(length); self.y = self.y.saturating_add_unsigned(length);
} }
fn walk_south(&mut self, length: u32) { fn walk_south(&mut self, length: u32) {
self.y = self.y.saturating_sub_unsigned(length); self.y = self.y.saturating_sub_unsigned(length);
} }
fn walk_west(&mut self, length: u32) { fn walk_west(&mut self, length: u32) {
self.x = self.x.saturating_sub_unsigned(length); self.x = self.x.saturating_sub_unsigned(length);
} }
fn walk_east(&mut self, length: u32) { fn walk_east(&mut self, length: u32) {
self.x = self.x.saturating_add_unsigned(length); self.x = self.x.saturating_add_unsigned(length);
} }
fn distance_to_origin(self) -> u32 { fn distance_to_origin(self) -> u32 {
@@ -63,10 +62,7 @@ struct State {
impl State { impl State {
fn init() -> Self { fn init() -> Self {
Self { Self {
pos: Pos { pos: Pos { x: 0, y: 0 },
x: 0,
y: 0,
},
dir: FaceDir::N, dir: FaceDir::N,
} }
} }
@@ -98,16 +94,16 @@ impl State {
}; };
} }
fn next_location(&mut self, step: &Step) -> Self { fn next_location(&mut self, step: &Step) -> Self {
match step { match step {
Step(Turn::Left, length) => { Step(Turn::Left, length) => {
self.turn_left(); self.turn_left();
self.walk_for(*length); self.walk_for(*length);
}, }
Step(Turn::Right, length) => { Step(Turn::Right, length) => {
self.turn_right(); self.turn_right();
self.walk_for(*length); self.walk_for(*length);
}, }
}; };
*self *self
} }
@@ -117,22 +113,24 @@ impl State {
Step(Turn::Left, length) => { Step(Turn::Left, length) => {
self.turn_left(); self.turn_left();
length length
}, }
Step(Turn::Right, length) => { Step(Turn::Right, length) => {
self.turn_right(); self.turn_right();
length length
} }
}; };
(0..(*length)).map(|_| { (0..(*length))
self.walk_for(1); .map(|_| {
self.pos self.walk_for(1);
}).collect() self.pos
})
.collect()
} }
fn visit_locations(self, steps: Vec<Step>) -> Self { fn visit_locations(self, steps: Vec<Step>) -> Self {
steps steps.into_iter().fold(self, |mut state, step| {
.into_iter() State::next_location(&mut state, &step)
.fold(self, |mut state, step| State::next_location(&mut state, &step)) })
} }
fn scan_steps(&mut self, steps: Vec<Step>) -> Vec<Pos> { fn scan_steps(&mut self, steps: Vec<Step>) -> Vec<Pos> {
@@ -156,28 +154,24 @@ fn first_repeated_loc(vec: &Vec<Pos>) -> Option<&Pos> {
None None
} }
fn parse_input(_input: &str) -> Vec<Step> { fn parse_input(_input: &str) -> Vec<Step> {
_input _input
.to_string() .to_string()
.strip_suffix("\n") .strip_suffix("\n")
.unwrap_or(&_input) .unwrap_or(&_input)
.split(", ") .split(", ")
.map(|s| .map(|s| {
{ let (direction, value) = s.split_at(1);
let (direction, value) = s.split_at(1); let length: u32 = value
let length: u32 = value.parse().expect( .parse()
&format!( .expect(&format!("invalid length! {:?}", value).to_string());
"invalid length! {:?}", match direction {
value "L" => Step(Turn::Left, length),
).to_string()); "R" => Step(Turn::Right, length),
match direction { _ => panic!("invalid turn direction!"),
"L" => Step(Turn::Left, length),
"R" => Step(Turn::Right, length),
_ => panic!("invalid turn direction!"),
}
} }
).collect() })
.collect()
} }
pub fn part_one(input: &str) -> Option<u32> { pub fn part_one(input: &str) -> Option<u32> {
@@ -214,4 +208,3 @@ mod tests {
assert_eq!(result, Some(4)); assert_eq!(result, Some(4));
} }
} }

View File

@@ -106,7 +106,11 @@ fn main() {
AppArguments::Time { day, all, store } => time::handle(day, all, store), AppArguments::Time { day, all, store } => time::handle(day, all, store),
AppArguments::Download { day } => download::handle(day), AppArguments::Download { day } => download::handle(day),
AppArguments::Read { day } => read::handle(day), AppArguments::Read { day } => read::handle(day),
AppArguments::Scaffold { day, download, overwrite } => { AppArguments::Scaffold {
day,
download,
overwrite,
} => {
scaffold::handle(day, overwrite); scaffold::handle(day, overwrite);
if download { if download {
download::handle(day); download::handle(day);