1
- use std:: { collections :: HashMap , io:: stdin} ;
1
+ use std:: io:: stdin;
2
2
3
3
fn eval ( line : & str , plus_precedence : i32 ) -> i64 {
4
4
let mut nums = Vec :: new ( ) ;
5
5
let mut operators = Vec :: new ( ) ;
6
- let precedence = HashMap :: from ( [ ( '(' , 0 ) , ( ')' , 0 ) , ( '+' , plus_precedence) , ( '*' , 1 ) ] ) ;
6
+ let precedence = |c| match c { '+' => plus_precedence, '*' => 1 , _ => 0 } ;
7
7
8
8
for c in line. chars ( ) . chain ( ")" . chars ( ) ) {
9
9
match c {
@@ -12,11 +12,11 @@ fn eval(line: &str, plus_precedence: i32) -> i64 {
12
12
while !operators. is_empty ( ) && c != '(' {
13
13
let op = operators. pop ( ) . unwrap ( ) ;
14
14
if op == '(' && c == ')' { break }
15
- if precedence. get ( & op) . unwrap ( ) < precedence. get ( & c ) . unwrap ( ) {
15
+ if precedence ( op) < precedence ( c ) {
16
16
operators. push ( op) ;
17
17
break ;
18
18
}
19
- let func = if op == '*' { |( a, b) | a * b } else { |( a, b) | a + b } ;
19
+ let func = match op { '*' => |( a, b) | a * b, _ => |( a, b) | a + b } ;
20
20
let value = nums. pop ( ) . zip ( nums. pop ( ) ) . map ( func) . unwrap ( ) ;
21
21
nums. push ( value) ;
22
22
}
@@ -30,6 +30,6 @@ fn eval(line: &str, plus_precedence: i32) -> i64 {
30
30
31
31
fn main ( ) {
32
32
let lines: Vec < String > = stdin ( ) . lines ( ) . filter_map ( Result :: ok) . collect ( ) ;
33
- println ! ( "{:? }" , lines. iter( ) . map( |l| eval( & l, 1 ) ) . sum:: <i64 >( ) ) ;
34
- println ! ( "{:? }" , lines. iter( ) . map( |l| eval( & l, 2 ) ) . sum:: <i64 >( ) ) ;
33
+ println ! ( "{}" , lines. iter( ) . map( |l| eval( & l, 1 ) ) . sum:: <i64 >( ) ) ;
34
+ println ! ( "{}" , lines. iter( ) . map( |l| eval( & l, 2 ) ) . sum:: <i64 >( ) ) ;
35
35
}
0 commit comments