@@ -12,18 +12,15 @@ pub fn pt1(filename: &str) -> (u32, u32) {
1212 let mut biggest = 0 ;
1313 let mut current = 0 ;
1414
15- // iterating through each line of the file
16- read_lines ( filename) . for_each ( |line| {
17- if let Ok ( line) = line {
18- match line. as_str ( ) {
19- // if empty line, compare current block with biggest found until then
20- "" => {
21- biggest = biggest. max ( current) ;
22- current = 0 ;
23- }
24- // if number, continue the sum of current block
25- _ => current += line. parse :: < u32 > ( ) . expect ( "unable to parse line {line}" ) ,
15+ read_lines ( filename) . flatten ( ) . for_each ( |line| {
16+ match line. as_str ( ) {
17+ // if empty line, compare current block with biggest found until then
18+ "" => {
19+ biggest = biggest. max ( current) ;
20+ current = 0 ;
2621 }
22+ // if number, continue the sum of current block
23+ _ => current += line. parse :: < u32 > ( ) . expect ( "unable to parse line {line}" ) ,
2724 }
2825 } ) ;
2926
@@ -43,26 +40,24 @@ pub fn pt2(filename: &str) -> (u32, u32) {
4340 let mut current = 0 ;
4441
4542 // iterating through each line of the file
46- read_lines ( filename) . for_each ( |line| {
47- if let Ok ( line) = line {
48- match line. as_str ( ) {
49- // if empty line, compare current block with 3 biggest known
50- "" => {
51- if current > biggest[ 0 ] {
52- biggest[ 2 ] = biggest[ 1 ] ;
53- biggest[ 1 ] = biggest[ 0 ] ;
54- biggest[ 0 ] = current;
55- } else if current > biggest[ 1 ] {
56- biggest[ 2 ] = biggest[ 1 ] ;
57- biggest[ 1 ] = current;
58- } else if current > biggest[ 2 ] {
59- biggest[ 2 ] = current;
60- }
61- current = 0 ;
43+ read_lines ( filename) . flatten ( ) . for_each ( |line| {
44+ match line. as_str ( ) {
45+ // if empty line, compare current block with 3 biggest known
46+ "" => {
47+ if current > biggest[ 0 ] {
48+ biggest[ 2 ] = biggest[ 1 ] ;
49+ biggest[ 1 ] = biggest[ 0 ] ;
50+ biggest[ 0 ] = current;
51+ } else if current > biggest[ 1 ] {
52+ biggest[ 2 ] = biggest[ 1 ] ;
53+ biggest[ 1 ] = current;
54+ } else if current > biggest[ 2 ] {
55+ biggest[ 2 ] = current;
6256 }
63- // if number, continue the sum of current block
64- _ => current += line. parse :: < u32 > ( ) . expect ( "unable to parse line {line}" ) ,
57+ current = 0 ;
6558 }
59+ // if number, continue the sum of current block
60+ _ => current += line. parse :: < u32 > ( ) . expect ( "unable to parse line {line}" ) ,
6661 }
6762 } ) ;
6863
0 commit comments