create golf version of quine

This commit is contained in:
Crispy 2024-08-11 19:28:17 +02:00
parent aa610035b1
commit 5b59f487e6
7 changed files with 365 additions and 1 deletions

View file

@ -8,6 +8,7 @@ use image::{GenericImageView, ImageReader};
const FULL_CHARSET: &[u8] = b" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\n\t";
fn main() {
golfed();
// source code conversion
let source_text = read_to_string("../Assets/test/quine.shader").unwrap();
let mut encoded_text: Vec<u32> = Vec::new();
@ -35,6 +36,10 @@ fn main() {
temp = 0;
}
}
if i > 0{
temp <<= (4-i)*8;
encoded_text.push(temp);
}
let mut out_string = String::new();
out_string += "\t\t\t\tconst uint text[] = {";
for t in &encoded_text {
@ -76,3 +81,75 @@ fn main() {
let mut file = File::create("out.h").unwrap();
file.write_all(out_string.as_bytes()).unwrap();
}
fn golfed() {
// source code conversion
let source_text = read_to_string("../Assets/test/quine_golf.shader").unwrap();
let mut encoded_text: Vec<u32> = Vec::new();
let mut temp = 0;
let mut i = 0;
let mut blob_start = 0;
let mut used_charset:Vec<_> = " 0123456789abcdefx,".chars().collect();
for (index, char) in source_text.chars().enumerate() {
if char == '?' {
blob_start = index;
}
temp <<= 8;
let char_index = used_charset
.iter()
.position(|&c| c == char)
.unwrap_or_else(|| {
used_charset.push(char);
used_charset.len() - 1
});
temp |= (char_index as u32) & 0xff;
i += 1;
if i == 4 {
i = 0;
encoded_text.push(temp);
temp = 0;
}
}
if i > 0{
temp <<= (4-i)*8;
encoded_text.push(temp);
}
let mut out_string = String::new();
out_string += "w t[]={";
for t in &encoded_text {
out_string += &format!("{t:#010x},");
}
out_string += "};\n";
out_string += &format!("w L={};\n", encoded_text.len()*11);
out_string += &format!("w b={blob_start};\n");
// font
let img = ImageReader::open("6x6.png").unwrap().decode().unwrap();
let mut out = Vec::new();
for c in used_charset {
let col = c as u32 & 0b1_1111;
let row = (c as u32 >> 5) & 0b111;
let mut encoded: [u32; 4] = [0; 4];
for section in 0..4 {
for y in 0..4 {
for x in 0..8 {
let px = col * 9 + x;
let py = row * 16 + (16 - 1 - section * 4) - y;
let pixel = (img.get_pixel(px, py).0[0] != 0) as u32;
let offset = x + ((y & 3) << 3);
encoded[section as usize] |= pixel << offset;
}
}
}
out.push(encoded);
}
out_string += "w A[][4]={";
for (_i, c) in out.iter().enumerate() {
out_string += &format!("{{{},{},{},{}}},", c[0], c[1], c[2], c[3]);
}
out_string += "};\n";
let mut file = File::create("out_golfed.h").unwrap();
file.write_all(out_string.as_bytes()).unwrap();
}