day1
parent
afaf4a1ddf
commit
38123f3a00
@ -1,2 +1,3 @@
|
||||
/target
|
||||
target/
|
||||
input
|
||||
testinput
|
||||
|
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day01"
|
||||
version = "0.1.0"
|
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day01"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
@ -0,0 +1,30 @@
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
fn main() {
|
||||
let lines = io::stdin().lock().lines().map(|l| l.unwrap());
|
||||
let mut sum = 0;
|
||||
let mut max = [0, 0, 0];
|
||||
|
||||
for line in lines {
|
||||
if let Ok(num) = line.parse::<usize>() {
|
||||
sum += num;
|
||||
} else {
|
||||
match (sum > max[0], sum > max[1], sum > max[2]) {
|
||||
(_, _, true) => {
|
||||
max.rotate_left(1);
|
||||
max[2] = sum;
|
||||
}
|
||||
(_, true, false) => {
|
||||
max[0] = max[1];
|
||||
max[1] = sum;
|
||||
}
|
||||
(true, false, false) => {
|
||||
max[0] = sum;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
sum = 0;
|
||||
}
|
||||
}
|
||||
println!("P1: {} P2: {}", max[2], max.iter().sum::<usize>());
|
||||
}
|
Loading…
Reference in New Issue