Solve 03
This commit is contained in:
parent
4129bdd0d5
commit
90eeb5dc03
1 changed files with 59 additions and 0 deletions
59
src/bin/03.rs
Normal file
59
src/bin/03.rs
Normal 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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue