Small formatting wibbles.

This commit is contained in:
2020-12-08 10:03:27 -08:00
parent ec0205f8de
commit a77a039c70
9 changed files with 31 additions and 18 deletions

View File

@@ -1,8 +1,8 @@
use advent2020::errors::{ExecutionError, InstructionParseError, TopLevelError}; use advent2020::errors::{ExecutionError, InstructionParseError, TopLevelError};
use std::{collections::HashSet, env};
use std::fmt; use std::fmt;
use std::fs; use std::fs;
use std::str::FromStr; use std::str::FromStr;
use std::{collections::HashSet, env};
#[derive(Clone)] #[derive(Clone)]
struct Machine { struct Machine {
@@ -27,15 +27,21 @@ impl FromStr for Instruction {
lowered.make_ascii_lowercase(); lowered.make_ascii_lowercase();
let mut items = s.split(' '); let mut items = s.split(' ');
let instruction = items.next().ok_or(InstructionParseError::EmptyInstruction)?; let instruction = items
let operand = items.next().ok_or(InstructionParseError::MissingOperand(instruction.to_string()))?; .next()
.ok_or(InstructionParseError::EmptyInstruction)?;
let operand = items.next().ok_or(InstructionParseError::MissingOperand(
instruction.to_string(),
))?;
let operand_value = isize::from_str(operand)?; let operand_value = isize::from_str(operand)?;
match instruction { match instruction {
"nop" => Ok(Instruction::NOP(operand_value)), "nop" => Ok(Instruction::NOP(operand_value)),
"acc" => Ok(Instruction::ACC(operand_value)), "acc" => Ok(Instruction::ACC(operand_value)),
"jmp" => Ok(Instruction::JMP(operand_value)), "jmp" => Ok(Instruction::JMP(operand_value)),
_ => Err(InstructionParseError::UnknownOpcode(instruction.to_string())), _ => Err(InstructionParseError::UnknownOpcode(
instruction.to_string(),
)),
} }
} }
} }
@@ -72,7 +78,11 @@ impl FromStr for Machine {
impl Machine { impl Machine {
fn pretty_print(&self) { fn pretty_print(&self) {
for (idx, instr) in self.instructions.iter().enumerate() { for (idx, instr) in self.instructions.iter().enumerate() {
let pointer = if (idx as isize) == self.location { "--> " } else { " " }; let pointer = if (idx as isize) == self.location {
"--> "
} else {
" "
};
println!("{} {:04}: {}", pointer, idx, instr); println!("{} {:04}: {}", pointer, idx, instr);
} }
} }
@@ -171,7 +181,10 @@ fn main() -> Result<(), TopLevelError> {
// this is part 2 // this is part 2
for mut variant in machine.variants() { for mut variant in machine.variants() {
if let Ok((true, final_value)) = variant.terminates() { if let Ok((true, final_value)) = variant.terminates() {
println!("\nFound a variant that halts! Its last value is {}", final_value); println!(
"\nFound a variant that halts! Its last value is {}",
final_value
);
variant.pretty_print(); variant.pretty_print();
} }
} }

View File

@@ -108,14 +108,14 @@ impl<'a> From<nom::Err<nom::error::Error<&'a str>>> for BaggageRuleParseError {
} }
} }
#[derive(Error,Debug)] #[derive(Error, Debug)]
pub enum InstructionParseError { pub enum InstructionParseError {
#[error("Unknown opcode {0}")] #[error("Unknown opcode {0}")]
UnknownOpcode(String), UnknownOpcode(String),
#[error("Couldn't convert number: {source}")] #[error("Couldn't convert number: {source}")]
NumConversionError { NumConversionError {
#[from] #[from]
source: ParseIntError source: ParseIntError,
}, },
#[error("Encountered an empty instruction (?)")] #[error("Encountered an empty instruction (?)")]
EmptyInstruction, EmptyInstruction,
@@ -123,7 +123,7 @@ pub enum InstructionParseError {
MissingOperand(String), MissingOperand(String),
} }
#[derive(Error,Debug)] #[derive(Error, Debug)]
pub enum ExecutionError { pub enum ExecutionError {
#[error("Tried to execute non-existent instruction at {0}")] #[error("Tried to execute non-existent instruction at {0}")]
NonExistentLocation(isize), NonExistentLocation(isize),