From 3d6b90e328e76c30d6e7ba7e4c50f35aee3785a2 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Mon, 18 Dec 2023 21:10:48 +0100 Subject: [PATCH] Move read_lines to utils --- src/main.rs | 1 + src/utils/input_reader.rs | 13 +++++++++++++ src/utils/mod.rs | 1 + src/year_2023/day_01_trebuchet/part_one.rs | 15 ++------------- src/year_2023/day_01_trebuchet/part_two.rs | 14 +++----------- src/year_2023/day_02_cube_conundrum/part_one.rs | 14 ++------------ src/year_2023/day_02_cube_conundrum/part_two.rs | 15 ++++----------- src/year_2023/day_03_gear_ratios/part_one.rs | 15 ++++----------- src/year_2023/day_04_scratchcards/part_one.rs | 14 +++----------- src/year_2023/day_06_wait_for_it/part_one.rs | 14 +++----------- src/year_2023/day_06_wait_for_it/part_two.rs | 14 +++----------- 11 files changed, 39 insertions(+), 91 deletions(-) create mode 100644 src/utils/input_reader.rs create mode 100644 src/utils/mod.rs diff --git a/src/main.rs b/src/main.rs index 03ed285..5d26c58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ pub mod year_2023; +pub mod utils; fn main() { println!("Hello, world!"); diff --git a/src/utils/input_reader.rs b/src/utils/input_reader.rs new file mode 100644 index 0000000..e7b2fb4 --- /dev/null +++ b/src/utils/input_reader.rs @@ -0,0 +1,13 @@ +// https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/read_lines.html +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; + + +pub fn read_lines

(filename: P) -> io::Result>> +where + P: AsRef, +{ + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..44b92ab --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod input_reader; \ No newline at end of file diff --git a/src/year_2023/day_01_trebuchet/part_one.rs b/src/year_2023/day_01_trebuchet/part_one.rs index 98fa680..59e923a 100644 --- a/src/year_2023/day_01_trebuchet/part_one.rs +++ b/src/year_2023/day_01_trebuchet/part_one.rs @@ -1,10 +1,7 @@ -// https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/read_lines.html -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use crate::utils::input_reader; pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut result = 0; for line in lines { @@ -15,14 +12,6 @@ pub fn part_one() { } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} - fn parse_line(s: &str) -> u32 { const RADIX: u32 = 10; diff --git a/src/year_2023/day_01_trebuchet/part_two.rs b/src/year_2023/day_01_trebuchet/part_two.rs index 37c7ce0..0f3ae59 100644 --- a/src/year_2023/day_01_trebuchet/part_two.rs +++ b/src/year_2023/day_01_trebuchet/part_two.rs @@ -1,10 +1,9 @@ use std::collections::HashMap; -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; + +use crate::utils::input_reader; pub fn part_two() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut result = 0; for line in lines { @@ -15,13 +14,6 @@ pub fn part_two() { } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} fn parse_line(s: &str) -> u32 { const RADIX: u32 = 10; diff --git a/src/year_2023/day_02_cube_conundrum/part_one.rs b/src/year_2023/day_02_cube_conundrum/part_one.rs index f14bcbf..45c725a 100644 --- a/src/year_2023/day_02_cube_conundrum/part_one.rs +++ b/src/year_2023/day_02_cube_conundrum/part_one.rs @@ -1,9 +1,7 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use crate::utils::input_reader; pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut result = 0; for line in lines { @@ -14,14 +12,6 @@ pub fn part_one() { } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} - fn process_line(s: &str) -> u32 { let v: Vec<&str> = s.split(':').collect(); diff --git a/src/year_2023/day_02_cube_conundrum/part_two.rs b/src/year_2023/day_02_cube_conundrum/part_two.rs index 671eb80..7b42d58 100644 --- a/src/year_2023/day_02_cube_conundrum/part_two.rs +++ b/src/year_2023/day_02_cube_conundrum/part_two.rs @@ -1,7 +1,6 @@ use std::cmp::max; -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; + +use crate::utils::input_reader; struct Game { red: u32, @@ -10,7 +9,7 @@ struct Game { } pub fn part_two() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut result = 0; for line in lines { @@ -21,13 +20,7 @@ pub fn part_two() { } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} + fn process_line(s: &str) -> u32 { let v: Vec<&str> = s.split(':').collect(); diff --git a/src/year_2023/day_03_gear_ratios/part_one.rs b/src/year_2023/day_03_gear_ratios/part_one.rs index e7c5ad1..8d6e24c 100644 --- a/src/year_2023/day_03_gear_ratios/part_one.rs +++ b/src/year_2023/day_03_gear_ratios/part_one.rs @@ -1,6 +1,6 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use std::{fs::File, io}; + +use crate::utils::input_reader; enum Engine { Symbol(char), @@ -9,7 +9,7 @@ enum Engine { } pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let matrix = create_matrix(lines); let result = process_matrix(&matrix); @@ -17,13 +17,6 @@ pub fn part_one() { } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} fn create_matrix(lines: io::Lines>) -> Vec> { let mut matrix: Vec> = Vec::new(); diff --git a/src/year_2023/day_04_scratchcards/part_one.rs b/src/year_2023/day_04_scratchcards/part_one.rs index 6c5bcdc..35ee2cf 100644 --- a/src/year_2023/day_04_scratchcards/part_one.rs +++ b/src/year_2023/day_04_scratchcards/part_one.rs @@ -1,10 +1,9 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; use std::collections::HashSet; +use crate::utils::input_reader; + pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut result = 0; for line in lines { @@ -46,10 +45,3 @@ fn process_line(s: &str) -> u32 { } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} \ No newline at end of file diff --git a/src/year_2023/day_06_wait_for_it/part_one.rs b/src/year_2023/day_06_wait_for_it/part_one.rs index 93d1e1e..12c567a 100644 --- a/src/year_2023/day_06_wait_for_it/part_one.rs +++ b/src/year_2023/day_06_wait_for_it/part_one.rs @@ -1,9 +1,7 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use crate::utils::input_reader; pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut margin = 1; let mut race_results: Vec> = Vec::new(); @@ -41,10 +39,4 @@ fn process_race_result(time: u32, distance: u32) -> u32 { return win_count; } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} + diff --git a/src/year_2023/day_06_wait_for_it/part_two.rs b/src/year_2023/day_06_wait_for_it/part_two.rs index dfa2ab6..7f19ccd 100644 --- a/src/year_2023/day_06_wait_for_it/part_two.rs +++ b/src/year_2023/day_06_wait_for_it/part_two.rs @@ -1,9 +1,7 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use crate::utils::input_reader; pub fn part_two() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut race_result: Vec = Vec::new(); for line in lines { @@ -38,10 +36,4 @@ fn process_race_result(time: u64, distance: u64) -> u64 { return win_count; } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} +