Add day1 part 2
This commit is contained in:
parent
e6471a3c3f
commit
8b407fe5e0
1 changed files with 22 additions and 5 deletions
|
|
@ -7,15 +7,15 @@ impl Solution for Day01 {
|
||||||
|
|
||||||
fn parse(&self, data: &str) -> Self::Input {
|
fn parse(&self, data: &str) -> Self::Input {
|
||||||
data.split("\n")
|
data.split("\n")
|
||||||
.map(|l| {
|
.filter_map(|l| {
|
||||||
let mut chars = l.chars();
|
let mut chars = l.chars();
|
||||||
let first = chars.next();
|
let first = chars.next();
|
||||||
let num = chars.collect::<String>().parse::<i16>();
|
let num = chars.collect::<String>().parse::<i16>();
|
||||||
|
|
||||||
match (first, num) {
|
match (first, num) {
|
||||||
(Some('L'), Ok(n)) => -1 * n,
|
(Some('L'), Ok(n)) => Some(-1 * n),
|
||||||
(Some('R'), Ok(n)) => n,
|
(Some('R'), Ok(n)) => Some(n),
|
||||||
_ => 0,
|
_ => None,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
|
|
@ -32,6 +32,23 @@ impl Solution for Day01 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(&self, input: &Self::Input) -> usize {
|
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
|
0
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.1 as usize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue