generated from luke-else/AdventOfCodeXXXX
feat: Completed Day 1 YAY
All checks were successful
Continuous integration / Check (push) Successful in 43s
Continuous integration / Test Suite (push) Successful in 44s
Continuous integration / Rustfmt (push) Successful in 31s
Continuous integration / Clippy (push) Successful in 44s
Continuous integration / build (push) Successful in 45s
All checks were successful
Continuous integration / Check (push) Successful in 43s
Continuous integration / Test Suite (push) Successful in 44s
Continuous integration / Rustfmt (push) Successful in 31s
Continuous integration / Clippy (push) Successful in 44s
Continuous integration / build (push) Successful in 45s
This commit is contained in:
@@ -5,16 +5,16 @@ pub struct Day01 {}
|
||||
impl Solution for Day01 {
|
||||
fn part1(
|
||||
&self,
|
||||
_input: &mut Vec<String>,
|
||||
input: &mut Vec<String>,
|
||||
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
|
||||
Ok(Box::new("Ready"))
|
||||
Ok(Box::new(Day01::count_zeros(input, false)))
|
||||
}
|
||||
|
||||
fn part2(
|
||||
&self,
|
||||
_input: &mut Vec<String>,
|
||||
input: &mut Vec<String>,
|
||||
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
|
||||
Ok(Box::new("Ready"))
|
||||
Ok(Box::new(Day01::count_zeros(input, true)))
|
||||
}
|
||||
|
||||
fn get_day(&self) -> u8 {
|
||||
@@ -22,7 +22,59 @@ impl Solution for Day01 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Day01 {}
|
||||
impl Day01 {
|
||||
fn count_zeros(input: &Vec<String>, count_passing: bool) -> usize {
|
||||
// Dial starts at 50
|
||||
let mut pos: i64 = 50;
|
||||
let mut zeros: usize = 0;
|
||||
|
||||
for line in input {
|
||||
let line = line.trim();
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut chars = line.chars();
|
||||
let dir = chars.next().expect("empty line");
|
||||
let dist_str: String = chars.collect::<String>().trim().to_string();
|
||||
let dist: i64 = dist_str.parse().expect("invalid distance number");
|
||||
|
||||
// Count any time that the dial moves past 0.
|
||||
if count_passing {
|
||||
// Count all zeros passed during the move
|
||||
let step = match dir {
|
||||
'L' | 'l' => -1,
|
||||
'R' | 'r' => 1,
|
||||
_ => panic!("invalid direction (must start with L or R): {}", dir),
|
||||
};
|
||||
|
||||
for _ in 0..dist {
|
||||
pos = (pos + step).rem_euclid(100);
|
||||
if pos == 0 {
|
||||
zeros += 1;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Move directly to the new position
|
||||
match dir {
|
||||
'L' | 'l' => {
|
||||
pos = (pos - dist).rem_euclid(100);
|
||||
}
|
||||
'R' | 'r' => {
|
||||
pos = (pos + dist).rem_euclid(100);
|
||||
}
|
||||
_ => panic!("invalid direction (must start with L or R): {}", dir),
|
||||
}
|
||||
|
||||
if pos == 0 {
|
||||
zeros += 1;
|
||||
}
|
||||
}
|
||||
zeros
|
||||
}
|
||||
}
|
||||
|
||||
/// Test from puzzle input
|
||||
#[cfg(test)]
|
||||
@@ -44,7 +96,7 @@ mod test {
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
assert_eq!(answer, "3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -61,6 +113,6 @@ mod test {
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
assert_eq!(answer, "6");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user