commit 4608ca2230a2b38ac041bd166d96f38f3ce5e212 Author: Christian Nieves Date: Thu Dec 8 20:04:37 2022 -0600 Day 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..a5b53ef --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..08babb8 --- /dev/null +++ b/Cargo.toml @@ -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" diff --git a/src/01.rs b/src/01.rs new file mode 100644 index 0000000..fc2c291 --- /dev/null +++ b/src/01.rs @@ -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 = 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::() { + 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, n: u32)-> i32 { + return vector[vector.len() -(n as usize)]; +}