File tree Expand file tree Collapse file tree 4 files changed +16
-10
lines changed Expand file tree Collapse file tree 4 files changed +16
-10
lines changed Original file line number Diff line number Diff line change 22
33use {
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 > > >
5960where
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}
Original file line number Diff line number Diff line change 66
77use { crate :: helpers:: read_lines, std:: time:: SystemTime } ;
88
9+ /// solves the part 1 of day 01 and return its result and elapsed time
910pub 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
3537pub 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 "" => {
Original file line number Diff line number Diff line change 66
77use { crate :: helpers:: read_lines, std:: time:: SystemTime } ;
88
9+ /// solves the part 1 of day 02 and return its result and elapsed time
910pub 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
2223pub 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
Original file line number Diff line number Diff line change 66
77use { crate :: helpers:: read_lines, std:: time:: SystemTime } ;
88
9+ /// solves the part 1 of day 03 and return its result and elapsed time
910pub 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
2223pub 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;
You can’t perform that action at this time.
0 commit comments