1
0
Fork 0
This commit is contained in:
Lars Martens 2025-12-03 08:12:32 +01:00
parent 4129bdd0d5
commit 90eeb5dc03
Signed by: haselkern
GPG key ID: B5CF1F363C179AD4

59
src/bin/03.rs Normal file
View file

@ -0,0 +1,59 @@
use aoc::*;
const INPUT: &str = include_str!("../../input/03");
fn main() {
println!("Part 1: {}", part1(INPUT));
println!("Part 2: {}", part2(INPUT));
}
#[test]
fn example() {
assert_example!(part1, "03-test", 357);
assert_example!(part2, "03-test", 3121910778619);
}
fn part1(input: &str) -> i64 {
parse_input(input).map(|b| b.max_joltage(2)).sum()
}
fn part2(input: &str) -> i64 {
parse_input(input).map(|b| b.max_joltage(12)).sum()
}
struct Bank {
joltages: Vec<i64>,
}
impl Bank {
fn max_joltage(&self, batteries: usize) -> i64 {
let mut sum = 0;
let mut lower_bound = 0;
for b in 0..batteries {
let mut max_joltage = 0;
let upper_bound = self.joltages.len() - batteries + b;
for i in lower_bound..=upper_bound {
if self.joltages[i] > max_joltage {
max_joltage = self.joltages[i];
lower_bound = i + 1;
}
}
sum = sum * 10 + max_joltage;
}
sum
}
fn parse(line: &str) -> Self {
let joltages = line
.chars()
.map(|c| c.to_digit(10).unwrap() as i64)
.collect();
Self { joltages }
}
}
fn parse_input(input: &str) -> impl Iterator<Item = Bank> + use<'_> {
input.lines().map(Bank::parse)
}