Day 1 puzzle
commit
8f28ad0005
|
@ -0,0 +1 @@
|
|||
/target
|
|
@ -0,0 +1,14 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aocrust"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"day1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "aocrust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[workspace]
|
||||
members = [
|
||||
"day1"
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
day1 = { path = "./day1" }
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,30 @@
|
|||
use std::fs;
|
||||
|
||||
pub fn start() {
|
||||
let input = fs::read_to_string("./day1/src/input.txt").unwrap();
|
||||
let elves = input.split("\n");
|
||||
let mut elves_cals: Vec<(u32, u32)> = vec![];
|
||||
let mut i = 1;
|
||||
let mut sum = 0;
|
||||
for cal in elves {
|
||||
match cal.parse::<u32>() {
|
||||
Ok(cal) => sum += cal,
|
||||
Err(_) => {
|
||||
elves_cals.push((i, sum));
|
||||
i += 1;
|
||||
sum = 0;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
elves_cals.sort_by(|a, b| b.1.cmp(&a.1));
|
||||
|
||||
println!("{:?}", elves_cals.get(0).unwrap());
|
||||
|
||||
let res: u32 = elves_cals
|
||||
.iter()
|
||||
.map(|f| f.1)
|
||||
.take(3).sum();
|
||||
|
||||
println!("{:?}", res);
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
use day1;
|
||||
|
||||
fn main() {
|
||||
day1::start();
|
||||
}
|
Reference in New Issue