Day 1
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
7
Cargo.lock
generated
Normal file
7
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 = "aoc"
|
||||||
|
version = "0.1.0"
|
16
Cargo.toml
Normal file
16
Cargo.toml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
[package]
|
||||||
|
name = "aoc"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "one"
|
||||||
|
path = "src/01.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "two"
|
||||||
|
path = "src/02.rs"
|
54
src/01.rs
Normal file
54
src/01.rs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
use std::{fs::File, io::Read};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// ------ PART ONE --------
|
||||||
|
//
|
||||||
|
let filename = "input.txt";
|
||||||
|
let file_result = File::open(filename);
|
||||||
|
let mut file = match file_result {
|
||||||
|
Ok(file) => file,
|
||||||
|
Err(error) => panic!("{}", error),
|
||||||
|
};
|
||||||
|
let mut contents = String::new();
|
||||||
|
match file.read_to_string(&mut contents) {
|
||||||
|
Ok(_) => {},
|
||||||
|
Err(error) => panic!("{}", error),
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut elf : usize = 0;
|
||||||
|
let mut calories :Vec<i32> = Vec::new();
|
||||||
|
for line in contents.lines() {
|
||||||
|
if line.is_empty() {
|
||||||
|
elf += 1
|
||||||
|
} else {
|
||||||
|
// add mnissing vec element
|
||||||
|
if calories.len() == elf {
|
||||||
|
calories.push(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
let c = match line.parse::<u32>() {
|
||||||
|
Ok(result) => result,
|
||||||
|
Err(error) => panic!("bitch you got strings in your numbies {}", error),
|
||||||
|
};
|
||||||
|
|
||||||
|
calories[elf] += c as i32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let mut max = 0;
|
||||||
|
for c in calories {
|
||||||
|
if c > max {
|
||||||
|
max = c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("Max Calories is {}", max);
|
||||||
|
|
||||||
|
// ------ PART TWO --------
|
||||||
|
calories.sort();
|
||||||
|
println!("top{}", top_n(&calories, 1)+top_n(&calories, 2)+top_n(&calories, 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn top_n(vector : &Vec<i32>, n: u32)-> i32 {
|
||||||
|
return vector[vector.len() -(n as usize)];
|
||||||
|
}
|
Reference in New Issue
Block a user