mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 14:31:34 +01:00
Solve 2025 day 3 part 1 in rust
This commit is contained in:
parent
71794dd9c9
commit
586e674325
4 changed files with 67 additions and 3 deletions
7
2025/rust/day-03/Cargo.lock
generated
Normal file
7
2025/rust/day-03/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "day-03"
|
||||
version = "0.1.0"
|
||||
6
2025/rust/day-03/Cargo.toml
Normal file
6
2025/rust/day-03/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "day-03"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
51
2025/rust/day-03/src/main.rs
Normal file
51
2025/rust/day-03/src/main.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let banks = parse_input(&fs::read_to_string("../../inputs/03.txt").unwrap());
|
||||
|
||||
println!("Total output joltage: {}", total_joltage(banks));
|
||||
}
|
||||
|
||||
fn parse_input(contents: &str) -> Vec<Vec<u8>> {
|
||||
contents
|
||||
.trim()
|
||||
.lines()
|
||||
.map(|line: &str| line.bytes().map(|b| b - b'0').collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn total_joltage(banks: Vec<Vec<u8>>) -> u32 {
|
||||
banks.iter().map(|bank| largest_joltage(bank)).sum()
|
||||
}
|
||||
|
||||
fn largest_joltage(batteries: &[u8]) -> u32 {
|
||||
let mut first = batteries[0];
|
||||
let mut second = batteries[1];
|
||||
|
||||
for i in 1..batteries.len() - 1 {
|
||||
if batteries[i] > first {
|
||||
first = batteries[i];
|
||||
second = batteries[i + 1];
|
||||
} else if batteries[i + 1] > second {
|
||||
second = batteries[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
(first * 10 + second) as u32
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const TEST_INPUT: &str = "987654321111111
|
||||
811111111111119
|
||||
234234234234278
|
||||
818181911112111
|
||||
";
|
||||
|
||||
#[test]
|
||||
fn test_part1() {
|
||||
assert_eq!(total_joltage(parse_input(TEST_INPUT)), 357);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue