-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
62 lines (57 loc) · 2.01 KB
/
main.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
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn main() {
// File hosts must exist in current path before this produces output
if let Ok(lines) = read_lines("./2.input") {
// Consumes the iterator, returns an (Optional) String
let mut total = 0;
for line in lines {
if let Ok(line_str) = line {
let plays: Vec<&str> = line_str.split(" ").collect();
let opp_play = match plays[0] {
"A" => "R",
"B" => "P",
"C" => "S",
_ => "XXX"
};
let outcome = match plays[1] {
"X" => 0,
"Y" => 3,
"Z" => 6,
_ => 9999
};
let my_play: &str;
if outcome == 3 {
my_play = opp_play;
} else {
my_play = match (opp_play, outcome) {
("R", 0) => "S",
("R", 6) => "P",
("P", 0) => "R",
("P", 6) => "S",
("S", 0) => "P",
("S", 6) => "R",
_ => "XXXX"
};
}
let bonus = match my_play {
"R" => 1,
"P" => 2,
"S" => 3,
_ => 9999
};
let score = bonus + outcome;
total += score;
println!("{} : {}-{} ({} + {} = {}) -> {}", line_str, opp_play, my_play, bonus, outcome, score, total);
}
}
}
}
// The output is wrapped in a Result to allow matching on errors
// Returns an Iterator to the Reader of the lines of the file.
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}