From 9ca7f37c1e136fad7be94f6a56e34fcd4fc0c1be Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Mon, 12 Dec 2022 14:38:01 -0600 Subject: feat: impl day 10 --- Cargo.lock | 101 ++++++++++++++++++++++++++ Cargo.toml | 5 +- aoc_2022/Cargo.toml | 9 ++- aoc_2022/input/day_10.txt | 139 +++++++++++++++++++++++++++++++++++ aoc_2022/input/day_10_example.txt | 147 ++++++++++++++++++++++++++++++++++++++ aoc_2022/src/day_10.rs | 132 ++++++++++++++++++++++++++++++++++ aoc_2022/src/lib.rs | 1 + aoc_2022/src/main.rs | 5 +- 8 files changed, 532 insertions(+), 7 deletions(-) create mode 100644 aoc_2022/input/day_10.txt create mode 100644 aoc_2022/input/day_10_example.txt create mode 100644 aoc_2022/src/day_10.rs diff --git a/Cargo.lock b/Cargo.lock index 10d37c4..a1e90fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,10 +23,111 @@ dependencies = [ "anyhow", "aoc", "indoc", + "pretty_assertions", ] +[[package]] +name = "ctor" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "indoc" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adab1eaa3408fb7f0c777a73e7465fd5656136fc93b670eb6df3c88c2c1344e3" + +[[package]] +name = "output_vt100" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628223faebab4e3e40667ee0b2336d34a5b960ff60ea743ddfdbcf7770bcfb66" +dependencies = [ + "winapi", +] + +[[package]] +name = "pretty_assertions" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a25e9bcb20aa780fd0bb16b72403a9064d6b3f22f026946029acb941a50af755" +dependencies = [ + "ctor", + "diff", + "output_vt100", + "yansi", +] + +[[package]] +name = "proc-macro2" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "1.0.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[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-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" diff --git a/Cargo.toml b/Cargo.toml index 13d3f5a..2ed9eac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,8 @@ edition = "2021" description = "Library used for Advent of Code solution crates" [dependencies] -anyhow = { workspace = true } -indoc = { workspace = true } +anyhow.workspace = true +indoc.workspace = true [workspace] members = ["aoc_2022"] @@ -16,3 +16,4 @@ default-members = ["aoc_2022"] aoc = { path = "." } anyhow = "1.0.66" indoc = "1.0.7" +pretty_assertions = "1.3.0" diff --git a/aoc_2022/Cargo.toml b/aoc_2022/Cargo.toml index 4bc060e..6edfccb 100644 --- a/aoc_2022/Cargo.toml +++ b/aoc_2022/Cargo.toml @@ -6,6 +6,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aoc = { workspace = true } -anyhow = { workspace = true } -indoc = { workspace = true } +aoc.workspace = true +anyhow.workspace = true +indoc.workspace = true + +[dev-dependencies] +pretty_assertions.workspace = true diff --git a/aoc_2022/input/day_10.txt b/aoc_2022/input/day_10.txt new file mode 100644 index 0000000..c26a664 --- /dev/null +++ b/aoc_2022/input/day_10.txt @@ -0,0 +1,139 @@ +noop +addx 12 +addx -5 +addx -1 +noop +addx 4 +noop +addx 1 +addx 4 +noop +addx 13 +addx -8 +noop +addx -19 +addx 24 +addx 1 +noop +addx 4 +noop +addx 1 +addx 5 +addx -1 +addx -37 +addx 16 +addx -13 +addx 18 +addx -11 +addx 2 +addx 23 +noop +addx -18 +addx 9 +addx -8 +addx 2 +addx 5 +addx 2 +addx -21 +addx 26 +noop +addx -15 +addx 20 +noop +addx 3 +noop +addx -38 +addx 3 +noop +addx 26 +addx -4 +addx -19 +addx 3 +addx 1 +addx 5 +addx 3 +noop +addx 2 +addx 3 +noop +addx 2 +noop +noop +noop +noop +addx 5 +noop +noop +noop +addx 3 +noop +addx -30 +addx -4 +addx 1 +addx 18 +addx -8 +addx -4 +addx 2 +noop +addx 7 +noop +noop +noop +noop +addx 5 +noop +noop +addx 5 +addx -2 +addx -20 +addx 27 +addx -20 +addx 25 +addx -2 +addx -35 +noop +noop +addx 4 +addx 3 +addx -2 +addx 5 +addx 2 +addx -11 +addx 1 +addx 13 +addx 2 +addx 5 +addx 6 +addx -1 +addx -2 +noop +addx 7 +addx -2 +addx 6 +addx 1 +addx -21 +addx 22 +addx -38 +addx 5 +addx 3 +addx -1 +noop +noop +addx 5 +addx 1 +addx 4 +addx 3 +addx -2 +addx 2 +noop +addx 7 +addx -1 +addx 2 +addx 4 +addx -10 +addx -19 +addx 35 +addx -1 +noop +noop +noop diff --git a/aoc_2022/input/day_10_example.txt b/aoc_2022/input/day_10_example.txt new file mode 100644 index 0000000..2bb94df --- /dev/null +++ b/aoc_2022/input/day_10_example.txt @@ -0,0 +1,147 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop + diff --git a/aoc_2022/src/day_10.rs b/aoc_2022/src/day_10.rs new file mode 100644 index 0000000..9c5179d --- /dev/null +++ b/aoc_2022/src/day_10.rs @@ -0,0 +1,132 @@ +use std::{fmt::Display, str::FromStr}; + +use aoc::{Problem, Solution}; + +enum Instruction { + Addx(isize), + Noop, +} + +impl From for usize { + fn from(value: Instruction) -> Self { + match value { + Instruction::Addx(_) => 2, + Instruction::Noop => 1, + } + } +} + +impl Display for Instruction { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Instruction::Addx(x) => write!(f, "addx {}", x), + Instruction::Noop => write!(f, "noop"), + } + } +} + +impl FromStr for Instruction { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + match s { + "noop" => Ok(Self::Noop), + s if s.contains("addx") => s + .split_once(' ') + .ok_or_else(|| anyhow::anyhow!("Incorrect format for addx")) + .and_then(|(_, n)| n.parse().map_err(Into::into)) + .map(Self::Addx), + s => Err(anyhow::anyhow!("Unknown instruction: {s}")), + } + } +} + +fn generate_states(input: &str) -> Vec { + let mut register = 1; + input + .lines() + .flat_map(Instruction::from_str) + .flat_map(move |i| { + let mut states = vec![register]; + if let Instruction::Addx(x) = i { + states.push(register); + register += x; + } + states + }) + .collect() +} + +pub struct Day10; + +impl Problem for Day10 { + const DAY: u8 = 10; + + const INPUT: &'static str = include_str!("../input/day_10.txt"); +} + +impl Solution for Day10 { + type Answer1 = isize; + + type Answer2 = String; + + fn part_1(input: &str) -> Result { + let signal_strength = generate_states(input) + .iter() + .enumerate() + .skip(19) + .step_by(40) + .map(|(cycle, register)| (cycle as isize + 1) * register) + .sum(); + + Ok(signal_strength) + } + + fn part_2(input: &str) -> Result { + let output = generate_states(input) + .chunks(40) + .map(|chunk| { + chunk.iter().enumerate().map(|(draw, sprite)| { + if (sprite - 1..=sprite + 1).contains(&(draw as isize)) { + '#' + } else { + '.' + } + }) + }) + .fold(String::new(), |mut acc, line| { + acc.extend(line); + acc.push('\n'); + acc + }); + + Ok(output) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use pretty_assertions::assert_eq; + + const TEST_INPUT: &str = include_str!("../input/day_10_example.txt"); + + #[test] + fn test_part_1_example() -> Result<(), anyhow::Error> { + Ok(assert_eq!(13140, Day10::part_1(TEST_INPUT)?)) + } + + #[test] + fn test_part_2_example() -> Result<(), anyhow::Error> { + let test_output = indoc::indoc! {r#" + ##..##..##..##..##..##..##..##..##..##.. + ###...###...###...###...###...###...###. + ####....####....####....####....####.... + #####.....#####.....#####.....#####..... + ######......######......######......#### + #######.......#######.......#######..... + "#}; + + Ok(assert_eq!(test_output, Day10::part_2(TEST_INPUT)?)) + } +} diff --git a/aoc_2022/src/lib.rs b/aoc_2022/src/lib.rs index 09de695..2483772 100644 --- a/aoc_2022/src/lib.rs +++ b/aoc_2022/src/lib.rs @@ -11,3 +11,4 @@ pub mod day_6; pub mod day_7; pub mod day_8; pub mod day_9; +pub mod day_10; diff --git a/aoc_2022/src/main.rs b/aoc_2022/src/main.rs index f791a30..1dd90df 100644 --- a/aoc_2022/src/main.rs +++ b/aoc_2022/src/main.rs @@ -1,8 +1,8 @@ use anyhow::Result; use aoc::Solution; use aoc_2022::{ - day_1::Day1, day_2::Day2, day_3::Day3, day_4::Day4, day_5::Day5, day_6::Day6, day_7::Day7, - day_8::Day8, day_9::Day9, + day_1::Day1, day_10::Day10, day_2::Day2, day_3::Day3, day_4::Day4, day_5::Day5, day_6::Day6, + day_7::Day7, day_8::Day8, day_9::Day9, }; fn main() -> Result<()> { @@ -15,6 +15,7 @@ fn main() -> Result<()> { Day7::solve()?; Day8::solve()?; Day9::solve()?; + Day10::solve()?; Ok(()) } -- cgit v1.2.3-70-g09d2