Split into parts and learn about borrow checking smore
This commit is contained in:
parent
2525f43f99
commit
5f08074bf5
|
@ -2,7 +2,7 @@ use num_derive::FromPrimitive;
|
|||
use num_traits::FromPrimitive;
|
||||
use std::cmp::Ordering;
|
||||
use std::fs;
|
||||
use std::io::{self, BufRead};
|
||||
use std::io::{self, BufRead, Seek, Read};
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, FromPrimitive)]
|
||||
|
@ -92,8 +92,47 @@ impl PlayStrat {
|
|||
fn main() {
|
||||
let filename = "etc/p02.txt";
|
||||
let file = fs::File::open(filename).expect("Can't open file");
|
||||
let lines = io::BufReader::new(file).lines();
|
||||
let mut reader = io::BufReader::new(file);
|
||||
|
||||
println!("PART ONE ---------------");
|
||||
part1(reader.by_ref().lines());
|
||||
reader.rewind().expect("Can't rewind");
|
||||
|
||||
println!("\n PART TWO ---------------");
|
||||
part2(reader.by_ref().lines());
|
||||
}
|
||||
|
||||
fn part1(lines: std::io::Lines<&mut io::BufReader<fs::File>>) {
|
||||
let mut total_score: u32 = 0;
|
||||
for l in lines {
|
||||
let line = l.expect("Can't read line.");
|
||||
let mut split = line.split_ascii_whitespace();
|
||||
let theirs = split
|
||||
.next()
|
||||
.expect("Want two items per line")
|
||||
.parse::<Rps>()
|
||||
.expect("Can't get their move");
|
||||
let mine = split
|
||||
.next()
|
||||
.expect("Want two items per line")
|
||||
.parse::<Rps>()
|
||||
.expect("Can't get my strategy");
|
||||
|
||||
let score = mine.score(theirs);
|
||||
total_score += score;
|
||||
println!(
|
||||
"Line \"{}\" - Theirs: {:?}, Mine: {:?}, score {}",
|
||||
line,
|
||||
theirs,
|
||||
mine,
|
||||
score,
|
||||
);
|
||||
}
|
||||
|
||||
println!("Total score is {}", total_score);
|
||||
}
|
||||
|
||||
fn part2(lines: std::io::Lines<&mut io::BufReader<fs::File>>) {
|
||||
let mut total_score: u32 = 0;
|
||||
for l in lines {
|
||||
let line = l.expect("Can't read line.");
|
||||
|
@ -114,14 +153,12 @@ fn main() {
|
|||
let score = mine.score(theirs);
|
||||
total_score += score;
|
||||
println!(
|
||||
"Line \"{}\" - Theirs: {:?}, Strat: {:?}, Mine: {:?} ({:?}), score {} (so far {})",
|
||||
"Line \"{}\" - Theirs: {:?}, Strat: {:?}, Mine: {:?}, score {}",
|
||||
line,
|
||||
theirs,
|
||||
strat,
|
||||
mine,
|
||||
mine.cmp(&theirs),
|
||||
score,
|
||||
total_score,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue