Tweak the solutions to days 1, 2, and 3

This commit is contained in:
Eryn Wells 2022-12-04 17:53:40 -08:00
parent 846cfe0da6
commit 3b46147c27
3 changed files with 13 additions and 15 deletions

View file

@ -5,31 +5,32 @@ use std::io::Result;
use crate::file::line_reader_for_file;
fn get_calorie_totals<R: Read + BufRead>(lines: Lines<R>) -> Vec<u32> {
fn get_calorie_totals<R: Read + BufRead>(lines: Lines<R>) -> Result<Vec<u32>> {
let mut elves: Vec<u32> = Vec::new();
let mut current_calorie_count: u32 = 0;
for line in lines {
let unwrapped_line = line.unwrap();
if unwrapped_line.is_empty() {
let line = line?;
if line.is_empty() {
elves.push(current_calorie_count);
current_calorie_count = 0;
continue;
}
let u32_value = u32::from_str_radix(&unwrapped_line, 10)
.expect(format!("Couldn't read u32 value from string: {}", unwrapped_line).as_str());
let u32_value = u32::from_str_radix(&line, 10)
.expect(format!("Couldn't read u32 value from string: {}", line).as_str());
current_calorie_count += u32_value;
}
elves.sort_unstable_by(|a, b| b.cmp(a));
elves
Ok(elves)
}
pub fn main(input_filename: &String) -> Result<()> {
let line_reader = line_reader_for_file(input_filename.as_str())
pub fn main(input_filename: &str) -> Result<()> {
let line_reader = line_reader_for_file(input_filename)
.expect(format!("Unable to create line reader for file: {}", input_filename).as_str());
let elves = get_calorie_totals(line_reader);
let elves = get_calorie_totals(line_reader)
.expect("Unable to get knapsack calorie totals");
println!("Part 1: Elf with highest calorie count in knapsack: {}", elves[0]);

View file

@ -96,7 +96,6 @@ impl Outcome {
}
struct Round {
opponent: Shape,
me: Shape,
outcome: Outcome,
}
@ -107,7 +106,6 @@ impl Round {
match (Shape::part1_from_string(split[0]), Shape::part1_from_string(split[1])) {
(Some(opponents_shape), Some(my_shape)) => {
Some(Round {
opponent: opponents_shape,
me: my_shape,
outcome: Outcome::with_shapes(opponents_shape, my_shape)
})
@ -121,7 +119,6 @@ impl Round {
match (Shape::part2_from_string(split[0]), Outcome::part2_from_string(split[1])) {
(Some(opponents_shape), Some(expected_outcome)) => {
Some(Round {
opponent: opponents_shape,
me: Shape::part2_from_shape_and_outcome(opponents_shape, expected_outcome),
outcome: expected_outcome
})

View file

@ -13,17 +13,17 @@ fn main() {
println!("----- Day 1 -----");
let day1_datafile = &args[1];
day1::main(day1_datafile)
day1::main(&day1_datafile)
.expect("Unable to process day1 data file");
println!("----- Day 2 -----");
let day2_datafile = &args[2];
day2::main(day2_datafile.as_str())
day2::main(&day2_datafile)
.expect("Unable to process day2 data file");
println!("----- Day 3 -----");
let day3_datafile = &args[3];
day3::main(day3_datafile.as_str())
day3::main(&day3_datafile)
.expect("Unable to process day3 data file");
println!("----- Day 4 -----");