Skip to content

Commit a31dc2e

Browse files
committed
moved flatten to read_lines
1 parent cbc9dc6 commit a31dc2e

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

src/helpers.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
use {
44
std::fs::File,
5-
std::io::{self, BufRead},
5+
std::io::{BufRead, BufReader, Lines},
6+
std::iter::Flatten,
67
std::path::Path,
78
};
89

@@ -54,10 +55,12 @@ pub fn solve_day(day: u8) {
5455
}
5556
}
5657

57-
/// returns an iterator over each line in the input file
58-
pub fn read_lines<P>(filename: P) -> std::io::Lines<std::io::BufReader<std::fs::File>>
58+
/// returns an iterator over each line in the input file, ignoring lines that failed to be read
59+
pub fn read_lines<P>(filename: P) -> Flatten<Lines<BufReader<File>>>
5960
where
6061
P: AsRef<Path>,
6162
{
62-
io::BufReader::new(File::open(filename).expect("not able to open file")).lines()
63+
BufReader::new(File::open(filename).expect("not able to open file"))
64+
.lines()
65+
.flatten()
6366
}

src/solutions/day01.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
77
use {crate::helpers::read_lines, std::time::SystemTime};
88

9+
/// solves the part 1 of day 01 and return its result and elapsed time
910
pub fn pt1(filename: &str) -> (u32, u32) {
1011
let time = SystemTime::now();
1112

1213
let mut biggest = 0;
1314
let mut current = 0;
1415

15-
read_lines(filename).flatten().for_each(|line| {
16+
read_lines(filename).for_each(|line| {
1617
match line.as_str() {
1718
// if empty line, compare current block with biggest found until then
1819
"" => {
@@ -32,6 +33,7 @@ pub fn pt1(filename: &str) -> (u32, u32) {
3233
(answer, time)
3334
}
3435

36+
/// solves the part 2 of day 01 and return its result and elapsed time
3537
pub fn pt2(filename: &str) -> (u32, u32) {
3638
let time = SystemTime::now();
3739

@@ -40,7 +42,7 @@ pub fn pt2(filename: &str) -> (u32, u32) {
4042
let mut current = 0;
4143

4244
// iterating through each line of the file
43-
read_lines(filename).flatten().for_each(|line| {
45+
read_lines(filename).for_each(|line| {
4446
match line.as_str() {
4547
// if empty line, compare current block with 3 biggest known
4648
"" => {

src/solutions/day02.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
77
use {crate::helpers::read_lines, std::time::SystemTime};
88

9+
/// solves the part 1 of day 02 and return its result and elapsed time
910
pub fn pt1(filename: &str) -> (u32, u32) {
1011
let time = SystemTime::now();
1112

1213
let answer = read_lines(filename)
13-
.flatten()
1414
.map(|line| RoundMoves::from_line(line).total_points())
1515
.sum();
1616

@@ -19,11 +19,11 @@ pub fn pt1(filename: &str) -> (u32, u32) {
1919
(answer, time)
2020
}
2121

22+
/// solves the part 2 of day 02 and return its result and elapsed time
2223
pub fn pt2(filename: &str) -> (u32, u32) {
2324
let time = SystemTime::now();
2425

2526
let answer = read_lines(filename)
26-
.flatten()
2727
.map(|line| RoundOutcome::from_line(line).total_points())
2828
.sum();
2929

src/solutions/day03.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
77
use {crate::helpers::read_lines, std::time::SystemTime};
88

9+
/// solves the part 1 of day 03 and return its result and elapsed time
910
pub fn pt1(filename: &str) -> (u32, u32) {
1011
let time = SystemTime::now();
1112

1213
let answer = read_lines(filename)
13-
.flatten()
1414
.map(|line| Rucksack::from_line(line).value_of_common())
1515
.sum();
1616

@@ -19,6 +19,7 @@ pub fn pt1(filename: &str) -> (u32, u32) {
1919
(answer, time)
2020
}
2121

22+
/// solves the part 2 of day 03 and return its result and elapsed time
2223
pub fn pt2(filename: &str) -> (u32, u32) {
2324
let time = SystemTime::now();
2425

@@ -32,7 +33,7 @@ pub fn pt2(filename: &str) -> (u32, u32) {
3233

3334
let mut answer = 0;
3435

35-
for line in read_lines(filename).flatten() {
36+
for line in read_lines(filename) {
3637
match current_index {
3738
1 => {
3839
current_group.first = line;

0 commit comments

Comments
 (0)