quine golf: store 5 chars per uint by treating them as base 80 digits, 69x69 grid! total 4751 bytes

This commit is contained in:
Crispy 2024-08-12 19:29:43 +02:00
parent 9e20abb767
commit 725db5735b
3 changed files with 31 additions and 26 deletions

View file

@ -36,8 +36,8 @@ fn main() {
temp = 0;
}
}
if i > 0{
temp <<= (4-i)*8;
if i > 0 {
temp <<= (4 - i) * 8;
encoded_text.push(temp);
}
let mut out_string = String::new();
@ -82,7 +82,6 @@ fn main() {
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();
@ -90,13 +89,13 @@ fn golfed() {
let mut temp = 0;
let mut i = 0;
let mut blob_start = 0;
let mut used_charset:Vec<_> = " 0123456789abcdefx,".chars().collect();
let mut used_charset: Vec<_> = " 0123456789abcdefx,".chars().collect();
for (index, char) in source_text.chars().enumerate() {
if char == '?' && blob_start==0 {
if char == '?' && blob_start == 0 {
blob_start = index;
continue;
}
temp <<= 8;
temp *= 80;
let char_index = used_charset
.iter()
.position(|&c| c == char)
@ -104,16 +103,18 @@ fn golfed() {
used_charset.push(char);
used_charset.len() - 1
});
temp |= (char_index as u32) & 0xff;
temp += char_index as u32;
i += 1;
if i == 4 {
if i == 5 {
i = 0;
encoded_text.push(temp);
temp = 0;
}
}
if i > 0{
temp <<= (4-i)*8;
if i > 0 {
for _ in 0..(5 - i) {
temp *= 80;
}
encoded_text.push(temp);
}
let mut out_string = String::new();
@ -122,8 +123,12 @@ fn golfed() {
out_string += &format!("{t:#010x},");
}
out_string += "};\n";
out_string += &format!("w L={};", encoded_text.len()*11);
out_string += &format!("w b={blob_start};\n");
out_string += &format!(
"uint magic_number_L_plus_b= {};\n",
encoded_text.len() * 11 + blob_start
);
out_string += &format!("uint magic_number_L= {};\n", encoded_text.len() * 11);
// font
let img = ImageReader::open("6x6.png").unwrap().decode().unwrap();
@ -131,16 +136,14 @@ fn golfed() {
for c in used_charset {
let col = c as u32 & 0b1_1111;
let row = (c as u32 >> 5) & 0b111;
let mut encoded=0u32;
let mut encoded = 0u32;
for y in 0..6 {
for x in 0..5 {
let px = col * 6 + x;
let py = row * 6 + 5 - y;
let pixel = (img.get_pixel(px, py).0[0] >40) as u32;
let pixel = (img.get_pixel(px, py).0[0] > 40) as u32;
let offset = x + y * 5;
// if offset < 32{
encoded |= pixel << offset;
// }
encoded |= pixel << offset;
}
}
out.push(encoded);
@ -153,4 +156,4 @@ fn golfed() {
out_string += "};\n";
let mut file = File::create("out_golfed.h").unwrap();
file.write_all(out_string.as_bytes()).unwrap();
}
}