From 140f846b3f0074a6da180c069b14dc60654fe105 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sun, 8 Dec 2024 20:45:30 +0100 Subject: [PATCH] Automatic read the whole file to avoid measuring I/O time --- src/day01.rs | 5 +---- src/day02.rs | 5 +---- src/day03.rs | 5 +---- src/day04.rs | 5 +---- src/day05.rs | 5 +---- src/day06.rs | 5 +---- src/day07.rs | 5 +---- src/day08.rs | 5 +---- src/day09.rs | 15 +++++++++++++++ src/days.rs | 46 +++++++++++++++++++++------------------------- src/main.rs | 29 +++++++++++++++++++++++------ 11 files changed, 67 insertions(+), 63 deletions(-) create mode 100644 src/day09.rs diff --git a/src/day01.rs b/src/day01.rs index 67f5cce..6c624ca 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -1,9 +1,6 @@ use std::{cmp::Ordering, io::BufRead, iter::Iterator}; -pub fn read_location_ids_as_sorted(reader: R) -> (Vec, Vec) -where - R: BufRead, -{ +pub fn read_location_ids_as_sorted(reader: &mut dyn BufRead) -> (Vec, Vec) { let mut l1 = Vec::::new(); let mut l2 = Vec::::new(); diff --git a/src/day02.rs b/src/day02.rs index 60c558b..d6a5231 100644 --- a/src/day02.rs +++ b/src/day02.rs @@ -6,10 +6,7 @@ use crate::utils; type Reports = Vec>; -pub fn read_reports(reader: R) -> Reports -where - R: BufRead, -{ +pub fn read_reports(reader: &mut dyn BufRead) -> Reports { let mut reports = Vec::>::new(); for l in reader.lines() { reports.push(utils::split_to_vec(&l.unwrap(), " ")); diff --git a/src/day03.rs b/src/day03.rs index 9be94ea..4a2f59d 100644 --- a/src/day03.rs +++ b/src/day03.rs @@ -2,10 +2,7 @@ use std::io::BufRead; use regex::Regex; -pub fn read_program(mut reader: R) -> String -where - R: BufRead, -{ +pub fn read_program(reader: &mut dyn BufRead) -> String { let mut output = String::new(); reader.read_to_string(&mut output).unwrap(); output diff --git a/src/day04.rs b/src/day04.rs index a20b03e..6184148 100644 --- a/src/day04.rs +++ b/src/day04.rs @@ -2,10 +2,7 @@ use std::io::BufRead; type WordSearch = Vec>; -pub fn read_word_search(reader: R) -> WordSearch -where - R: BufRead, -{ +pub fn read_word_search(reader: &mut dyn BufRead) -> WordSearch { let mut word_search: WordSearch = Vec::new(); for l in reader.lines() { word_search.push(l.unwrap().chars().collect()); diff --git a/src/day05.rs b/src/day05.rs index 1d7b51c..c3a267b 100644 --- a/src/day05.rs +++ b/src/day05.rs @@ -30,10 +30,7 @@ impl PageOrdering { type Updates = Vec>; -pub fn read_ordering_and_updates(reader: R) -> (PageOrdering, Updates) -where - R: BufRead, -{ +pub fn read_ordering_and_updates(reader: &mut dyn BufRead) -> (PageOrdering, Updates) { let lines = reader.lines().map(Result::unwrap).collect::>(); let mut lines_iter = lines.iter(); diff --git a/src/day06.rs b/src/day06.rs index a96cf5d..df5d3e7 100644 --- a/src/day06.rs +++ b/src/day06.rs @@ -6,10 +6,7 @@ type Map = DMatrix; type Pos = Point2; type Dir = Vector2; -pub fn read_map(reader: R) -> (Map, Pos) -where - R: BufRead, -{ +pub fn read_map(reader: &mut dyn BufRead) -> (Map, Pos) { let mut map = DMatrix::default(); let mut guard_position = Pos::default(); diff --git a/src/day07.rs b/src/day07.rs index 70ebafa..00c15e2 100644 --- a/src/day07.rs +++ b/src/day07.rs @@ -8,10 +8,7 @@ pub struct Equation { operands: Vec, } -pub fn read(reader: R) -> Vec -where - R: BufRead, -{ +pub fn read(reader: &mut dyn BufRead) -> Vec { reader .lines() .map(|l| { diff --git a/src/day08.rs b/src/day08.rs index 73a945d..8e37a27 100644 --- a/src/day08.rs +++ b/src/day08.rs @@ -8,10 +8,7 @@ type Antinodes = DMatrix; const EMPTY: char = ' '; -pub fn read(reader: R) -> Antennae -where - R: BufRead, -{ +pub fn read(reader: &mut dyn BufRead) -> Antennae { let mut antennae = Antennae::default(); for (i, l) in reader.lines().enumerate() { diff --git a/src/day09.rs b/src/day09.rs new file mode 100644 index 0000000..1112347 --- /dev/null +++ b/src/day09.rs @@ -0,0 +1,15 @@ +use std::io::BufRead; + +pub fn read(reader: &mut dyn BufRead) -> i32 { + 0 +} + +#[cfg(test)] +mod tests { + + #[test] + fn test_part1() {} + + #[test] + fn test_part2() {} +} diff --git a/src/days.rs b/src/days.rs index 518c47a..3c1918a 100644 --- a/src/days.rs +++ b/src/days.rs @@ -1,10 +1,9 @@ -use std::{fs, io::BufReader}; +use std::io::BufRead; use crate::*; -pub fn day01() -> String { - let f = fs::File::open("data/day01.input").unwrap(); - let (ids1, ids2) = day01::read_location_ids_as_sorted(BufReader::new(f)); +pub fn day01(reader: &mut dyn BufRead) -> String { + let (ids1, ids2) = day01::read_location_ids_as_sorted(reader); format!( "part1: {}, part2: {}", day01::sum_distances(&ids1, &ids2), @@ -12,9 +11,8 @@ pub fn day01() -> String { ) } -pub fn day02() -> String { - let f = fs::File::open("data/day02.input").unwrap(); - let reports = day02::read_reports(BufReader::new(f)); +pub fn day02(reader: &mut dyn BufRead) -> String { + let reports = day02::read_reports(reader); format!( "part1: {}, part2: {}", day02::nb_of_safe_reports(&reports), @@ -22,9 +20,8 @@ pub fn day02() -> String { ) } -pub fn day03() -> String { - let f = fs::File::open("data/day03.input").unwrap(); - let program = day03::read_program(BufReader::new(f)); +pub fn day03(reader: &mut dyn BufRead) -> String { + let program = day03::read_program(reader); format!( "part1: {}, part2: {}", day03::execute_corrupted_program(&program), @@ -32,9 +29,8 @@ pub fn day03() -> String { ) } -pub fn day04() -> String { - let f = fs::File::open("data/day04.input").unwrap(); - let word_search = day04::read_word_search(BufReader::new(f)); +pub fn day04(reader: &mut dyn BufRead) -> String { + let word_search = day04::read_word_search(reader); format!( "part1: {}, part2: {}", day04::nb_of_word_occurences(&word_search, "XMAS"), @@ -42,9 +38,8 @@ pub fn day04() -> String { ) } -pub fn day05() -> String { - let f = fs::File::open("data/day05.input").unwrap(); - let (ordering, updates) = day05::read_ordering_and_updates(BufReader::new(f)); +pub fn day05(reader: &mut dyn BufRead) -> String { + let (ordering, updates) = day05::read_ordering_and_updates(reader); format!( "part1: {}, part2: {}", day05::sum_of_mid_page_from_valid_updates(&ordering, &updates), @@ -52,9 +47,8 @@ pub fn day05() -> String { ) } -pub fn day06() -> String { - let f = fs::File::open("data/day06.input").unwrap(); - let (map, guard_pos) = day06::read_map(BufReader::new(f)); +pub fn day06(reader: &mut dyn BufRead) -> String { + let (map, guard_pos) = day06::read_map(reader); format!( "part1: {}, part2: {}", day06::nb_position_visited_by_guard(&map, guard_pos), @@ -62,9 +56,8 @@ pub fn day06() -> String { ) } -pub fn day07() -> String { - let f = fs::File::open("data/day07.input").unwrap(); - let equations = day07::read(BufReader::new(f)); +pub fn day07(reader: &mut dyn BufRead) -> String { + let equations = day07::read(reader); format!( "part1: {}, part2: {}", day07::sum_valid_equations(&equations), @@ -72,12 +65,15 @@ pub fn day07() -> String { ) } -pub fn day08() -> String { - let f = fs::File::open("data/day08.input").unwrap(); - let antennae = day08::read(BufReader::new(f)); +pub fn day08(reader: &mut dyn BufRead) -> String { + let antennae = day08::read(reader); format!( "part1: {}, part2: {}", day08::nb_antinodes(&antennae, day08::AntinodeMode::TwoPerPair), day08::nb_antinodes(&antennae, day08::AntinodeMode::Unlimited), ) } + +pub fn day09(reader: &mut dyn BufRead) -> String { + format!("part1: {}, part2: {}", 0, 0,) +} diff --git a/src/main.rs b/src/main.rs index b6edd9e..bc175e6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,8 @@ -use std::time::Instant; +use std::{ + fs, + io::{BufRead, Read}, + time::Instant, +}; use clap::Parser; use rayon::prelude::*; @@ -11,7 +15,7 @@ mod day05; mod day06; mod day07; mod day08; -// mod day09; +mod day09; // mod day10; // mod day11; // mod day12; @@ -43,7 +47,7 @@ struct Args { fn main() { println!("https://adventofcode.com/2024"); - let days: Vec String> = vec![ + let days: Vec String> = vec![ days::day01, days::day02, days::day03, @@ -52,7 +56,7 @@ fn main() { days::day06, days::day07, days::day08, - // days::day09, + days::day09, // days::day10, // days::day11, // days::day12, @@ -100,12 +104,25 @@ fn main() { } } -fn do_day(days: &[fn() -> String], day: usize) { +fn do_day(days: &[fn(&mut dyn BufRead) -> String], day: usize) { + let filepath = format!("data/day{:02}.input", day); + let mut f = fs::File::open(&filepath).unwrap_or_else(|error| { + println!( + "Cannot find file {}. Did you place the input files in the 'data' directory?\nError:{}", + filepath, error + ); + panic!(); + }); + + // We read the whole file to avoid measuring I/O time. + let mut buffer = Vec::new(); + f.read_to_end(&mut buffer).unwrap(); + let now = Instant::now(); println!( "Result of day {:02}: {} (time: {})", day, - days[day - 1](), + days[day - 1](&mut buffer.as_slice()), format_micros(now.elapsed().as_micros()) ); } -- 2.45.2