File tree Expand file tree Collapse file tree 1 file changed +39
-1
lines changed
Expand file tree Collapse file tree 1 file changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -94,12 +94,50 @@ pub fn part_1(data: crate::DataIn) -> crate::AoCResult<String> {
9494 Ok ( ret. to_string ( ) )
9595}
9696
97+ pub fn part_2 ( data : crate :: DataIn ) -> crate :: AoCResult < String > {
98+ let mut lines = data. collect_vec ( ) ;
99+
100+ let operations_str = lines. pop ( ) . expect ( "There's going to be at least one line" ) ;
101+ let operations = operations_str. split_whitespace ( ) ;
102+
103+ let mut lines = lines
104+ . into_iter ( )
105+ . map ( |line| line. chars ( ) . collect_vec ( ) . into_iter ( ) )
106+ . collect_vec ( ) ;
107+
108+ let ret = operations
109+ . map ( |oper| -> AoCResult < Num > {
110+ let numbers: Vec < Num > = ( 0 ..)
111+ . map ( |_| {
112+ let num: String = lines. iter_mut ( ) . map ( |i| i. next ( ) . unwrap_or ( ' ' ) ) . collect ( ) ;
113+ num. trim ( ) . to_owned ( )
114+ } )
115+ . take_while ( |num| !num. is_empty ( ) )
116+ . map ( |num| num. parse ( ) )
117+ . try_collect ( ) ?;
118+ if numbers. is_empty ( ) {
119+ panic ! ( "ran out of numbers before operations!" ) ;
120+ }
121+ match oper {
122+ "*" => Ok ( numbers. into_iter ( ) . product ( ) ) ,
123+ "+" => Ok ( numbers. into_iter ( ) . sum ( ) ) ,
124+ _ => Err ( AoCError :: new ( format ! ( "Unexpected operator {oper}!" ) ) ) ,
125+ }
126+ } )
127+ . try_fold ( 0 , |acc, num| -> AoCResult < Num > { Ok ( acc + num?) } ) ?;
128+
129+ Ok ( ret. to_string ( ) )
130+ }
131+
97132inventory:: submit!( crate :: AoCDay {
98133 year: "2025" ,
99134 day: "6" ,
100135 part_1: crate :: AoCPart {
101136 main: part_1,
102137 example: part_1
103138 } ,
104- part_2: None
139+ part_2: Some ( crate :: AoCPart {
140+ main: part_2,
141+ example: part_2
142+ } )
105143} ) ;
You can’t perform that action at this time.
0 commit comments