Day 1 puzzle

master
Wynd 2022-12-02 01:29:16 +02:00
commit 8f28ad0005
7 changed files with 2328 additions and 0 deletions

1
.gitignore vendored 100644
View File

@ -0,0 +1 @@
/target

14
Cargo.lock generated 100644
View File

@ -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"

14
Cargo.toml 100644
View File

@ -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" }

8
day1/Cargo.toml 100644
View File

@ -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]

2256
day1/src/input.txt 100644

File diff suppressed because it is too large Load Diff

30
day1/src/lib.rs 100644
View File

@ -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);
}

5
src/main.rs 100644
View File

@ -0,0 +1,5 @@
use day1;
fn main() {
day1::start();
}