diff --git a/advent_of_code/2022/10/Cargo.lock b/advent_of_code/2022/10/Cargo.lock new file mode 100644 index 0000000..9a9e38b --- /dev/null +++ b/advent_of_code/2022/10/Cargo.lock @@ -0,0 +1,48 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "either" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "main" +version = "0.1.0" +dependencies = [ + "itertools", + "nom", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36" +dependencies = [ + "memchr", + "minimal-lexical", +] diff --git a/advent_of_code/2022/10/Cargo.toml b/advent_of_code/2022/10/Cargo.toml new file mode 100644 index 0000000..ff37989 --- /dev/null +++ b/advent_of_code/2022/10/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "main" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.10.5" +nom = "7.1.1" diff --git a/advent_of_code/2022/10/input.txt b/advent_of_code/2022/10/input.txt new file mode 100644 index 0000000..dafc4c9 --- /dev/null +++ b/advent_of_code/2022/10/input.txt @@ -0,0 +1,139 @@ +addx 2 +addx 3 +addx -2 +addx 3 +noop +addx 6 +addx -1 +addx 4 +addx 1 +noop +addx 3 +addx 1 +addx 7 +noop +noop +addx -1 +addx 3 +addx 2 +noop +addx 4 +addx 2 +addx -25 +addx -7 +addx -4 +addx 2 +addx 2 +addx 19 +addx -8 +addx -5 +addx 2 +addx -9 +addx 16 +addx 3 +addx -2 +addx 12 +addx -5 +addx 2 +addx -15 +noop +noop +noop +addx 5 +addx 16 +addx -22 +addx -14 +addx 5 +noop +addx 29 +noop +noop +noop +addx -21 +addx 2 +noop +noop +addx 5 +addx -1 +addx 1 +noop +noop +addx 8 +addx -2 +addx 4 +noop +addx -22 +addx 29 +noop +addx -36 +noop +addx -2 +addx 6 +addx -2 +addx 2 +noop +noop +noop +addx 8 +addx 2 +addx 10 +noop +addx -5 +addx 3 +addx -2 +addx 9 +addx -2 +addx 2 +addx -21 +addx 10 +addx 17 +addx -38 +noop +noop +noop +addx 34 +addx -27 +addx 2 +addx -6 +addx 7 +addx 5 +addx 2 +addx 5 +noop +noop +noop +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx -29 +addx 35 +addx -3 +addx -25 +addx -8 +addx 1 +noop +addx 4 +addx 3 +addx -2 +addx 5 +noop +addx 8 +addx -6 +noop +addx -3 +addx 10 +noop +noop +addx 6 +addx -1 +addx -18 +addx 21 +addx -30 +addx 37 +addx 1 +noop +noop +noop +noop \ No newline at end of file diff --git a/advent_of_code/2022/10/src/main.rs b/advent_of_code/2022/10/src/main.rs new file mode 100644 index 0000000..12224d0 --- /dev/null +++ b/advent_of_code/2022/10/src/main.rs @@ -0,0 +1,112 @@ +use std::collections::BTreeMap; + +use itertools::Itertools; +use nom::{ + character::complete::{self, newline}, + branch::alt, + multi::separated_list1, + bytes::complete::tag, + sequence::preceded, + *, +}; + +#[derive(Debug)] +enum Command { + NoOp, + AddX(i32), +} +use Command::*; + +impl Command{ + pub fn cycles(&self) -> u32 { + match &self { + NoOp => 1, + AddX(_) => 2, + } + } +} + +fn parse_input(input: &str) -> IResult<&str, Vec> { + let (input, vecs) = separated_list1( + newline, + alt(( + tag("noop").map(|_| NoOp), + preceded(tag("addx "), complete::i32) + .map(|num| AddX(num)) + )), + )(input)?; + + Ok((input, vecs)) +} + +fn part1(){ + let important_cycles = [20, 60, 100, 140, 180, 220]; + let mut scores: BTreeMap = BTreeMap::new(); + let mut x = 1; + let mut cycles = 0; + + let input_data = include_str!("../input.txt"); + let (_, command_lines) = parse_input(input_data).unwrap(); + + // let result = fold(command_lines.iter(), (0, 1), |(cycles, x), command| match command { + // NoOp => (cycles + 1, x), + // AddX(num) => (cycles + 2, x + num), + // }); + // println!("Result: {:?}", result); + + for command in command_lines.iter() { + if important_cycles.contains(&(cycles + 1)) { + scores.insert(cycles + 1, (cycles as i32 + 1) * x); + } + + if important_cycles.contains(&(cycles + 2)) { + scores.insert(cycles + 2, (cycles as i32 + 2) * x); + } + + cycles += command.cycles(); + match command { + NoOp => {}, + AddX(num) => { x += num; }, + }; + } + + let score = scores.iter().map(|(_key, value)| value).sum::(); + println!("Score: {:?}", score); +} + +fn part2() { + let input_data = include_str!("../input.txt"); + let (_, command_lines) = parse_input(input_data).unwrap(); + + let mut x = 1; + let mut cycles = 0; + let mut pixels = "".to_string(); + + for command in command_lines.iter() { + for cycle_add in 0..command.cycles() { + let pixel_id = (cycles as i32 + cycle_add as i32) % 40; + + if ((x-1)..=(x+1)).contains(&pixel_id) { + pixels.push('#'); + } else { + pixels.push('.'); + } + } + + cycles += command.cycles(); + match command { + NoOp => {}, + AddX(num) => { + x += num; + }, + }; + } + + let result = pixels.chars().chunks(40).into_iter().map(|chunk| chunk.collect::()).join("\n"); + print!("{}", result); +} + +fn main(){ + part1(); + part2(); +} \ No newline at end of file diff --git a/advent_of_code/2022/10/test.txt b/advent_of_code/2022/10/test.txt new file mode 100644 index 0000000..94cd0a8 --- /dev/null +++ b/advent_of_code/2022/10/test.txt @@ -0,0 +1,146 @@ +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 \ No newline at end of file