Skip to content

Commit 3b2705c

Browse files
committed
fix(day_02): remove inline solution
1 parent 907e968 commit 3b2705c

File tree

3 files changed

+60
-40
lines changed

3 files changed

+60
-40
lines changed

day_02/src/hand.rs

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,24 @@ pub enum Hand {
55
Scissors,
66
}
77

8+
pub enum Outcome {
9+
Win,
10+
Draw,
11+
Lose,
12+
}
13+
14+
impl Outcome {
15+
pub fn sum(&self) -> u32 {
16+
match self {
17+
Win => 6,
18+
Draw => 3,
19+
Lose => 0,
20+
}
21+
}
22+
}
23+
824
use Hand::*;
25+
use Outcome::*;
926

1027
impl Hand {
1128
pub fn new(input: &char) -> Self {
@@ -16,7 +33,7 @@ impl Hand {
1633
}
1734
}
1835

19-
fn shape_sum(&self) -> u32 {
36+
fn sum(&self) -> u32 {
2037
match self {
2138
Rock => 1,
2239
Paper => 2,
@@ -27,25 +44,54 @@ impl Hand {
2744
fn outcome(&self, opponent: &Hand) -> u32 {
2845
match opponent {
2946
Rock => match self {
30-
Rock => 3,
31-
Paper => 6,
32-
Scissors => 0,
47+
Rock => Draw.sum(),
48+
Paper => Win.sum(),
49+
Scissors => Lose.sum(),
3350
},
3451
Paper => match self {
35-
Rock => 0,
36-
Paper => 3,
37-
Scissors => 6,
52+
Rock => Lose.sum(),
53+
Paper => Draw.sum(),
54+
Scissors => Win.sum(),
3855
},
3956
Scissors => match self {
40-
Rock => 6,
41-
Paper => 0,
42-
Scissors => 3,
57+
Rock => Win.sum(),
58+
Paper => Lose.sum(),
59+
Scissors => Draw.sum(),
4360
},
4461
}
4562
}
4663

47-
pub fn round(&self, opponent: &Hand) -> u32 {
48-
let shape_sum = self.shape_sum();
64+
pub fn cheat(&self, opponent: &Hand) -> u32 {
65+
match self {
66+
Rock => {
67+
Lose.sum()
68+
+ match opponent {
69+
Rock => Scissors.sum(),
70+
Paper => Rock.sum(),
71+
Scissors => Paper.sum(),
72+
}
73+
}
74+
Paper => {
75+
Draw.sum()
76+
+ match opponent {
77+
Rock => Rock.sum(),
78+
Paper => Paper.sum(),
79+
Scissors => Scissors.sum(),
80+
}
81+
}
82+
Scissors => {
83+
Win.sum()
84+
+ match opponent {
85+
Rock => Paper.sum(),
86+
Paper => Scissors.sum(),
87+
Scissors => Rock.sum(),
88+
}
89+
}
90+
}
91+
}
92+
93+
pub fn play(&self, opponent: &Hand) -> u32 {
94+
let shape_sum = self.sum();
4995
let outcome = self.outcome(opponent);
5096
shape_sum + outcome
5197
}

day_02/src/part_01.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::Input;
55
pub fn main(input: &Input) -> Result<u32> {
66
Ok(input
77
.into_iter()
8-
.map(|(opponent, elf)| elf.round(opponent))
8+
.map(|(opponent, elf)| elf.play(opponent))
99
.sum())
1010
}
1111

day_02/src/part_02.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
11
use std::io::Result;
22

3-
use crate::Hand::*;
43
use crate::Input;
54

65
pub fn main(input: &Input) -> Result<u32> {
76
Ok(input
87
.into_iter()
9-
.map(|(opponent, outcome)| match outcome {
10-
// lose
11-
Rock => {
12-
0 + match opponent {
13-
Rock => 3,
14-
Paper => 1,
15-
Scissors => 2,
16-
}
17-
}
18-
// draw
19-
Paper => {
20-
3 + match opponent {
21-
Rock => 1,
22-
Paper => 2,
23-
Scissors => 3,
24-
}
25-
}
26-
// win
27-
Scissors => {
28-
6 + match opponent {
29-
Rock => 2,
30-
Paper => 3,
31-
Scissors => 1,
32-
}
33-
}
34-
})
8+
.map(|(opponent, instruction)| instruction.cheat(opponent))
359
.sum())
3610
}
3711

0 commit comments

Comments
 (0)