Tweak the solutions to days 1, 2, and 3
This commit is contained in:
parent
846cfe0da6
commit
3b46147c27
3 changed files with 13 additions and 15 deletions
|
@ -5,31 +5,32 @@ use std::io::Result;
|
||||||
|
|
||||||
use crate::file::line_reader_for_file;
|
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 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 unwrapped_line = line.unwrap();
|
let line = line?;
|
||||||
if unwrapped_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;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let u32_value = u32::from_str_radix(&unwrapped_line, 10)
|
let u32_value = u32::from_str_radix(&line, 10)
|
||||||
.expect(format!("Couldn't read u32 value from string: {}", unwrapped_line).as_str());
|
.expect(format!("Couldn't read u32 value from string: {}", line).as_str());
|
||||||
current_calorie_count += u32_value;
|
current_calorie_count += u32_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
elves.sort_unstable_by(|a, b| b.cmp(a));
|
elves.sort_unstable_by(|a, b| b.cmp(a));
|
||||||
elves
|
Ok(elves)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(input_filename: &String) -> Result<()> {
|
pub fn main(input_filename: &str) -> Result<()> {
|
||||||
let line_reader = line_reader_for_file(input_filename.as_str())
|
let line_reader = line_reader_for_file(input_filename)
|
||||||
.expect(format!("Unable to create line reader for file: {}", input_filename).as_str());
|
.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]);
|
println!("Part 1: Elf with highest calorie count in knapsack: {}", elves[0]);
|
||||||
|
|
||||||
|
|
|
@ -96,7 +96,6 @@ impl Outcome {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Round {
|
struct Round {
|
||||||
opponent: Shape,
|
|
||||||
me: Shape,
|
me: Shape,
|
||||||
outcome: Outcome,
|
outcome: Outcome,
|
||||||
}
|
}
|
||||||
|
@ -107,7 +106,6 @@ impl Round {
|
||||||
match (Shape::part1_from_string(split[0]), Shape::part1_from_string(split[1])) {
|
match (Shape::part1_from_string(split[0]), Shape::part1_from_string(split[1])) {
|
||||||
(Some(opponents_shape), Some(my_shape)) => {
|
(Some(opponents_shape), Some(my_shape)) => {
|
||||||
Some(Round {
|
Some(Round {
|
||||||
opponent: opponents_shape,
|
|
||||||
me: my_shape,
|
me: my_shape,
|
||||||
outcome: Outcome::with_shapes(opponents_shape, 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])) {
|
match (Shape::part2_from_string(split[0]), Outcome::part2_from_string(split[1])) {
|
||||||
(Some(opponents_shape), Some(expected_outcome)) => {
|
(Some(opponents_shape), Some(expected_outcome)) => {
|
||||||
Some(Round {
|
Some(Round {
|
||||||
opponent: opponents_shape,
|
|
||||||
me: Shape::part2_from_shape_and_outcome(opponents_shape, expected_outcome),
|
me: Shape::part2_from_shape_and_outcome(opponents_shape, expected_outcome),
|
||||||
outcome: expected_outcome
|
outcome: expected_outcome
|
||||||
})
|
})
|
||||||
|
|
|
@ -13,17 +13,17 @@ fn main() {
|
||||||
|
|
||||||
println!("----- Day 1 -----");
|
println!("----- Day 1 -----");
|
||||||
let day1_datafile = &args[1];
|
let day1_datafile = &args[1];
|
||||||
day1::main(day1_datafile)
|
day1::main(&day1_datafile)
|
||||||
.expect("Unable to process day1 data file");
|
.expect("Unable to process day1 data file");
|
||||||
|
|
||||||
println!("----- Day 2 -----");
|
println!("----- Day 2 -----");
|
||||||
let day2_datafile = &args[2];
|
let day2_datafile = &args[2];
|
||||||
day2::main(day2_datafile.as_str())
|
day2::main(&day2_datafile)
|
||||||
.expect("Unable to process day2 data file");
|
.expect("Unable to process day2 data file");
|
||||||
|
|
||||||
println!("----- Day 3 -----");
|
println!("----- Day 3 -----");
|
||||||
let day3_datafile = &args[3];
|
let day3_datafile = &args[3];
|
||||||
day3::main(day3_datafile.as_str())
|
day3::main(&day3_datafile)
|
||||||
.expect("Unable to process day3 data file");
|
.expect("Unable to process day3 data file");
|
||||||
|
|
||||||
println!("----- Day 4 -----");
|
println!("----- Day 4 -----");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue