Day 1 done

This commit is contained in:
Sebastian 2022-12-01 18:47:25 +01:00
commit 2a2043db7f
5 changed files with 2297 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "AoC2022"
version = "0.1.0"

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "AoC2022"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

2243
inputs/day1.txt Normal file

File diff suppressed because it is too large Load Diff

38
src/bin/day1.rs Normal file
View File

@ -0,0 +1,38 @@
use std::error::Error;
use std::fs::File;
use std::io::{self, BufRead};
use std::vec::Vec;
fn main() -> Result<(), Box<dyn Error>> {
let file = File::open("inputs/day1.txt")?;
let lines = io::BufReader::new(file).lines().map(|l| l.unwrap());
let mut elves = Vec::<Vec<u32>>::new();
let mut current_elf = Vec::<u32>::new();
for line in lines {
if line != "" {
let calories = line.parse().unwrap();
current_elf.push(calories);
} else {
elves.push(current_elf);
current_elf = Vec::<u32>::new();
}
}
let mut calories_per_elf: Vec<u32> = elves
.iter()
.map(|e| e.iter().fold(0, |a, b| a + b))
.collect();
calories_per_elf.sort();
calories_per_elf.reverse();
println!("Answer Part1: {}", calories_per_elf[0]);
let answer2 = calories_per_elf[0] + calories_per_elf[1] + calories_per_elf[2];
println!("Answer Part2: {}", answer2);
Ok(())
}