day3: part 1

This commit is contained in:
2025-12-02 21:13:17 -08:00
parent fb24991102
commit 0ea40260c1
2 changed files with 55 additions and 0 deletions

54
src/day3.rs Normal file
View File

@@ -0,0 +1,54 @@
use aoc_runner_derive::{aoc, aoc_generator};
type Bank = Vec<u8>;
#[aoc_generator(day3)]
fn parse(input: &str) -> Vec<Bank> {
input
.lines()
.map(|bank| bank.bytes().map(|c| c - b'0').collect())
.collect()
}
fn max_joltage(bank: &Bank, n: usize) -> u64 {
assert_eq!(n, 2, "unimplemented");
let mut max = 0;
for start in 0..bank.len() - 1 {
let cur = bank[start] as u64 * 10u64.pow(n as u32 - 1)
+ *bank[start + 1..].iter().max().unwrap() as u64;
if cur > max {
max = cur
}
}
max
}
#[aoc(day3, part1)]
fn part1(input: &Vec<Bank>) -> u64 {
input.iter().map(|bank| max_joltage(bank, 2)).sum()
}
#[aoc(day3, part2)]
fn part2(input: &Vec<Bank>) -> u64 {
todo!()
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE: &str = "987654321111111
811111111111119
234234234234278
818181911112111";
#[test]
fn part1_example() {
assert_eq!(part1(&parse(EXAMPLE)), 357);
}
#[test]
fn part2_example() {
assert_eq!(part2(&parse(EXAMPLE)), 0);
}
}

View File

@@ -1,3 +1,4 @@
mod day3;
mod day1; mod day1;
mod day2; mod day2;