2022-12-01 17:21:27 -06:00
|
|
|
mod common;
|
|
|
|
|
2022-12-01 23:56:36 -06:00
|
|
|
// A -> Rock
|
|
|
|
// B -> Paper
|
|
|
|
// C -> Scissors
|
|
|
|
|
|
|
|
// X -> Rock -> 1
|
|
|
|
// Y -> Paper -> 2
|
|
|
|
// Z -> Scissors -> 3
|
|
|
|
|
2022-12-01 17:21:27 -06:00
|
|
|
fn main() {
|
|
|
|
let input = common::day_input(2);
|
|
|
|
println!("Part 1: {}", part1(&input));
|
|
|
|
println!("Part 2: {}", part2(&input));
|
|
|
|
}
|
|
|
|
|
2022-12-01 23:56:36 -06:00
|
|
|
fn rps(a: char, b: char) -> i32 {
|
|
|
|
match b {
|
|
|
|
'X' => {
|
|
|
|
1 + match a {
|
|
|
|
'A' => 3,
|
|
|
|
'B' => 0,
|
|
|
|
'C' => 6,
|
|
|
|
_ => 1000000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'Y' => {
|
|
|
|
2 + match a {
|
|
|
|
'A' => 6,
|
|
|
|
'B' => 3,
|
|
|
|
'C' => 0,
|
|
|
|
_ => 1000000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'Z' => {
|
|
|
|
3 + match a {
|
|
|
|
'A' => 0,
|
|
|
|
'B' => 6,
|
|
|
|
'C' => 3,
|
|
|
|
_ => 1000000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => 1000000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 17:21:27 -06:00
|
|
|
fn part1(input: &str) -> i32 {
|
2022-12-01 23:56:36 -06:00
|
|
|
input
|
|
|
|
.lines()
|
|
|
|
.map(|s| {
|
|
|
|
let mut c = s.chars();
|
|
|
|
rps(c.nth(0).unwrap(), c.nth(1).unwrap())
|
|
|
|
})
|
|
|
|
.sum()
|
2022-12-01 17:21:27 -06:00
|
|
|
}
|
|
|
|
|
2022-12-02 00:00:00 -06:00
|
|
|
fn rps2(a: char, b: char) -> i32 {
|
|
|
|
match b {
|
|
|
|
'X' => {
|
|
|
|
0 + match a {
|
|
|
|
'A' => 3,
|
|
|
|
'B' => 1,
|
|
|
|
'C' => 2,
|
|
|
|
_ => 1000000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'Y' => {
|
|
|
|
3 + match a {
|
|
|
|
'A' => 1,
|
|
|
|
'B' => 2,
|
|
|
|
'C' => 3,
|
|
|
|
_ => 1000000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'Z' => {
|
|
|
|
6 + match a {
|
|
|
|
'A' => 2,
|
|
|
|
'B' => 3,
|
|
|
|
'C' => 1,
|
|
|
|
_ => 1000000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => 1000000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 17:21:27 -06:00
|
|
|
fn part2(input: &str) -> i32 {
|
2022-12-02 00:00:00 -06:00
|
|
|
input
|
|
|
|
.lines()
|
|
|
|
.map(|s| {
|
|
|
|
let mut c = s.chars();
|
|
|
|
rps2(c.nth(0).unwrap(), c.nth(1).unwrap())
|
|
|
|
})
|
|
|
|
.sum()
|
2022-12-01 17:21:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2022-12-01 23:56:36 -06:00
|
|
|
const TEST_INPUT: &str = "A Y
|
|
|
|
B X
|
|
|
|
C Z";
|
2022-12-01 17:21:27 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_part1() {
|
2022-12-01 23:56:36 -06:00
|
|
|
assert_eq!(part1(TEST_INPUT), 15)
|
2022-12-01 17:21:27 -06:00
|
|
|
}
|
|
|
|
|
2022-12-02 00:00:00 -06:00
|
|
|
#[test]
|
|
|
|
fn test_part2() {
|
|
|
|
assert_eq!(part2(TEST_INPUT), 12)
|
|
|
|
}
|
2022-12-01 17:21:27 -06:00
|
|
|
}
|