Created
December 25, 2017 05:29
-
-
Save anonymous/c0ac5a65c08822b9da674d5a94e9c0f4 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::VecDeque; | |
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | |
enum State { A, B, C, D, E, F } | |
// enum State { A, B } | |
const ITERATIONS: usize = 12_459_852; | |
// const ITERATIONS: usize = 6; | |
fn main() { | |
let mut tape = VecDeque::new(); | |
tape.push_back(false); | |
let mut cursor = 0; | |
let mut state = State::A; | |
for _ in 0..ITERATIONS { | |
// println!("({}, {:?}): {:?}", cursor, state, tape); | |
// sample | |
/* | |
let (new_val, cursor_change, new_state) = match (state, tape[cursor]) { | |
(State::A, false) => (true, 1, State::B), | |
(State::A, true) => (false, -1, State::B), | |
(State::B, false) => (true, -1, State::A), | |
(State::B, true) => (true, 1, State::A), | |
}; | |
*/ | |
let (new_val, cursor_change, new_state) = match (state, tape[cursor]) { | |
(State::A, false) => (true, 1, State::B), | |
(State::A, true) => (true, -1, State::E), | |
(State::B, false) => (true, 1, State::C), | |
(State::B, true) => (true, 1, State::F), | |
(State::C, false) => (true, -1, State::D), | |
(State::C, true) => (false, 1, State::B), | |
(State::D, false) => (true, 1, State::E), | |
(State::D, true) => (false, -1, State::C), | |
(State::E, false) => (true, -1, State::A), | |
(State::E, true) => (false, 1, State::D), | |
(State::F, false) => (true, 1, State::A), | |
(State::F, true) => (true, 1, State::C), | |
}; | |
state = new_state; | |
tape[cursor] = new_val; | |
if cursor == 0 && cursor_change == -1 { | |
tape.push_front(false); | |
} else { | |
cursor = (cursor as isize + cursor_change) as usize; | |
} | |
if cursor == tape.len() { | |
tape.push_back(false); | |
} | |
} | |
let checksum = tape.iter().filter(|&&b| b).count(); | |
println!("{}", checksum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment