Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d68346c241 | ||
|
|
fddc6b8524 | ||
|
|
e0a7b7b9e8 |
No files matched your search
+25
@@ -22,3 +22,28 @@ impl Display for TuringMachineError {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn busy_beaver_2() {
|
||||
let mut machine = TuringMachine::with_state_entries(
|
||||
[
|
||||
"A|0|1;R;B",
|
||||
"A|1|1;L;C",
|
||||
"B|0|1;L;A",
|
||||
"B|1|1;R;B",
|
||||
"C|0|1;L;B",
|
||||
"C|1|1;R;-HALT-",
|
||||
]
|
||||
.into_iter(),
|
||||
"A",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
while let NextState::State(_) = machine.step() {}
|
||||
assert_eq!(machine.peek(4).to_string(), " 1 1 1 1 [1] 1 0 0 ");
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -2,7 +2,7 @@ use std::{collections::HashMap, fmt::Display, num::NonZeroUsize};
|
||||
|
||||
use crate::{
|
||||
TuringMachineError,
|
||||
tape::{Tape, TapeSymbol},
|
||||
tape::{Tape, TapePeek, TapeSymbol},
|
||||
};
|
||||
|
||||
pub struct TuringMachine {
|
||||
@@ -62,6 +62,12 @@ impl TuringMachine {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TuringMachine {
|
||||
pub fn peek(&'a self, radius: usize) -> TapePeek<'a> {
|
||||
self.tape.peek(radius)
|
||||
}
|
||||
}
|
||||
|
||||
const ALPHABET: [char; 26] = [
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
|
||||
'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||
|
||||
+28
-11
@@ -11,7 +11,7 @@ pub struct Tape {
|
||||
impl Default for Tape {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
left_half: Default::default(),
|
||||
left_half: vec![TapeSymbol::Zero],
|
||||
right_half: vec![TapeSymbol::Zero],
|
||||
head: Default::default(),
|
||||
}
|
||||
@@ -66,43 +66,60 @@ impl Tape {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn peek(&self, radius: usize) -> Vec<&TapeSymbol> {
|
||||
pub fn peek(&self, radius: usize) -> TapePeek<'_> {
|
||||
let mut fields = Vec::new();
|
||||
match self.head.side {
|
||||
TapeSide::Left => {
|
||||
let left_lower_bound = self.head.index.saturating_sub(radius);
|
||||
let left_upper_bound = self.head.index + radius;
|
||||
let left_overshoot = left_upper_bound.saturating_sub(self.left_half.len() - 1);
|
||||
let left_overshoot = left_upper_bound.saturating_sub(self.left_half.len());
|
||||
let right_upper_bound = radius.saturating_sub(self.head.index);
|
||||
let right_overshoot = right_upper_bound.saturating_sub(self.right_half.len() - 1);
|
||||
let right_overshoot = right_upper_bound.saturating_sub(self.right_half.len());
|
||||
|
||||
fields.resize(left_overshoot, &TapeSymbol::Zero);
|
||||
let left_side = self.left_half[left_lower_bound..=self.head.index + radius]
|
||||
let left_side = self.left_half[left_lower_bound..left_upper_bound - left_overshoot]
|
||||
.iter()
|
||||
.rev();
|
||||
fields.extend(left_side);
|
||||
|
||||
let right_side = self.right_half[..=right_upper_bound].iter();
|
||||
let right_side = self.right_half[..right_upper_bound].iter();
|
||||
fields.extend(right_side);
|
||||
fields.resize(fields.len() + right_overshoot, &TapeSymbol::Zero);
|
||||
}
|
||||
TapeSide::Right => {
|
||||
let right_lower_bound = self.head.index.saturating_sub(radius);
|
||||
let right_upper_bound = self.head.index + radius;
|
||||
let right_overshoot = right_upper_bound.saturating_sub(self.right_half.len() - 1);
|
||||
let right_overshoot = right_upper_bound.saturating_sub(self.right_half.len());
|
||||
let left_upper_bound = radius.saturating_sub(self.head.index);
|
||||
let left_overshoot = left_upper_bound.saturating_sub(self.left_half.len() - 1);
|
||||
let left_overshoot = left_upper_bound.saturating_sub(self.left_half.len());
|
||||
|
||||
fields.resize(left_overshoot, &TapeSymbol::Zero);
|
||||
let left_side = self.left_half[..=left_upper_bound].iter().rev();
|
||||
let left_side = self.left_half[..left_upper_bound].iter().rev();
|
||||
fields.extend(left_side);
|
||||
|
||||
let right_side = self.right_half[right_lower_bound..=self.head.index + radius].iter();
|
||||
let right_side =
|
||||
self.right_half[right_lower_bound..right_upper_bound - right_overshoot].iter();
|
||||
fields.extend(right_side);
|
||||
fields.resize(fields.len() + right_overshoot, &TapeSymbol::Zero);
|
||||
}
|
||||
}
|
||||
fields
|
||||
TapePeek(fields)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TapePeek<'a>(Vec<&'a TapeSymbol>);
|
||||
|
||||
impl Display for TapePeek<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let middle = self.0.len() / 2;
|
||||
for (index, symbol) in self.0.iter().enumerate() {
|
||||
if index == middle {
|
||||
write!(f, "[{}]", symbol)?;
|
||||
} else {
|
||||
write!(f, " {} ", symbol)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user