-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_02.rs
101 lines (86 loc) · 2.37 KB
/
day_02.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const ROCK: u32 = 1;
const PAPER: u32 = 2;
const SCISSORS: u32 = 3;
const WIN: u32 = 6;
const DRAW: u32 = 3;
const LOSE: u32 = 0;
pub fn part_1(input: &str) -> u32 {
let scores = input.lines().map(|line| {
let pair: Vec<u32> = line
.chars()
.filter_map(|c| match c {
'A' | 'X' => Some(ROCK),
'B' | 'Y' => Some(PAPER),
'C' | 'Z' => Some(SCISSORS),
_ => None,
})
.collect();
let score = match (pair[0], pair[1]) {
(ROCK, PAPER) => WIN,
(ROCK, SCISSORS) => LOSE,
(PAPER, ROCK) => LOSE,
(PAPER, SCISSORS) => WIN,
(SCISSORS, PAPER) => LOSE,
(SCISSORS, ROCK) => WIN,
_ => DRAW,
};
score + pair[1]
});
scores.sum()
}
pub fn part_2(input: &str) -> u32 {
let scores = input.lines().map(|line| {
let pair: Vec<u32> = line
.chars()
.filter_map(|c| match c {
'A' => Some(ROCK),
'B' => Some(PAPER),
'C' => Some(SCISSORS),
'X' => Some(LOSE),
'Y' => Some(DRAW),
'Z' => Some(WIN),
_ => None,
})
.collect();
let score = match (pair[0], pair[1]) {
(ROCK, WIN) => PAPER,
(ROCK, LOSE) => SCISSORS,
(PAPER, WIN) => SCISSORS,
(PAPER, LOSE) => ROCK,
(SCISSORS, WIN) => ROCK,
(SCISSORS, LOSE) => PAPER,
_ => pair[0],
};
score + pair[1]
});
scores.sum()
}
#[cfg(test)]
mod tests {
use crate::get_input;
use super::*;
#[test]
fn test_part_1_dummy() {
let input = get_input("2022", "02", Some("dummy"));
let output = part_1(&input);
assert_eq!(output, 15);
}
#[test]
fn test_part_1() {
let input = get_input("2022", "02", None);
let output = part_1(&input);
assert_eq!(output, 9177);
}
#[test]
fn test_part_2_dummy() {
let input = get_input("2022", "02", Some("dummy"));
let output = part_2(&input);
assert_eq!(output, 12);
}
#[test]
fn test_part_2() {
let input = get_input("2022", "02", None);
let output = part_2(&input);
assert_eq!(output, 12111);
}
}