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

@@ -118,4 +118,4 @@ fn next_target(low_index: usize, high_index: usize, avoid: &[usize]) -> Option<u
}
None
}
}

View File

@@ -211,4 +211,4 @@ fn main() -> Result<(), TopLevelError> {
);
Ok(())
}
}

View File

@@ -147,4 +147,4 @@ fn main() -> Result<(), TopLevelError> {
}
Err(TopLevelError::NoSolutionFound)
}
}

View File

@@ -73,4 +73,4 @@ fn every_seat() -> BTreeSet<char> {
}
result
}
}

View File

@@ -1,8 +1,8 @@
use advent2020::errors::{ExecutionError, InstructionParseError, TopLevelError};
use std::{collections::HashSet, env};
use std::fmt;
use std::fs;
use std::str::FromStr;
use std::{collections::HashSet, env};
#[derive(Clone)]
struct Machine {
@@ -27,15 +27,21 @@ impl FromStr for Instruction {
lowered.make_ascii_lowercase();
let mut items = s.split(' ');
let instruction = items.next().ok_or(InstructionParseError::EmptyInstruction)?;
let operand = items.next().ok_or(InstructionParseError::MissingOperand(instruction.to_string()))?;
let instruction = items
.next()
.ok_or(InstructionParseError::EmptyInstruction)?;
let operand = items.next().ok_or(InstructionParseError::MissingOperand(
instruction.to_string(),
))?;
let operand_value = isize::from_str(operand)?;
match instruction {
"nop" => Ok(Instruction::NOP(operand_value)),
"acc" => Ok(Instruction::ACC(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 {
fn pretty_print(&self) {
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);
}
}
@@ -171,10 +181,13 @@ fn main() -> Result<(), TopLevelError> {
// this is part 2
for mut variant in machine.variants() {
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();
}
}
Ok(())
}
}

View File

@@ -169,4 +169,4 @@ fn main() -> Result<(), TopLevelError> {
);
Ok(())
}
}

View File

@@ -96,4 +96,4 @@ fn main() -> Result<(), TopLevelError> {
);
Ok(())
}
}

View File

@@ -90,4 +90,4 @@ fn main() -> Result<(), TopLevelError> {
println!("The product of the trees encountered is {}", product);
Ok(())
}
}

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 {
#[error("Unknown opcode {0}")]
UnknownOpcode(String),
#[error("Couldn't convert number: {source}")]
NumConversionError {
#[from]
source: ParseIntError
source: ParseIntError,
},
#[error("Encountered an empty instruction (?)")]
EmptyInstruction,
@@ -123,8 +123,8 @@ pub enum InstructionParseError {
MissingOperand(String),
}
#[derive(Error,Debug)]
#[derive(Error, Debug)]
pub enum ExecutionError {
#[error("Tried to execute non-existent instruction at {0}")]
NonExistentLocation(isize),
}
}