diff --git a/day08/Cargo.lock b/day08/Cargo.lock new file mode 100644 index 0000000..3b72fd5 --- /dev/null +++ b/day08/Cargo.lock @@ -0,0 +1,25 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day08" +version = "0.1.0" +dependencies = [ + "itertools", +] + +[[package]] +name = "either" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e" + +[[package]] +name = "itertools" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b4baf93f58d4425749ca49a51c50ebab072c5df6994d08fed93541c331481dc" +dependencies = [ + "either", +] diff --git a/day08/Cargo.toml b/day08/Cargo.toml new file mode 100644 index 0000000..974b040 --- /dev/null +++ b/day08/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day08" +version = "0.1.0" +edition = "2024" + +[dependencies] +itertools = "*" diff --git a/day08/example.txt b/day08/example.txt new file mode 100644 index 0000000..a9df2bc --- /dev/null +++ b/day08/example.txt @@ -0,0 +1,4 @@ +rect 3x2 +rotate column x=1 by 1 +rotate row y=0 by 4 +rotate column x=1 by 1 diff --git a/day08/input.txt b/day08/input.txt new file mode 100644 index 0000000..d827762 --- /dev/null +++ b/day08/input.txt @@ -0,0 +1,194 @@ +rect 1x1 +rotate row y=0 by 7 +rect 1x1 +rotate row y=0 by 5 +rect 1x1 +rotate row y=0 by 5 +rect 1x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 3 +rect 1x1 +rotate row y=0 by 5 +rect 1x1 +rotate row y=0 by 3 +rect 1x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 3 +rect 2x1 +rotate row y=0 by 7 +rect 6x1 +rotate row y=0 by 3 +rect 2x1 +rotate row y=0 by 2 +rect 1x2 +rotate row y=1 by 10 +rotate row y=0 by 3 +rotate column x=0 by 1 +rect 2x1 +rotate column x=20 by 1 +rotate column x=15 by 1 +rotate column x=5 by 1 +rotate row y=1 by 5 +rotate row y=0 by 2 +rect 1x2 +rotate row y=0 by 5 +rotate column x=0 by 1 +rect 4x1 +rotate row y=2 by 15 +rotate row y=0 by 5 +rotate column x=0 by 1 +rect 4x1 +rotate row y=2 by 5 +rotate row y=0 by 5 +rotate column x=0 by 1 +rect 4x1 +rotate row y=2 by 10 +rotate row y=0 by 10 +rotate column x=8 by 1 +rotate column x=5 by 1 +rotate column x=0 by 1 +rect 9x1 +rotate column x=27 by 1 +rotate row y=0 by 5 +rotate column x=0 by 1 +rect 4x1 +rotate column x=42 by 1 +rotate column x=40 by 1 +rotate column x=22 by 1 +rotate column x=17 by 1 +rotate column x=12 by 1 +rotate column x=7 by 1 +rotate column x=2 by 1 +rotate row y=3 by 10 +rotate row y=2 by 5 +rotate row y=1 by 3 +rotate row y=0 by 10 +rect 1x4 +rotate column x=37 by 2 +rotate row y=3 by 18 +rotate row y=2 by 30 +rotate row y=1 by 7 +rotate row y=0 by 2 +rotate column x=13 by 3 +rotate column x=12 by 1 +rotate column x=10 by 1 +rotate column x=7 by 1 +rotate column x=6 by 3 +rotate column x=5 by 1 +rotate column x=3 by 3 +rotate column x=2 by 1 +rotate column x=0 by 1 +rect 14x1 +rotate column x=38 by 3 +rotate row y=3 by 12 +rotate row y=2 by 10 +rotate row y=0 by 10 +rotate column x=7 by 1 +rotate column x=5 by 1 +rotate column x=2 by 1 +rotate column x=0 by 1 +rect 9x1 +rotate row y=4 by 20 +rotate row y=3 by 25 +rotate row y=2 by 10 +rotate row y=0 by 15 +rotate column x=12 by 1 +rotate column x=10 by 1 +rotate column x=8 by 3 +rotate column x=7 by 1 +rotate column x=5 by 1 +rotate column x=3 by 3 +rotate column x=2 by 1 +rotate column x=0 by 1 +rect 14x1 +rotate column x=34 by 1 +rotate row y=1 by 45 +rotate column x=47 by 1 +rotate column x=42 by 1 +rotate column x=19 by 1 +rotate column x=9 by 2 +rotate row y=4 by 7 +rotate row y=3 by 20 +rotate row y=0 by 7 +rotate column x=5 by 1 +rotate column x=3 by 1 +rotate column x=2 by 1 +rotate column x=0 by 1 +rect 6x1 +rotate row y=4 by 8 +rotate row y=3 by 5 +rotate row y=1 by 5 +rotate column x=5 by 1 +rotate column x=4 by 1 +rotate column x=3 by 2 +rotate column x=2 by 1 +rotate column x=1 by 3 +rotate column x=0 by 1 +rect 6x1 +rotate column x=36 by 3 +rotate column x=25 by 3 +rotate column x=18 by 3 +rotate column x=11 by 3 +rotate column x=3 by 4 +rotate row y=4 by 5 +rotate row y=3 by 5 +rotate row y=2 by 8 +rotate row y=1 by 8 +rotate row y=0 by 3 +rotate column x=3 by 4 +rotate column x=0 by 4 +rect 4x4 +rotate row y=4 by 10 +rotate row y=3 by 20 +rotate row y=1 by 10 +rotate row y=0 by 10 +rotate column x=8 by 1 +rotate column x=7 by 1 +rotate column x=6 by 1 +rotate column x=5 by 1 +rotate column x=3 by 1 +rotate column x=2 by 1 +rotate column x=1 by 1 +rotate column x=0 by 1 +rect 9x1 +rotate row y=0 by 40 +rotate column x=44 by 1 +rotate column x=35 by 5 +rotate column x=18 by 5 +rotate column x=15 by 3 +rotate column x=10 by 5 +rotate row y=5 by 15 +rotate row y=4 by 10 +rotate row y=3 by 40 +rotate row y=2 by 20 +rotate row y=1 by 45 +rotate row y=0 by 35 +rotate column x=48 by 1 +rotate column x=47 by 5 +rotate column x=46 by 5 +rotate column x=45 by 1 +rotate column x=43 by 1 +rotate column x=40 by 1 +rotate column x=38 by 2 +rotate column x=37 by 3 +rotate column x=36 by 2 +rotate column x=32 by 2 +rotate column x=31 by 2 +rotate column x=28 by 1 +rotate column x=23 by 3 +rotate column x=22 by 3 +rotate column x=21 by 5 +rotate column x=20 by 1 +rotate column x=18 by 1 +rotate column x=17 by 3 +rotate column x=13 by 1 +rotate column x=10 by 1 +rotate column x=8 by 1 +rotate column x=7 by 5 +rotate column x=6 by 5 +rotate column x=5 by 1 +rotate column x=3 by 5 +rotate column x=2 by 5 +rotate column x=1 by 5 diff --git a/day08/src/main.rs b/day08/src/main.rs new file mode 100644 index 0000000..f306105 --- /dev/null +++ b/day08/src/main.rs @@ -0,0 +1,107 @@ +#[cfg(debug_assertions)] +const SCREEN_X: usize = 7; +#[cfg(debug_assertions)] +const SCREEN_Y: usize = 3; +#[cfg(debug_assertions)] +const FILENAME: &str = "example.txt"; + +#[cfg(not(debug_assertions))] +const SCREEN_X: usize = 50; +#[cfg(not(debug_assertions))] +const SCREEN_Y: usize = 6; +#[cfg(not(debug_assertions))] +const FILENAME: &str = "input.txt"; + +fn main() { + let mut grid = vec![vec!['.'; SCREEN_Y]; SCREEN_X]; + + let instructions: Vec<_> = std::fs::read_to_string(FILENAME) + .unwrap() + .lines() + .map(Instruction::parse) + .collect(); + + part1(&mut grid, instructions); +} + +#[allow(clippy::needless_range_loop, clippy::ptr_arg)] +fn part1(grid: &mut Vec>, instructions: Vec) { + for instruction in &instructions { + match instruction { + Instruction::Rect(x, y) => { + for ix in 0..*x { + for iy in 0..*y { + grid[ix][iy] = '#'; + } + } + } + Instruction::RotateX(x, y) => { + grid.get_mut(*x).unwrap().rotate_right(*y); + } + Instruction::RotateY(y, x) => { + let mut the_vec = vec![]; + for ix in 0..SCREEN_X { + the_vec.push(grid[ix][*y]); + } + the_vec.rotate_right(*x); + for ix in 0..SCREEN_X { + grid[ix][*y] = the_vec[ix]; + } + } + } + } + + let result = grid.iter().flatten().filter(|e| **e == '#').count(); + println!("{result}"); // 128 + + for iy in 0..SCREEN_Y { + for ix in 0..SCREEN_X { + print!("{}", grid[ix][iy]); + } + println!(); // EOARGPHYAO + } +} + +#[derive(Debug)] +enum Instruction { + Rect(usize, usize), + RotateX(usize, usize), // x=2 by 5 => RotateX(2, 5) + RotateY(usize, usize), // y=0 by 3 => RotateY(0, 3) +} + +impl Instruction { + fn parse(s: &str) -> Self { + let mut words = s.split_whitespace(); + + let first = words.next().unwrap(); + + if first == "rect" { + let tuple = parse_grid(words.next().unwrap()); + Self::Rect(tuple.0, tuple.1) + } else { + let second = words.next().unwrap(); + let tuple = parse_rotate(words); + if second == "row" { + Self::RotateY(tuple.0, tuple.1) + } else { + Self::RotateX(tuple.0, tuple.1) + } + } + } +} + +fn parse_rotate<'a>(mut words: impl DoubleEndedIterator) -> (usize, usize) { + let equals = words.next().unwrap(); + let equals_value = equals.split('=').next_back().unwrap().parse().unwrap(); + + let shift = words.next_back().unwrap().parse().unwrap(); + + (equals_value, shift) +} + +fn parse_grid(s: &str) -> (usize, usize) { + let mut iter = s.split('x'); + let x = iter.next().unwrap().to_string().parse().unwrap(); + let y = iter.next().unwrap().to_string().parse().unwrap(); + (x, y) +}