Refactor file handling to file.rs
This commit is contained in:
parent
46b058d619
commit
4cd7570ba9
3 changed files with 30 additions and 8 deletions
|
@ -1,10 +1,10 @@
|
|||
use std::fs::File;
|
||||
use std::io::BufRead;
|
||||
use std::io::BufReader;
|
||||
use std::io::Lines;
|
||||
use std::io::Read;
|
||||
use std::io::Result;
|
||||
|
||||
use crate::file::line_reader_for_file;
|
||||
|
||||
fn get_calorie_totals<R: Read + BufRead>(lines: Lines<R>) -> Vec<u32> {
|
||||
let mut elves: Vec<u32> = Vec::new();
|
||||
let mut current_calorie_count: u32 = 0;
|
||||
|
@ -13,7 +13,6 @@ fn get_calorie_totals<R: Read + BufRead>(lines: Lines<R>) -> Vec<u32> {
|
|||
let unwrapped_line = line.unwrap();
|
||||
if unwrapped_line.is_empty() {
|
||||
elves.push(current_calorie_count);
|
||||
println!("Elf {}: {} calories", elves.len(), current_calorie_count);
|
||||
current_calorie_count = 0;
|
||||
continue;
|
||||
}
|
||||
|
@ -28,12 +27,15 @@ fn get_calorie_totals<R: Read + BufRead>(lines: Lines<R>) -> Vec<u32> {
|
|||
}
|
||||
|
||||
pub fn main(input_filename: &String) -> Result<()> {
|
||||
let file = File::open(input_filename)?;
|
||||
let reader = BufReader::new(file);
|
||||
let line_reader = 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(reader.lines());
|
||||
println!("Part 1: Elf with highest calorie count in knapsack: {}", elves[0]);
|
||||
|
||||
println!("Elf with highest calorie count in knapsack: {}", elves[0]);
|
||||
let sum_of_top_three = &elves[0] + &elves[1] + &elves[2];
|
||||
println!("Part 2: Elves with top 3 highest calorie counts in their knapsacks: {}, {}, {} = {}",
|
||||
&elves[0], &elves[1], &elves[2], sum_of_top_three);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
11
2022/aoc2022/src/file.rs
Normal file
11
2022/aoc2022/src/file.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use std::fs::File;
|
||||
use std::io::BufRead;
|
||||
use std::io::BufReader;
|
||||
use std::io::Lines;
|
||||
use std::io::Result;
|
||||
|
||||
pub fn line_reader_for_file(filename: &str) -> Result<Lines<BufReader<File>>> {
|
||||
let file = File::open(filename)?;
|
||||
let reader = BufReader::new(file);
|
||||
Ok(reader.lines())
|
||||
}
|
|
@ -1,12 +1,21 @@
|
|||
use std::env;
|
||||
|
||||
mod day1;
|
||||
mod day2;
|
||||
mod file;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
dbg!("Command line args: {}", &args);
|
||||
|
||||
assert!(args.len() >= 3, "Missing command line arguments");
|
||||
|
||||
println!("----- Day 1 -----");
|
||||
let day1_datafile = &args[1];
|
||||
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())
|
||||
.expect("Unable to process day2 data file");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue