Made writing day14 to gif a feature that can be enabled

This commit is contained in:
Dreaded_X 2022-12-16 21:48:51 +01:00
parent 77f992a70a
commit 5d7a0f60ee
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9
2 changed files with 39 additions and 33 deletions

View File

@ -10,3 +10,6 @@ anyhow = "1.0"
regex = "1" regex = "1"
lazy_static = "1.4.0" lazy_static = "1.4.0"
gif = "0.12.0" gif = "0.12.0"
[features]
gif = []

View File

@ -1,10 +1,14 @@
#![feature(test)] #![feature(test)]
use core::fmt; use core::fmt;
use std::{str::FromStr, cmp::{min, max}, borrow::Cow, fs::File}; use std::{str::FromStr, cmp::{min, max}};
use anyhow::Result; use anyhow::Result;
use aoc::Solver; use aoc::Solver;
#[cfg(feature = "gif")]
use gif::{Encoder, Repeat, Frame}; use gif::{Encoder, Repeat, Frame};
#[cfg(feature = "gif")]
use std::{borrow::Cow, fs::File};
// -- Runners -- // -- Runners --
fn main() -> Result<()> { fn main() -> Result<()> {
@ -234,24 +238,7 @@ impl<'a> Cave {
} }
} }
fn print(&self) { #[cfg(feature = "gif")]
for line in self.grid.iter() {
for block in line {
let c = match block {
Block::Air => '.',
Block::Rock => '#',
Block::Sand => 'o',
Block::Source => '+',
Block::Void => 'x',
};
print!("{}", c);
}
println!("");
}
println!("");
}
fn frame(&self) -> Cow<[u8]> { fn frame(&self) -> Cow<[u8]> {
let buffer = self.grid.iter().flat_map(|line| line.iter().map(|block| match block { let buffer = self.grid.iter().flat_map(|line| line.iter().map(|block| match block {
Block::Air => 0, Block::Air => 0,
@ -269,6 +256,7 @@ impl<'a> Cave {
} }
} }
#[cfg(feature = "gif")]
fn write_frame<T: std::io::Write>(encoder: &mut Encoder<T>, cave: &Cave, delay: u16) { fn write_frame<T: std::io::Write>(encoder: &mut Encoder<T>, cave: &Cave, delay: u16) {
let mut frame = Frame::default(); let mut frame = Frame::default();
frame.width = cave.size.0 as u16; frame.width = cave.size.0 as u16;
@ -291,18 +279,26 @@ impl aoc::Solver for Day {
fn part1(input: &str) -> Self::Output1 { fn part1(input: &str) -> Self::Output1 {
let mut cave = Cave::from_str(input, false); let mut cave = Cave::from_str(input, false);
// let color_map = &[0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xea, 0xc7, 0x99, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF]; #[cfg(feature = "gif")]
// let mut image = File::create("visualize/14/part1.gif").unwrap(); let color_map = &[0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xea, 0xc7, 0x99, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF];
// let mut encoder = Encoder::new(&mut image, cave.size.0 as u16, cave.size.1 as u16, color_map).unwrap(); #[cfg(feature = "gif")]
// encoder.set_repeat(Repeat::Infinite).unwrap(); let mut image = File::create("visualize/14/part1.gif").unwrap();
#[cfg(feature = "gif")]
let mut encoder = Encoder::new(&mut image, cave.size.0 as u16, cave.size.1 as u16, color_map).unwrap();
#[cfg(feature = "gif")]
encoder.set_repeat(Repeat::Infinite).unwrap();
#[cfg(feature = "gif")]
write_frame(&mut encoder, &mut cave, 100);
// write_frame(&mut encoder, &mut cave, 100);
while cave.simulate_sand() { while cave.simulate_sand() {
// write_frame(&mut encoder, &mut cave, 1); #[cfg(feature = "gif")]
write_frame(&mut encoder, &mut cave, 1);
} }
// write_frame(&mut encoder, &mut cave, 1000); #[cfg(feature = "gif")]
write_frame(&mut encoder, &mut cave, 1000);
cave.count_sand() cave.count_sand()
} }
@ -310,18 +306,25 @@ impl aoc::Solver for Day {
fn part2(input: &str) -> Self::Output2 { fn part2(input: &str) -> Self::Output2 {
let mut cave = Cave::from_str(input, true); let mut cave = Cave::from_str(input, true);
// let color_map = &[0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xea, 0xc7, 0x99, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF]; #[cfg(feature = "gif")]
// let mut image = File::create("visualize/14/part2.gif").unwrap(); let color_map = &[0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xea, 0xc7, 0x99, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF];
// let mut encoder = Encoder::new(&mut image, cave.size.0 as u16, cave.size.1 as u16, color_map).unwrap(); #[cfg(feature = "gif")]
// encoder.set_repeat(Repeat::Infinite).unwrap(); let mut image = File::create("visualize/14/part2.gif").unwrap();
#[cfg(feature = "gif")]
let mut encoder = Encoder::new(&mut image, cave.size.0 as u16, cave.size.1 as u16, color_map).unwrap();
#[cfg(feature = "gif")]
encoder.set_repeat(Repeat::Infinite).unwrap();
// write_frame(&mut encoder, &mut cave, 100); #[cfg(feature = "gif")]
write_frame(&mut encoder, &mut cave, 100);
while cave.simulate_sand() { while cave.simulate_sand() {
// write_frame(&mut encoder, &mut cave, 1); #[cfg(feature = "gif")]
write_frame(&mut encoder, &mut cave, 1);
} }
// write_frame(&mut encoder, &mut cave, 1000); #[cfg(feature = "gif")]
write_frame(&mut encoder, &mut cave, 1000);
cave.count_sand() cave.count_sand()
} }