day 1 solution in rust
parent
6175397651
commit
7f02eb867c
|
@ -0,0 +1 @@
|
||||||
|
target
|
|
@ -0,0 +1,16 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.66"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aoc2022"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
]
|
|
@ -0,0 +1,9 @@
|
||||||
|
[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]
|
||||||
|
anyhow = "1.0.66"
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,27 @@
|
||||||
|
use anyhow::Result;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
pub fn run() -> Result<()> {
|
||||||
|
let file_contents = fs::read_to_string("day1.txt")?;
|
||||||
|
let lines = file_contents.lines();
|
||||||
|
let mut calories = Vec::with_capacity(lines.clone().count());
|
||||||
|
let mut count: u32 = 0;
|
||||||
|
|
||||||
|
for line in lines {
|
||||||
|
if line.is_empty() {
|
||||||
|
calories.push(count);
|
||||||
|
count = 0;
|
||||||
|
} else {
|
||||||
|
count += line.parse::<u32>().expect("not a number");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let max = calories.iter().max().expect("Should have been a number");
|
||||||
|
println!("Maximum calories is {}", max);
|
||||||
|
|
||||||
|
let mut sorted_calories = calories.clone();
|
||||||
|
sorted_calories.sort_by(|a, b| b.cmp(a));
|
||||||
|
let top_three: u32 = sorted_calories.get(0..3).expect("Not able to be sorted").iter().sum();
|
||||||
|
println!("Top 3 calories {}", top_three);
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod day1;
|
|
@ -0,0 +1,41 @@
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
mod days;
|
||||||
|
|
||||||
|
fn help() {
|
||||||
|
println!("Usage: {} <day-number>", env!("CARGO_BIN_NAME"));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_day(number: i32) -> Result<()> {
|
||||||
|
match number {
|
||||||
|
1 => {
|
||||||
|
days::day1::run()?;
|
||||||
|
}
|
||||||
|
_ => return Err(anyhow!("Invalid day provided")),
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
|
match args.len() {
|
||||||
|
2 => {
|
||||||
|
let number: i32 = match args[1].parse() {
|
||||||
|
Ok(n) => n,
|
||||||
|
Err(_) => {
|
||||||
|
help();
|
||||||
|
return Err(anyhow!("Argument is not an integer"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
run_day(number)?;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
help();
|
||||||
|
return Err(anyhow!("Invalid arguments provided"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue