54 lines
1.5 KiB
Rust
54 lines
1.5 KiB
Rust
use crate::days::Solution;
|
|
|
|
pub struct Day01;
|
|
|
|
impl Solution for Day01 {
|
|
type Input = Vec<i16>;
|
|
|
|
fn parse(&self, data: &str) -> Self::Input {
|
|
data.split("\n")
|
|
.filter_map(|l| {
|
|
let mut chars = l.chars();
|
|
let first = chars.next();
|
|
let num = chars.collect::<String>().parse::<i16>();
|
|
|
|
match (first, num) {
|
|
(Some('L'), Ok(n)) => Some(-1 * n),
|
|
(Some('R'), Ok(n)) => Some(n),
|
|
_ => None,
|
|
}
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn part1(&self, input: &Self::Input) -> usize {
|
|
input
|
|
.iter()
|
|
.fold((50, 0), |(sum, res), x| match (sum + x) % 100 {
|
|
0 => (0, res + 1),
|
|
newsum => (newsum, res),
|
|
})
|
|
.1
|
|
}
|
|
|
|
fn part2(&self, input: &Self::Input) -> usize {
|
|
input
|
|
.iter()
|
|
.fold((50, 0), |(pointer, res), x| {
|
|
let new_pointer = (pointer + x).rem_euclid(100);
|
|
let rem_x = x % 100;
|
|
(
|
|
(pointer + x).rem_euclid(100),
|
|
res + (x / 100).abs()
|
|
+ if (pointer != 0 && pointer + rem_x != new_pointer)
|
|
|| pointer + rem_x == 0
|
|
{
|
|
1
|
|
} else {
|
|
0
|
|
},
|
|
)
|
|
})
|
|
.1 as usize
|
|
}
|
|
}
|