From 428b3da1d911506b35545dc4c406ee549e88f15f Mon Sep 17 00:00:00 2001 From: Codinget Date: Thu, 1 Dec 2022 19:08:18 +0000 Subject: [PATCH] day 1 --- .gitignore | 1 + d1/.gitignore | 1 + d1/Cargo.lock | 7 +++++++ d1/Cargo.toml | 8 ++++++++ d1/src/main.rs | 31 +++++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+) create mode 100644 .gitignore create mode 100644 d1/.gitignore create mode 100644 d1/Cargo.lock create mode 100644 d1/Cargo.toml create mode 100644 d1/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..06c798b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +input.txt diff --git a/d1/.gitignore b/d1/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/d1/.gitignore @@ -0,0 +1 @@ +/target diff --git a/d1/Cargo.lock b/d1/Cargo.lock new file mode 100644 index 0000000..dda6202 --- /dev/null +++ b/d1/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "d1" +version = "0.1.0" diff --git a/d1/Cargo.toml b/d1/Cargo.toml new file mode 100644 index 0000000..4fb6a1c --- /dev/null +++ b/d1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d1" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/d1/src/main.rs b/d1/src/main.rs new file mode 100644 index 0000000..fa1dfec --- /dev/null +++ b/d1/src/main.rs @@ -0,0 +1,31 @@ +use std::fs::read_to_string; +use std::io::Error; + +fn main() -> Result<(), Error> { + let mut max = 0; + let mut all = Vec::new(); + let mut current = 0; + + for line in read_to_string("input.txt")?.split("\n") { + if line == "" { + if current >= max { + max = current + } + all.push(current); + current = 0 + } else { + current += line.parse::().unwrap() + } + } + if current >= max { + max = current + } + all.push(current); + + all.sort_by(|a, b| b.cmp(a)); + + println!("part 1: {}", max); + println!("part 2: {}", all.iter().take(3).sum::()); + + Ok(()) +}