day3 part1

This commit is contained in:
2025-12-03 22:32:22 +01:00
parent b106ffe622
commit 4c105832b7
3 changed files with 64 additions and 1 deletions

44
src/day3.rs Normal file
View File

@@ -0,0 +1,44 @@
#[aoc(day3, part1)]
pub fn part1(input: &str) -> u32 {
input.split("\n").map(|line| get_joltage(line)).sum()
}
fn get_joltage(line: &str) -> u32 {
let len = line.len();
let (revpos_1, val_1) = line[..(len - 1)]
.chars()
.rev()
.enumerate()
.max_by(|x, y| x.1.cmp(&y.1))
.unwrap();
let val_2 = line[(len - 1 - revpos_1)..].chars().max().unwrap();
let result = val_1.to_digit(10).unwrap() * 10 + val_2.to_digit(10).unwrap();
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1_1() {
let input = String::from(
"987654321111111
811111111111119
234234234234278
818181911112111",
);
assert_eq!(part1(&input), 357)
}
#[test]
fn test_part1_2() {
let input = String::from(
"9891111111111111111
111111111111111
818181911112111",
);
assert_eq!(part1(&input), 99 + 11 + 92)
}
}