Add day 8

This commit is contained in:
Kienan Stewart 2022-05-15 10:39:26 -04:00
parent 571c0c2511
commit 065d2d8192
4 changed files with 138 additions and 0 deletions

7
day8/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day8"
version = "0.1.0"

8
day8/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day8"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1
day8/input Normal file

File diff suppressed because one or more lines are too long

122
day8/src/main.rs Normal file
View File

@ -0,0 +1,122 @@
struct Image {
width: usize,
height: usize,
layers: Vec<Vec<u32>>,
}
impl Image {
fn from_str(width: usize, height: usize, data: &str) -> Image {
assert_eq!(data.len() % (width * height), 0);
let mut image = Image {
width: width,
height: height,
layers: Vec::new(),
};
let mut layer: usize = 0;
image.layers.push(Vec::new());
let mut index: usize = 0;
for c in data.chars() {
if index >= (width*height) {
layer += 1;
image.layers.push(Vec::new());
index = 0;
}
image.layers[layer].push(c.to_digit(10).expect("Could not convert character to digit"));
index += 1;
}
return image;
}
fn layer_n_digits(&self, layer_index: usize, value: u32) -> usize {
let mut count: usize = 0;
for v in self.layers[layer_index].iter() {
if *v == value {
count += 1;
}
}
return count;
}
fn layer_merge(&self) -> Vec<u32> {
let mut result: Vec<u32> = Vec::new();
result.resize(self.width * self.height, 2);
for pixel_index in 0..(self.width*self.height) {
for layer_index in 0..self.layers.len() {
// 2 is transparent
let value = self.layers[layer_index][pixel_index];
if value != 2 {
result[pixel_index] = value;
break;
}
}
}
return result;
}
}
fn main() {
let data = std::fs::read_to_string("input")
.expect("Failed to read 'input'");
let image = Image::from_str(25, 6, &data);
println!("Image contains {} layers", image.layers.len());
let mut layer_fewest_zeroes: usize = 0;
let mut least_zeroes: usize = 200;
for index in 0..image.layers.len() {
let n_zeroes = image.layer_n_digits(index, 0);
if n_zeroes < least_zeroes {
layer_fewest_zeroes = index;
least_zeroes = n_zeroes;
}
}
println!("Layer {} has {} zeroes", layer_fewest_zeroes, least_zeroes);
println!("[Part 1] {}", image.layer_n_digits(layer_fewest_zeroes, 1) * image.layer_n_digits(layer_fewest_zeroes, 2));
let merged = image.layer_merge();
let mut n: u32 = 0;
for i in merged.iter() {
let c = if *i == 0 {
'⬛'
} else if *i == 1 {
'⬜'
}
else if *i == 2 {
' '
}
else {
'?'
};
print!("{}", c);
n += 1;
if n % 25 == 0 {
print!("\n");
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn create_image() {
let data = "123456789012";
let image = Image::from_str(3, 2, data);
assert_eq!(image.layers.len(), 2);
let layer_0 = vec![1, 2, 3, 4, 5, 6];
let layer_1 = vec![7, 8, 9, 0, 1, 2];
assert_eq!(image.layers[0], layer_0);
assert_eq!(image.layers[1], layer_1);
}
#[test]
fn layer_merge() {
let data = "0222112222120000";
let image = Image::from_str(2, 2, data);
let merged = image.layer_merge();
assert_eq!(merged[0], 0);
assert_eq!(merged[1], 1);
assert_eq!(merged[2], 1);
assert_eq!(merged[3], 0);
}
}