Last active
October 28, 2020 12:44
-
-
Save oxalica/84dd3b60e50b6beef5371ed2767f50b0 to your computer and use it in GitHub Desktop.
Brainfuck quine generator
This file contains hidden or 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 brainfuck::{program::Program, tape}; // brainfuck = "0.2.1" | |
| fn gen_printer(s: &str) -> String { | |
| assert!(!s.is_empty()); | |
| let s = s.as_bytes(); | |
| let mut buf = "+".repeat(s[0] as usize) + "."; | |
| for (&prev, &cur) in s.iter().zip(&s[1..]) { | |
| if prev < cur { | |
| buf += &"+".repeat((cur - prev) as usize); | |
| } else if cur < prev { | |
| buf += &"-".repeat((prev - cur) as usize); | |
| } | |
| buf += "."; | |
| } | |
| buf += "[-]"; | |
| buf | |
| } | |
| // 8505 bytes | |
| fn gen_quine1() -> String { | |
| const SHIFT: usize = 3; | |
| let header = ">".repeat(SHIFT); | |
| let footer = { | |
| let rewind = "<[<]".to_owned(); | |
| let header_printer = gen_printer(&header); | |
| let data_duper = format!( | |
| ">[<{}>[-<.<+>>]<{}.[-]>>]<<<[<]", | |
| "+".repeat('+' as usize), | |
| "+".repeat('>' as usize - '+' as usize), | |
| ); | |
| let data_printer = ">[.>]".to_owned(); | |
| rewind + &header_printer + &data_duper + &data_printer | |
| }; | |
| let mut data = String::new(); | |
| for b in footer.bytes() { | |
| data += &"+".repeat(b as usize); | |
| data += ">"; | |
| } | |
| header + &data + &footer | |
| } | |
| // 1085 bytes | |
| fn gen_quine2() -> String { | |
| const TABLE: &[u8] = b"+-.<>[]"; | |
| let header = ">>".to_owned(); | |
| let footer_templ = " | |
| [<] | |
| +62.. | |
| >[ | |
| <.-19> | |
| [-<.<+>>] | |
| <+19[->+<] | |
| >> | |
| ] | |
| <<<[<] | |
| >[ | |
| -[-[-[-[-[-[-<+2>]<+29>]<+2>]<+14>]<+1>]<+2>]<+43 | |
| .>> | |
| ] | |
| "; | |
| let mut footer = String::new(); | |
| let mut cnt = 0; | |
| for ch in footer_templ.chars() { | |
| if ch.is_ascii_digit() { | |
| cnt = cnt * 10 + ch.to_digit(10).unwrap(); | |
| } else { | |
| if cnt != 0 { | |
| let prev = footer.chars().next_back().unwrap(); | |
| for _ in 0..(cnt - 1) { | |
| footer.push(prev); | |
| } | |
| cnt = 0; | |
| } | |
| if !ch.is_ascii_whitespace() { | |
| footer.push(ch); | |
| } | |
| } | |
| } | |
| assert_eq!(cnt, 0); | |
| let mut data = String::new(); | |
| for ch in footer.chars() { | |
| let idx = TABLE | |
| .iter() | |
| .enumerate() | |
| .find(|(_, &b)| b == ch as u8) | |
| .unwrap() | |
| .0; | |
| data.push('>'); | |
| for _ in 0..(idx + 1) { | |
| data.push('+'); | |
| } | |
| } | |
| header + &data + &footer | |
| } | |
| // 993 bytes | |
| fn gen_quine3() -> String { | |
| const TABLE: &[u8] = b"+-.<>[]"; | |
| let header = ">>>".to_owned(); | |
| let footer_templ = " | |
| [<] | |
| <+7[>+9<-]>-... | |
| >[ | |
| <.-19> | |
| [-<.<+>>] | |
| <+19[->+<] | |
| >> | |
| ] | |
| <<<[<] | |
| >[ | |
| -[-[-[-[-[-[-<+2>]<+29>]<+2>]<+14>]<+1>]<+2>]<<+5[>+9<-]>-- | |
| .[-]>> | |
| ] | |
| "; | |
| let mut footer = String::new(); | |
| let mut cnt = 0; | |
| for ch in footer_templ.chars() { | |
| if ch.is_ascii_digit() { | |
| cnt = cnt * 10 + ch.to_digit(10).unwrap(); | |
| } else { | |
| if cnt != 0 { | |
| let prev = footer.chars().next_back().unwrap(); | |
| for _ in 0..(cnt - 1) { | |
| footer.push(prev); | |
| } | |
| cnt = 0; | |
| } | |
| if !ch.is_ascii_whitespace() { | |
| footer.push(ch); | |
| } | |
| } | |
| } | |
| assert_eq!(cnt, 0); | |
| let mut data = String::new(); | |
| for ch in footer.chars() { | |
| let idx = TABLE | |
| .iter() | |
| .enumerate() | |
| .find(|(_, &b)| b == ch as u8) | |
| .unwrap() | |
| .0; | |
| data.push('>'); | |
| for _ in 0..(idx + 1) { | |
| data.push('+'); | |
| } | |
| } | |
| header + &data + &footer | |
| } | |
| fn run_brainfuck(src: &str) -> String { | |
| let mut inp: &[u8] = &[]; | |
| let mut oup: Vec<u8> = Vec::new(); | |
| let program = Program::parse(&src).unwrap(); | |
| brainfuck::Interpreter::<tape::VecTape>::new(program, &mut inp, &mut oup) | |
| .run() | |
| .unwrap(); | |
| String::from_utf8(oup).unwrap() | |
| } | |
| fn assert_quine(src: &str) { | |
| assert_eq!(src, run_brainfuck(src)); | |
| } | |
| fn main() { | |
| let quines = [gen_quine1(), gen_quine2(), gen_quine3()]; | |
| for src in &quines { | |
| assert_quine(&src); | |
| } | |
| print!("{}", quines[2]); | |
| } |
This file contains hidden or 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
| >>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><[<]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...[-]>[<+++++++++++++++++++++++++++++++++++++++++++>[-<.<+>>]<+++++++++++++++++++.[-]>>]<<<[<]>[.>] |
This file contains hidden or 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
| >>>++++++>++++>+++++++>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+++>+++>+++++>++++++>++++>+++>++>++>++>++>++>++>++>++>++>++>++>++>++>++>++>++>++>++>++>+++++>++++++>++>++++>+++>++++>+>+++++>+++++>+++++++>++++>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>++++++>++>+++++>+>++++>+++++++>+++++>+++++>+++++++>++++>++++>++++>++++++>++++>+++++++>+++++>++++++>++>++++++>++>++++++>++>++++++>++>++++++>++>++++++>++>++++++>++>++++>+>+>+++++>+++++++>++++>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+++++>+++++++>++++>+>+>+++++>+++++++>++++>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+++++>+++++++>++++>+>+++++>+++++++>++++>+>+>+++++>+++++++>++++>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+++>+++++>+++++>+++++++[<]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..>[<.------------------->[-<.<+>>]<+++++++++++++++++++[->+<]>>]<<<[<]>[-[-[-[-[-[-[-<++>]<+++++++++++++++++++++++++++++>]<++>]<++++++++++++++>]<+>]<++>]<+++++++++++++++++++++++++++++++++++++++++++.>>] |
This file contains hidden or 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
| >>>>++++++>++++>+++++++>++++>+>+>+>+>+>+>+>++++++>+++++>+>+>+>+>+>+>+>+>+>++++>++>+++++++>+++++>++>+++>+++>+++>+++++>++++++>++++>+++>++>++>++>++>++>++>++>++>++>++>++>++>++>++>++>++>++>++>++>+++++>++++++>++>++++>+++>++++>+>+++++>+++++>+++++++>++++>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>++++++>++>+++++>+>++++>+++++++>+++++>+++++>+++++++>++++>++++>++++>++++++>++++>+++++++>+++++>++++++>++>++++++>++>++++++>++>++++++>++>++++++>++>++++++>++>++++++>++>++++>+>+>+++++>+++++++>++++>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+++++>+++++++>++++>+>+>+++++>+++++++>++++>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+++++>+++++++>++++>+>+++++>+++++++>++++>+>+>+++++>+++++++>++++>++++>+>+>+>+>+>++++++>+++++>+>+>+>+>+>+>+>+>+>++++>++>+++++++>+++++>++>++>+++>++++++>++>+++++++>+++++>+++++>+++++++[<]<+++++++[>+++++++++<-]>-...>[<.------------------->[-<.<+>>]<+++++++++++++++++++[->+<]>>]<<<[<]>[-[-[-[-[-[-[-<++>]<+++++++++++++++++++++++++++++>]<++>]<++++++++++++++>]<+>]<++>]<<+++++[>+++++++++<-]>--.[-]>>] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment