day 1 in rust
This commit is contained in:
commit
21e32ff968
1
day01/.gitignore
vendored
Normal file
1
day01/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
7
day01/Cargo.lock
generated
Normal file
7
day01/Cargo.lock
generated
Normal file
@ -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"
|
8
day01/Cargo.toml
Normal file
8
day01/Cargo.toml
Normal file
@ -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]
|
14
day01/exampleInput.txt
Normal file
14
day01/exampleInput.txt
Normal file
@ -0,0 +1,14 @@
|
||||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
2248
day01/input.txt
Normal file
2248
day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
24
day01/src/main.rs
Normal file
24
day01/src/main.rs
Normal file
@ -0,0 +1,24 @@
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let file_path = "input.txt";
|
||||
println!("Reading file {}", file_path);
|
||||
let contents = fs::read_to_string(file_path).expect("Should have been able to read the file");
|
||||
let splitted: Vec<&str> = contents.split("\n\n").collect();
|
||||
let mut all_sums: Vec<i32> = vec![];
|
||||
for s in &splitted {
|
||||
let split_group: Vec<&str> = s.split("\n").collect();
|
||||
let mut sum_in_group: i32 = 0;
|
||||
for g in &split_group {
|
||||
sum_in_group += g.parse::<i32>().unwrap_or(0);
|
||||
}
|
||||
all_sums.push(sum_in_group);
|
||||
//println!("Sum: {}", sum_in_group.to_string());
|
||||
}
|
||||
|
||||
all_sums.sort();
|
||||
let top_three: Vec<i32> = all_sums[all_sums.len() - 3..].to_vec();
|
||||
println!("Top Three: {:?}", top_three);
|
||||
let sum: i32 = top_three.iter().sum();
|
||||
println!("{}", sum);
|
||||
}
|
Loading…
Reference in New Issue
Block a user