-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_2.rs
61 lines (54 loc) · 1.81 KB
/
day_2.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
pub fn run() {
let input_str = include_str!("../../inputs/input_2.txt");
// split input into a list of lists (size 2) of &strs
// each internal list is a round, eg. ["A", "X"]
let input: Vec<Vec<&str>> = input_str
.lines()
.map(|i| i
.split(' ')
.collect::<Vec<&str>>()
).collect();
part_one(&input);
part_two(&input);
}
fn part_one(input: &[Vec<&str>]) {
let mut score = 0;
for game in input {
// increase score by result points (6 for win, 3 for draw, 0 for loss)
score += match game[0] {
"A" => if game[1] == "Y" { 6 } else if game[1] == "X" { 3 } else { 0 },
"B" => if game[1] == "Z" { 6 } else if game[1] == "Y" { 3 } else { 0 },
"C" => if game[1] == "X" { 6 } else if game[1] == "Z" { 3 } else { 0 },
_ => {
println!("Invalid character '{}'", game[0]);
0
},
};
// increase score by choice points (1 for X, 2 for Y, 3 for Z)
score += match game[1] {
"X" => 1,
"Y" => 2,
"Z" => 3,
_ => {
println!("Invalid character '{}'", game[1]);
0
},
};
}
println!("Part one: {score}");
}
fn part_two(input: &[Vec<&str>]) {
let mut score = 0;
for game in input {
score += match game[1] {
"X" => if game[0] == "A" { 3 } else if game[0] == "C" { 2 } else { 1 },
"Y" => 3 + if game[0] == "C" { 3 } else if game[0] == "B" { 2 } else { 1 },
"Z" => 6 + if game[0] == "B" { 3 } else if game[0] == "A" { 2 } else { 1 },
_ => {
println!("Invalid character '{}'", game[0]);
0
},
};
}
println!("Part two: {score}");
}