Skip to content

Commit d1a2b32

Browse files
committed
Add day 18
1 parent 1f24938 commit d1a2b32

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

.aoc_tiles/tiles/2020/18.png

8.77 KB
Loading

2020/18/18.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::{collections::HashMap, io::stdin};
1+
use std::io::stdin;
22

33
fn eval(line: &str, plus_precedence: i32) -> i64 {
44
let mut nums = Vec::new();
55
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 };
77

88
for c in line.chars().chain(")".chars()) {
99
match c {
@@ -12,11 +12,11 @@ fn eval(line: &str, plus_precedence: i32) -> i64 {
1212
while !operators.is_empty() && c != '(' {
1313
let op = operators.pop().unwrap();
1414
if op == '(' && c == ')' { break }
15-
if precedence.get(&op).unwrap() < precedence.get(&c).unwrap() {
15+
if precedence(op) < precedence(c) {
1616
operators.push(op);
1717
break;
1818
}
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 };
2020
let value = nums.pop().zip(nums.pop()).map(func).unwrap();
2121
nums.push(value);
2222
}
@@ -30,6 +30,6 @@ fn eval(line: &str, plus_precedence: i32) -> i64 {
3030

3131
fn main() {
3232
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>());
3535
}

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- AOC TILES BEGIN -->
22
<h1 align="center">
3-
Advent of Code - 184/450 ⭐
3+
Advent of Code - 186/450 ⭐
44
</h1>
55
<h1 align="center">
66
2023 - 50 ⭐
@@ -237,7 +237,7 @@
237237
<img src=".aoc_tiles/tiles/2021/25.png" width="161px">
238238
</a>
239239
<h1 align="center">
240-
2020 - 34
240+
2020 - 36
241241
</h1>
242242
<a href="2020/01/01.rs">
243243
<img src=".aoc_tiles/tiles/2020/01.png" width="161px">
@@ -290,6 +290,9 @@
290290
<a href="2020/17/17.rs">
291291
<img src=".aoc_tiles/tiles/2020/17.png" width="161px">
292292
</a>
293+
<a href="2020/18/18.rs">
294+
<img src=".aoc_tiles/tiles/2020/18.png" width="161px">
295+
</a>
293296
<!-- AOC TILES END -->
294297

295298
*The above tiles are clickable, leading to the solution of the corresponding day.*

0 commit comments

Comments
 (0)