advent-of-code/2022/01/rust/main.rs

54 lines
1.1 KiB
Rust
Raw Normal View History

2023-11-18 16:52:07 +00:00
#![feature(test)]
extern crate test;
#[cfg(test)]
mod tests {
use super::*;
2024-12-08 20:34:32 +00:00
use test::{black_box, Bencher};
2023-11-18 16:52:07 +00:00
#[bench]
fn bench_a(b: &mut Bencher) {
2024-12-08 20:34:32 +00:00
b.iter(|| black_box(part_a(INPUT)));
2023-11-18 16:52:07 +00:00
}
#[bench]
fn bench_b(b: &mut Bencher) {
2024-12-08 20:34:32 +00:00
b.iter(|| black_box(part_b(INPUT)));
2023-11-18 16:52:07 +00:00
}
}
2023-11-19 15:51:29 +00:00
const INPUT: &str = include_str!("../input.txt");
const TEST: &str = include_str!("../test.txt");
fn part_a(input: &str) {
let result: u32 = input
2023-11-18 15:45:50 +00:00
.trim()
.split("\n\n")
.map(|x| x.lines().map(|x| x.parse::<u32>().unwrap()).sum())
.max()
.unwrap();
2023-11-19 15:51:29 +00:00
println!("{}", result);
2023-11-18 15:45:50 +00:00
}
2023-11-18 16:52:07 +00:00
2023-11-19 15:51:29 +00:00
fn part_b(input: &str) {
let mut result = input
2023-11-18 16:52:07 +00:00
.trim()
.split("\n\n")
.map(|x| x.lines().map(|x| x.parse::<u32>().unwrap()).sum::<u32>())
2023-11-18 17:00:01 +00:00
.collect::<Vec<u32>>();
result.sort_unstable();
let total = result.into_iter().rev().take(3).sum::<u32>();
2023-11-19 15:51:29 +00:00
println!("{}", total);
2023-11-18 16:52:07 +00:00
}
fn main() {
2023-11-19 15:51:29 +00:00
println!("Part A:");
part_a(TEST);
part_a(INPUT);
println!("\nPart B:");
part_b(TEST);
part_b(INPUT);
2023-11-18 16:52:07 +00:00
}