diff --git a/2022/Cargo.lock b/2022/Cargo.lock index 311f35a..98baac9 100644 --- a/2022/Cargo.lock +++ b/2022/Cargo.lock @@ -26,6 +26,7 @@ dependencies = [ "path-absolutize", "regex", "serde", + "termcolor", ] [[package]] @@ -139,8 +140,48 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "unicode-ident" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/2022/Cargo.toml b/2022/Cargo.toml index e5abf91..ca42234 100644 --- a/2022/Cargo.toml +++ b/2022/Cargo.toml @@ -9,3 +9,4 @@ itertools = "0.10.5" path-absolutize = "3.0.14" regex = "1.7.0" serde = { version = "1.0.149", features = ["derive"] } +termcolor = "1.1.3" diff --git a/2022/src/bin/day12.rs b/2022/src/bin/day12.rs index 2dd4f32..74a81e3 100644 --- a/2022/src/bin/day12.rs +++ b/2022/src/bin/day12.rs @@ -1,10 +1,17 @@ use std::collections::{HashMap, HashSet, VecDeque}; +use std::io::Write; + +use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor}; const INPUT: &str = include_str!("../../input/day12.txt"); fn main() { let (hill, start, goal) = Hill::parse(INPUT); + palette().unwrap(); + println!(); + render(INPUT).unwrap(); + println!("Part 1: {}", part1(&hill, start, goal)); println!("Part 2: {}", part2(&hill, goal)); } @@ -121,3 +128,54 @@ impl Hill { a } } + +fn render(text: &str) -> anyhow::Result<()> { + let writer = BufferWriter::stderr(ColorChoice::Always); + let mut buf = writer.buffer(); + + for line in aoc::lines(text) { + for c in line.chars() { + buf.set_color(&color(c))?; + write!(&mut buf, "█")?; + } + writeln!(&mut buf)?; + } + + buf.set_color(ColorSpec::new().set_reset(true))?; + writer.print(&buf)?; + Ok(()) +} + +fn color(c: char) -> ColorSpec { + let mut color = ColorSpec::new(); + let spec = match c { + 'S' => Color::Blue, + 'E' => Color::Green, + c => { + let elev = (c as u64) - ('a' as u64); + let gray = (elev * 256 / 26) as u8; + Color::Rgb(gray, gray, gray) + } + }; + color.set_fg(Some(spec)); + color +} + +fn palette() -> anyhow::Result<()> { + let writer = BufferWriter::stderr(ColorChoice::Always); + let mut buf = writer.buffer(); + + let alphabet = "abcdefghijklmnopqrstuvwxyz"; + + writeln!(&mut buf, "{}", alphabet)?; + + for c in alphabet.chars() { + buf.set_color(&color(c))?; + write!(&mut buf, "█")?; + } + writeln!(&mut buf)?; + + buf.set_color(ColorSpec::new().set_reset(true))?; + writer.print(&buf)?; + Ok(()) +}