day3 part2
This commit is contained in:
39
src/day3.rs
39
src/day3.rs
@@ -18,10 +18,48 @@ fn get_joltage(line: &str) -> u32 {
|
||||
result
|
||||
}
|
||||
|
||||
#[aoc(day3, part2)]
|
||||
pub fn part2(input: &str) -> u64 {
|
||||
input
|
||||
.split("\n")
|
||||
.map(|line| get_joltage_rec(line, 12, 0))
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn get_joltage_rec(line: &str, num: usize, acc: u64) -> u64 {
|
||||
if num < 1 {
|
||||
return acc;
|
||||
}
|
||||
let len = line.len();
|
||||
let (revpos, val) = line[..(len - num + 1)]
|
||||
.chars()
|
||||
.rev()
|
||||
.enumerate()
|
||||
.max_by(|x, y| x.1.cmp(&y.1))
|
||||
.unwrap();
|
||||
let pos = len - num - revpos;
|
||||
get_joltage_rec(
|
||||
&line[(pos + 1)..],
|
||||
num - 1,
|
||||
acc * 10 + val.to_digit(10).unwrap() as u64,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part2_1() {
|
||||
let input = String::from(
|
||||
"987654321111111
|
||||
811111111111119
|
||||
234234234234278
|
||||
818181911112111",
|
||||
);
|
||||
assert_eq!(part2(&input), 3121910778619)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part1_1() {
|
||||
let input = String::from(
|
||||
@@ -32,6 +70,7 @@ mod tests {
|
||||
);
|
||||
assert_eq!(part1(&input), 357)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part1_2() {
|
||||
let input = String::from(
|
||||
|
||||
Reference in New Issue
Block a user