ICFP 2007 Contest: https://web.archive.org/web/20090301164728/https://save-endo.cs.uu.nl/

Fixes to pass self-test!

Changed files
+44 -20
dna2rna
prefixes
rna2bmp
+14 -6
dna2rna/src/match_replace.rs
···
replace += DnaRef::from_base(b);
}
TemplateItem::Ref(n, l) => {
-
let (start, end) = matched.groups[n];
-
let (_, rhs) = dna.split(start);
-
let (group, _) = rhs.split(end - start);
-
replace += protect(group, l);
+
if n < matched.groups.len() {
+
let (start, end) = matched.groups[n];
+
let (_, rhs) = dna.split(start);
+
let (group, _) = rhs.split(end - start);
+
replace += protect(group, l);
+
} else {
+
replace += protect(DnaRef::new(), l);
+
}
}
TemplateItem::Len(n) => {
-
let (start, end) = matched.groups[n];
-
replace += asnat(end - start);
+
if n < matched.groups.len() {
+
let (start, end) = matched.groups[n];
+
replace += asnat(end - start);
+
} else {
+
replace += asnat(0);
+
}
}
}
}
+1
prefixes/startup.dna
···
+
IIPIFFCPICFPPICIICCIICIPPPFIIC
+9 -4
rna2bmp/src/bitmap.rs
···
pub type Point = (usize, usize);
pub type Pixel = (u8, u8, u8, u8);
+
// A bitmap is large enough that it will stack overflow. The Box isn't
+
// strictly necessary if I can just be careful everywhere, but it's nice
+
// to have.
pub struct Bitmap {
-
data: [[Pixel; SIZE]; SIZE],
+
data: Box<[[Pixel; SIZE]; SIZE]>,
}
impl Bitmap {
pub fn new() -> Bitmap {
Bitmap {
-
data: [[(0, 0, 0, 0); SIZE]; SIZE],
+
data: Box::new([[(0, 0, 0, 0); SIZE]; SIZE]),
}
}
···
pub fn to_data(&self) -> Vec<u8> {
let mut writer = Writer::new();
-
let row_padding = 3 - (3 * SIZE % 4);
+
let row_padding = (4 - (3 * SIZE) % 4) % 4;
+
let pix_size: u32 = (SIZE * (3 * SIZE + row_padding)) as u32;
writer.output_u16(0x4d42); // BM
-
writer.output_u32((0x36 + SIZE * (3 * SIZE + row_padding)) as u32); // Size
+
writer.output_u32(0x36 + pix_size); // Size
writer.output_u16(0x0); // Reserved
writer.output_u16(0x0); // Reserved
writer.output_u32(0x36); // Offset of pixel array
···
writer.output_u16(0x1); // Color planes
writer.output_u16(0x18); // Bits per pixel
writer.output_u32(0x0); // No pixel array compression
+
writer.output_u32(pix_size); // Pixel data size
writer.output_u32(0x0); // Horizontal resolution
writer.output_u32(0x0); // Vertical resolution
writer.output_u32(0x0); // Colors in the palette
+2
rna2bmp/src/main.rs
···
if fs::write(&args[2], bmp.to_data()).is_err() {
println!("Could not write to {}", args[2]);
}
+
} else {
+
println!("No bitmap after RNA processing");
}
} else {
println!("Could not open {}", args[1]);
+18 -10
rna2bmp/src/rna_processor.rs
···
return (
(r * a / 255) as u8,
-
(r * g / 255) as u8,
-
(r * b / 255) as u8,
+
(g * a / 255) as u8,
+
(b * a / 255) as u8,
a as u8,
);
}
···
position: (0, 0),
mark: (0, 0),
dir: (1, 0),
-
bitmaps: Vec::new(),
+
bitmaps: vec![Bitmap::new()],
}
}
···
} else if r == "PCCIFFP" {
self.mark = self.position;
} else if r == "PFFICCP" {
-
// TODO: Can I rework this to reduce casts or locals?
+
// TODO: Can I rework this to reduce casts or locals?
let x0 = self.position.0 as isize;
let y0 = self.position.1 as isize;
let x1 = self.mark.0 as isize;
···
let d = max(dx.abs(), dy.abs());
let c = if dx * dy <= 0 { 1 } else { 0 };
let mut x = x0 * d + (d - c) / 2;
-
let mut y = x0 * d + (d - c) / 2;
+
let mut y = y0 * d + (d - c) / 2;
for _ in 0..d {
self.set_pixel(((x / d) as usize, (y / d) as usize));
x += dx;
···
if let Some(b) = self.bitmaps.last_mut() {
let old = *b.pixel(self.position);
let new = self.bucket.current_pixel();
+
if new == old {
+
continue;
+
}
let mut to_fill = Vec::new();
to_fill.push(self.position);
while let Some(p) = to_fill.pop() {
···
self.bitmaps.push(Bitmap::new());
}
} else if r == "PFFPCCP" {
+
// TODO: Better structure
if let Some(bmp0) = self.bitmaps.pop() {
if let Some(mut bmp1) = self.bitmaps.pop() {
for x in 0..SIZE {
for y in 0..SIZE {
-
let (r0, b0, g0, a0) = *bmp0.pixel((x, y));
-
let (r1, b1, g1, a1) = *bmp1.pixel((x, y));
+
let (r0, g0, b0, a0) = *bmp0.pixel((x, y));
+
let (r1, g1, b1, a1) = *bmp1.pixel((x, y));
let r = r0 + ((r1 as usize) * (255 - a0 as usize) / 255) as u8;
let g = g0 + ((g1 as usize) * (255 - a0 as usize) / 255) as u8;
let b = b0 + ((b1 as usize) * (255 - a0 as usize) / 255) as u8;
···
}
}
self.bitmaps.push(bmp1);
-
}
+
} else {
+
self.bitmaps.push(bmp0);
+
}
}
} else if r == "PFFICCF" {
if let Some(bmp0) = self.bitmaps.pop() {
···
for x in 0..SIZE {
for y in 0..SIZE {
let (_, _, _, a0) = *bmp0.pixel((x, y));
-
let (r1, b1, g1, a1) = *bmp1.pixel((x, y));
+
let (r1, g1, b1, a1) = *bmp1.pixel((x, y));
let r = ((r1 as usize) * (a0 as usize) / 255) as u8;
let g = ((g1 as usize) * (a0 as usize) / 255) as u8;
let b = ((b1 as usize) * (a0 as usize) / 255) as u8;
···
}
}
self.bitmaps.push(bmp1);
-
}
+
} else {
+
self.bitmaps.push(bmp0);
+
}
}
}
}