@@ -13,7 +13,7 @@ pub fn pt1(filename: &str) -> (u32, u32) {
1313 let answer = read_lines ( filename)
1414 // getting a `RoundMoves` out of each line and calculating its `total_points`
1515 . map ( |line| match line {
16- Ok ( line) => round_parse_1 ( line) . total_points ( ) ,
16+ Ok ( line) => RoundMoves :: from_line ( line) . total_points ( ) ,
1717 _ => panic ! ( "unable to read line" ) ,
1818 } )
1919 // getting the sum of all `total_points`
@@ -24,40 +24,14 @@ pub fn pt1(filename: &str) -> (u32, u32) {
2424 ( answer, time)
2525}
2626
27- /// according to part 1 instructions, parses a `line` in the file to its equivalent `RoundMoves`
28- fn round_parse_1 ( line : String ) -> RoundMoves {
29- // each line should be two letters separated by a space
30- let moves: Vec < & str > = line. split ( ' ' ) . collect ( ) ;
31-
32- if moves. len ( ) != 2 {
33- panic ! ( "line contains more than one move" ) ;
34- }
35-
36- let opponent = match moves[ 0 ] {
37- "A" => Move :: Rock ,
38- "B" => Move :: Paper ,
39- "C" => Move :: Scissors ,
40- _ => panic ! ( "move {} not allowed" , moves[ 0 ] ) ,
41- } ;
42-
43- let player = match moves[ 1 ] {
44- "X" => Move :: Rock ,
45- "Y" => Move :: Paper ,
46- "Z" => Move :: Scissors ,
47- _ => panic ! ( "move {} not allowed" , moves[ 1 ] ) ,
48- } ;
49-
50- RoundMoves :: new ( opponent, player)
51- }
52-
5327pub fn pt2 ( filename : & str ) -> ( u32 , u32 ) {
5428 let time = SystemTime :: now ( ) ;
5529
5630 // iterating through each line
5731 let answer = read_lines ( filename)
5832 // getting a `RoundMoves` out of each line and calculating its `total_points`
5933 . map ( |line| match line {
60- Ok ( line) => round_parse_2 ( line) . total_points ( ) ,
34+ Ok ( line) => RoundOutcome :: from_line ( line) . total_points ( ) ,
6135 _ => panic ! ( "unable to read line" ) ,
6236 } )
6337 // getting the sum of all `total_points`
@@ -68,32 +42,6 @@ pub fn pt2(filename: &str) -> (u32, u32) {
6842 ( answer, time)
6943}
7044
71- /// according to part 1 instructions, parses a `line` in the file to its equivalent `RoundMoves`
72- fn round_parse_2 ( line : String ) -> RoundOutcome {
73- // each line should be two letters separated by a space
74- let moves: Vec < & str > = line. split ( ' ' ) . collect ( ) ;
75-
76- if moves. len ( ) != 2 {
77- panic ! ( "line contains more than one move" ) ;
78- }
79-
80- let opponent = match moves[ 0 ] {
81- "A" => Move :: Rock ,
82- "B" => Move :: Paper ,
83- "C" => Move :: Scissors ,
84- _ => panic ! ( "move {} not allowed" , moves[ 0 ] ) ,
85- } ;
86-
87- let outcome = match moves[ 1 ] {
88- "X" => Outcome :: Lost ,
89- "Y" => Outcome :: Draw ,
90- "Z" => Outcome :: Win ,
91- _ => panic ! ( "move {} not allowed" , moves[ 1 ] ) ,
92- } ;
93-
94- RoundOutcome :: new ( opponent, outcome)
95- }
96-
9745#[ derive( Debug ) ]
9846/// represents a round of the game, with a move from the opponent and a move from the player
9947struct RoundMoves {
@@ -137,6 +85,32 @@ impl RoundMoves {
13785 }
13886 }
13987
88+ /// according to part 1 instructions, parses a `line` in the file to its equivalent `RoundMoves`
89+ fn from_line ( line : String ) -> Self {
90+ // each line should be two letters separated by a space
91+ let moves: Vec < & str > = line. split ( ' ' ) . collect ( ) ;
92+
93+ if moves. len ( ) != 2 {
94+ panic ! ( "line contains more than one move" ) ;
95+ }
96+
97+ let opponent = match moves[ 0 ] {
98+ "A" => Move :: Rock ,
99+ "B" => Move :: Paper ,
100+ "C" => Move :: Scissors ,
101+ _ => panic ! ( "move {} not allowed" , moves[ 0 ] ) ,
102+ } ;
103+
104+ let player = match moves[ 1 ] {
105+ "X" => Move :: Rock ,
106+ "Y" => Move :: Paper ,
107+ "Z" => Move :: Scissors ,
108+ _ => panic ! ( "move {} not allowed" , moves[ 1 ] ) ,
109+ } ;
110+
111+ RoundMoves :: new ( opponent, player)
112+ }
113+
140114 /// returns a new instance of `RoundMoves`
141115 fn new ( opponent : Move , player : Move ) -> Self {
142116 RoundMoves { opponent, player }
@@ -186,6 +160,32 @@ impl RoundOutcome {
186160 }
187161 }
188162
163+ /// according to part 1 instructions, parses a `line` in the file to its equivalent `RoundMoves`
164+ fn from_line ( line : String ) -> Self {
165+ // each line should be two letters separated by a space
166+ let moves: Vec < & str > = line. split ( ' ' ) . collect ( ) ;
167+
168+ if moves. len ( ) != 2 {
169+ panic ! ( "line contains more than one move" ) ;
170+ }
171+
172+ let opponent = match moves[ 0 ] {
173+ "A" => Move :: Rock ,
174+ "B" => Move :: Paper ,
175+ "C" => Move :: Scissors ,
176+ _ => panic ! ( "move {} not allowed" , moves[ 0 ] ) ,
177+ } ;
178+
179+ let outcome = match moves[ 1 ] {
180+ "X" => Outcome :: Lost ,
181+ "Y" => Outcome :: Draw ,
182+ "Z" => Outcome :: Win ,
183+ _ => panic ! ( "move {} not allowed" , moves[ 1 ] ) ,
184+ } ;
185+
186+ RoundOutcome :: new ( opponent, outcome)
187+ }
188+
189189 /// returns a new instance of `RoundOutcome`
190190 fn new ( opponent : Move , outcome : Outcome ) -> Self {
191191 RoundOutcome { opponent, outcome }
@@ -215,13 +215,13 @@ mod tests {
215215 const FILENAME : & str = "./data/examples/example02.txt" ;
216216
217217 #[ test]
218- fn test_day02_pt01 ( ) {
218+ fn pt01 ( ) {
219219 let ( answer, _) = pt1 ( FILENAME ) ;
220220 assert_eq ! ( 15 , answer) ;
221221 }
222222
223223 #[ test]
224- fn test_day02_pt02 ( ) {
224+ fn pt02 ( ) {
225225 let ( answer, _) = pt2 ( FILENAME ) ;
226226 assert_eq ! ( 12 , answer) ;
227227 }
0 commit comments