Move Day 1 to its own crate
This commit is contained in:
parent
9dfd195bb6
commit
2a932235cc
4 changed files with 28 additions and 15 deletions
7
2022/day1/Cargo.lock
generated
Normal file
7
2022/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
2022/day1/Cargo.toml
Normal file
8
2022/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]
|
|
@ -1,16 +1,12 @@
|
||||||
use std::io::BufRead;
|
use std::env;
|
||||||
use std::io::Lines;
|
use std::fs;
|
||||||
use std::io::Read;
|
use std::str::Lines;
|
||||||
use std::io::Result;
|
|
||||||
|
|
||||||
use crate::file::line_reader_for_file;
|
fn get_calorie_totals(lines: Lines) -> Vec<u32> {
|
||||||
|
|
||||||
fn get_calorie_totals<R: Read + BufRead>(lines: Lines<R>) -> Result<Vec<u32>> {
|
|
||||||
let mut elves: Vec<u32> = Vec::new();
|
let mut elves: Vec<u32> = Vec::new();
|
||||||
let mut current_calorie_count: u32 = 0;
|
let mut current_calorie_count: u32 = 0;
|
||||||
|
|
||||||
for line in lines {
|
for line in lines {
|
||||||
let line = line?;
|
|
||||||
if line.is_empty() {
|
if line.is_empty() {
|
||||||
elves.push(current_calorie_count);
|
elves.push(current_calorie_count);
|
||||||
current_calorie_count = 0;
|
current_calorie_count = 0;
|
||||||
|
@ -23,13 +19,17 @@ fn get_calorie_totals<R: Read + BufRead>(lines: Lines<R>) -> Result<Vec<u32>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
elves.sort_unstable_by(|a, b| b.cmp(a));
|
elves.sort_unstable_by(|a, b| b.cmp(a));
|
||||||
Ok(elves)
|
elves
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(input_filename: &str) -> Result<()> {
|
fn main() {
|
||||||
let line_reader = line_reader_for_file(input_filename)
|
let args: Vec<String> = env::args().collect();
|
||||||
.expect(format!("Unable to create line reader for file: {}", input_filename).as_str());
|
|
||||||
let elves = get_calorie_totals(line_reader).expect("Unable to get knapsack calorie totals");
|
let filename = args.get(1).expect("Missing filename argument");
|
||||||
|
|
||||||
|
let file_contents = fs::read_to_string(&filename).expect("Unable to read file");
|
||||||
|
let lines = file_contents.lines();
|
||||||
|
let elves = get_calorie_totals(lines);
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"Part 1: Elf with highest calorie count in knapsack: {}",
|
"Part 1: Elf with highest calorie count in knapsack: {}",
|
||||||
|
@ -41,6 +41,4 @@ pub fn main(input_filename: &str) -> Result<()> {
|
||||||
"Part 2: Elves with top 3 highest calorie counts in their knapsacks: {}, {}, {} = {}",
|
"Part 2: Elves with top 3 highest calorie counts in their knapsacks: {}, {}, {} = {}",
|
||||||
&elves[0], &elves[1], &elves[2], sum_of_top_three
|
&elves[0], &elves[1], &elves[2], sum_of_top_three
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue