generated from bravit/advent-of-code-rust-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_02.rs
71 lines (55 loc) · 2.12 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
use anyhow::*;
use crate::Solution;
const TEST: &str = "\
A Y
B X
C Z
";
fn split (content: &str) -> Vec<&str> {
content.lines().collect()
}
/// Solve first part of the puzzle
fn part_a (content: &[&str]) -> anyhow::Result<usize> {
// Translate directly an input to its corresponding score
let score: u32 = content.iter().map(|row| {
match row {
r if *r == "A X" => 1 + 3, // Rock Rock
r if *r == "A Y" => 2 + 6, // Rock Paper
r if *r == "A Z" => 3 + 0, // Rock Scissors
r if *r == "B X" => 1 + 0, // Paper Rock
r if *r == "B Y" => 2 + 3, // Paper Paper
r if *r == "B Z" => 3 + 6, // Paper Scissors
r if *r == "C X" => 1 + 6, // Scissors Rock
r if *r == "C Y" => 2 + 0, // Scissors Paper
r if *r == "C Z" => 3 + 3, // Scissors Scissors
_ => panic!("Unexpected row: {}", row),
}
}).sum();
Ok(score as usize)
}
/// Solve second part of the puzzle
fn part_b (content: &[&str]) -> anyhow::Result<usize> {
// Translate directly an input to its corresponding score
let score: u32 = content.iter().map(|row| {
match row {
r if *r == "A X" => 3 + 0, // Rock Loose Scissors
r if *r == "A Y" => 1 + 3, // Rock Draw Rock
r if *r == "A Z" => 2 + 6, // Rock Win Paper
r if *r == "B X" => 1 + 0, // Paper Loose Rock
r if *r == "B Y" => 2 + 3, // Paper Draw Paper
r if *r == "B Z" => 3 + 6, // Paper Win Scissors
r if *r == "C X" => 2 + 0, // Scissors Loose Paper
r if *r == "C Y" => 3 + 3, // Scissors Draw Scissors
r if *r == "C Z" => 1 + 6, // Scissors Win Rock
_ => panic!(),
}
}).sum();
Ok(score as usize)
}
pub fn day_2(content: &[&str]) -> anyhow::Result<(Solution, Solution)> {
debug_assert!(part_a(&split(TEST)).unwrap_or_default() == 15);
debug_assert!(part_b(&split(TEST)).unwrap_or_default() == 12);
let ra = part_a(content)?;
let rb = part_b(content)?;
Ok((Solution::Unsigned(ra), Solution::Unsigned(rb)))
}