Move read_lines to utils

This commit is contained in:
LordMathis 2023-12-18 21:10:48 +01:00
parent a3752c5cee
commit 3d6b90e328
11 changed files with 39 additions and 91 deletions

View File

@ -1,4 +1,5 @@
pub mod year_2023;
pub mod utils;
fn main() {
println!("Hello, world!");

13
src/utils/input_reader.rs Normal file
View File

@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

1
src/utils/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod input_reader;

View File

@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn parse_line(s: &str) -> u32 {
const RADIX: u32 = 10;

View File

@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn parse_line(s: &str) -> u32 {
const RADIX: u32 = 10;

View File

@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn process_line(s: &str) -> u32 {
let v: Vec<&str> = s.split(':').collect();

View File

@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn process_line(s: &str) -> u32 {
let v: Vec<&str> = s.split(':').collect();

View File

@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn create_matrix(lines: io::Lines<io::BufReader<File>>) -> Vec<Vec<Engine>> {
let mut matrix: Vec<Vec<Engine>> = Vec::new();

View File

@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

View File

@ -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<u32>> = Vec::new();
@ -41,10 +39,4 @@ fn process_race_result(time: u32, distance: u32) -> u32 {
return win_count;
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

View File

@ -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<u64> = 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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}