From 945a4e6d2cfe01964ec1d672f39feef27a2acd8e Mon Sep 17 00:00:00 2001 From: primenumber Date: Mon, 17 Jun 2024 23:17:20 +0900 Subject: [PATCH] WIP --- src/record.rs | 10 ++++++---- src/train.rs | 6 ++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/record.rs b/src/record.rs index 6bfb428..cdbeb02 100644 --- a/src/record.rs +++ b/src/record.rs @@ -23,8 +23,8 @@ pub struct ScoreIsNotRegistered {} #[derive(Error, Debug)] pub enum ParseRecordError { - #[error("Failed to parse hand")] - FailedToParseHand, + #[error("Failed to parse hand :{0}")] + FailedToParseHand(String), #[error("invalid hand")] InvalidHand, } @@ -82,9 +82,10 @@ impl FromStr for Record { let splitted = record_str.split_ascii_whitespace().collect::>(); let l = splitted[0].len(); for i in 0..(l / 2) { - let h = splitted[0][(2 * i)..(2 * i + 2)] + let hand_s = &splitted[0][(2 * i)..(2 * i + 2)]; + let h = hand_s .parse::() - .or(Err(ParseRecordError::FailedToParseHand))?; + .or(Err(ParseRecordError::FailedToParseHand(hand_s.to_string())))?; board = match board.play_hand(h) { Some(next) => next, None => { @@ -136,6 +137,7 @@ pub fn load_records(path: &Path) -> Result> { reader.read_line(&mut buffer)?; let remain = buffer.trim().parse()?; + buffer.clear(); Ok(LoadRecords { reader, diff --git a/src/train.rs b/src/train.rs index 466af53..ab951b9 100644 --- a/src/train.rs +++ b/src/train.rs @@ -50,8 +50,10 @@ pub fn gen_dataset(matches: &ArgMatches) { eprintln!("Parse input..."); let mut boards_with_results = Vec::new(); - for record in load_records(Path::new(input_path)).unwrap() { - let mut timeline = record.unwrap().timeline().unwrap(); + let input_file = File::open(Path::new(input_path)).unwrap(); + for line in BufReader::new(input_file).lines() { + let record = line.unwrap().parse::().unwrap(); + let mut timeline = record.timeline().unwrap(); boards_with_results.append(&mut timeline); }