-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-2.rs
122 lines (111 loc) · 4.08 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::fs;
#[derive(PartialEq)]
enum Shape {
Rock, Paper, Scissors
}
enum Instruction {
Win, Lose, Draw
}
fn main () {
let file_str = fs::read_to_string("day-2.input").expect("Failed to read file");
let points = file_str.trim().split("\n").map(
|round| {
round.split_once(' ')
.and_then(|(them, me)| {
let me = match me {
"X" => Some(Shape::Rock),
"Y" => Some(Shape::Paper),
"Z" => Some(Shape::Scissors),
_ => None,
};
let them = match them {
"A" => Some(Shape::Rock),
"B" => Some(Shape::Paper),
"C" => Some(Shape::Scissors),
_ => None,
};
them.zip(me)
})
.map(|(them, me)| {
let shape_points = match me {
Shape::Rock => 1,
Shape::Paper => 2,
Shape::Scissors => 3,
};
let outcome_points = match (me, them) {
// Loss conditions
(Shape::Rock, Shape::Paper) => 0,
(Shape::Paper, Shape::Scissors) => 0,
(Shape::Scissors, Shape::Rock) => 0,
(me, them) => match me == them {
// Draw conditions
true => 3,
// Win conditions (by elimination)
false => 6
},
};
shape_points + outcome_points
})
}
).fold(Some(0), |acc : Option<i32>, next| acc.zip(next).map(|(acc, next)| acc + next));
if let Some(points) = points {
println!("Part 1 Total points: {}", points);
} else {
println!("Failed to parse!")
}
let points = file_str.trim().split("\n").map(
|round| {
round.split_once(' ')
.and_then(|(them, instruction)| {
let instruction = match instruction {
"X" => Some(Instruction::Lose),
"Y" => Some(Instruction::Draw),
"Z" => Some(Instruction::Win),
_ => None,
};
let them = match them {
"A" => Some(Shape::Rock),
"B" => Some(Shape::Paper),
"C" => Some(Shape::Scissors),
_ => None,
};
them.zip(instruction)
})
.map(|(them, instruction)| {
let me = match instruction {
Instruction::Win => match them {
Shape::Rock => Shape::Paper,
Shape::Paper => Shape::Scissors,
Shape::Scissors => Shape::Rock,
},
Instruction::Lose => match them {
Shape::Rock => Shape::Scissors,
Shape::Paper => Shape::Rock,
Shape::Scissors => Shape::Paper,
},
Instruction::Draw => match them {
Shape::Rock => Shape::Rock,
Shape::Paper => Shape::Paper,
Shape::Scissors => Shape::Scissors,
},
};
let shape_points = match me {
Shape::Rock => 1,
Shape::Paper => 2,
Shape::Scissors => 3,
};
let outcome_points = match instruction {
Instruction::Win => 6,
Instruction::Lose => 0,
Instruction::Draw => 3,
};
shape_points + outcome_points
})
}
).fold(Some(0), |acc : Option<i32>, next| acc.zip(next).map(|(acc, next)| acc + next));
if let Some(points) = points {
println!("Part 2 Total points: {}", points);
} else {
println!("Failed to parse!")
}
}