back adding day 1:
This commit is contained in:
7
day1/Cargo.lock
generated
Normal file
7
day1/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 = "day1"
|
||||||
|
version = "0.1.0"
|
||||||
8
day1/Cargo.toml
Normal file
8
day1/Cargo.toml
Normal 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]
|
||||||
2240
day1/input
Normal file
2240
day1/input
Normal file
File diff suppressed because it is too large
Load Diff
36
day1/src/main.rs
Normal file
36
day1/src/main.rs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io::{prelude::*, BufReader};
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()>{
|
||||||
|
let file = File::open("input")?;
|
||||||
|
let reader = BufReader::new(file);
|
||||||
|
|
||||||
|
let mut elves = reader.lines()
|
||||||
|
.fold(Vec::new(), |mut acc, line| {
|
||||||
|
let line = line.unwrap();
|
||||||
|
//empty lines mean new elf
|
||||||
|
if line.is_empty() || acc.is_empty() {
|
||||||
|
acc.push(0_u64);
|
||||||
|
}
|
||||||
|
|
||||||
|
// the first time through is an edge case preventing an else here
|
||||||
|
if ! line.is_empty() {
|
||||||
|
let last = acc.last_mut().unwrap();
|
||||||
|
*last += line.parse::<u64>().unwrap();
|
||||||
|
}
|
||||||
|
acc
|
||||||
|
});
|
||||||
|
|
||||||
|
//order the elves since we don't care about position anymore
|
||||||
|
elves.sort();
|
||||||
|
let max = elves.len() - 1;
|
||||||
|
|
||||||
|
//part 1 is get the max
|
||||||
|
println!("Part 1: {}", elves[max]);
|
||||||
|
|
||||||
|
//Part 2 is get the sum of the largest 3
|
||||||
|
let counts: u64 = elves[(max-2)..].iter().sum();
|
||||||
|
println!("Part 2: {counts}");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user