d10p2 trying question mark

This commit is contained in:
Dory 2024-03-12 09:21:21 -07:00
parent 2cbcc69ff3
commit 6fe087abb2

View File

@ -1,3 +1,4 @@
use std::num;
use std::env; use std::env;
use std::process; use std::process;
use std::fs::File; use std::fs::File;
@ -11,14 +12,16 @@ enum Inst{
#[derive(Debug)] #[derive(Debug)]
struct ParseError(String); struct ParseError(String);
impl From<num::ParseIntError> for ParseError {
fn from(e: num::ParseIntError) -> Self {
ParseError(e.to_string())
}
}
fn parse_line(line: String) -> Result<Inst, ParseError> { fn parse_line(line: String) -> Result<Inst, ParseError> {
match &line[..4] { match &line[..4] {
"noop" => Ok(Inst::Noop), "noop" => Ok(Inst::Noop),
"addx" => match line[5..].parse::<i64>() { "addx" => Ok(Inst::AddX(line[5..].parse::<i64>()?)),
Ok(operand) => Ok(Inst::AddX(operand)),
Err(e) => Err(ParseError(e.to_string())),
},
_ => Err(ParseError(String::from("bad instruction"))), _ => Err(ParseError(String::from("bad instruction"))),
} }
} }