Add solution for "Day 1: Secret Entrance", part 1
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
use crate::solvers::Solver;
|
||||
use std::io::BufRead;
|
||||
|
||||
pub struct SecretEntrance {}
|
||||
|
||||
impl SecretEntrance {
|
||||
const START_POSITION: i32 = 50;
|
||||
const DIAL_SIZE: i32 = 100;
|
||||
const COUNT_POSITION: i32 = 0;
|
||||
const POSITIVE_SIGN_CHAR: char = 'R';
|
||||
const NEGATIVE_SIGN_CHAR: char = 'L';
|
||||
}
|
||||
|
||||
impl Solver for SecretEntrance {
|
||||
const PUZZLE_INDEX: u8 = 1;
|
||||
const PUZZLE_NAME: &'static str = "Secret Entrance";
|
||||
|
||||
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64) {
|
||||
let mut position = Self::START_POSITION;
|
||||
let mut count = 0;
|
||||
let format = format!(
|
||||
"{{[{}{}]}}{{d}}",
|
||||
Self::POSITIVE_SIGN_CHAR,
|
||||
Self::NEGATIVE_SIGN_CHAR
|
||||
);
|
||||
for line in reader.lines().map_while(Result::ok) {
|
||||
if let Ok((sign, distance)) = scan_fmt!(&line, &format, char, i32) {
|
||||
position = (position
|
||||
+ if sign == Self::POSITIVE_SIGN_CHAR {
|
||||
distance
|
||||
} else {
|
||||
-distance
|
||||
})
|
||||
.rem_euclid(Self::DIAL_SIZE);
|
||||
}
|
||||
if position == Self::COUNT_POSITION {
|
||||
count = count + 1;
|
||||
}
|
||||
}
|
||||
(count, 0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user