use itertools::Itertools; enum Instruction { Rect(u32, u32), ShiftRight(u32, u32), ShiftDown(u32, u32), } pub fn process_part1(input: &str, grid_size: (usize, usize)) -> i32 { let instructions = input .lines() .map(parse_instruction) .map(|option| option.unwrap()) .collect_vec(); let mut grid = vec![vec!['.'; grid_size.0]; grid_size.1]; for instrution in instructions { match instrution { Instruction::Rect(width, height) => { for row in 0..=height { for col in 0..=width { grid[row as usize][col as usize] = '#'; } } } Instruction::ShiftRight(row, by) => grid[row as usize].rotate_right(by as usize), Instruction::ShiftDown(col, by) => { let mut col_items = grid.iter().map(|row| row[col as usize]).collect_vec(); col_items.rotate_right(by as usize); for (row, pixel) in col_items.iter().enumerate() { grid[row][col as usize] = *pixel; } } } for row in grid.clone() { println!("{}", row.iter().join("")); } println!(); } grid.iter() .map(|row| { row.iter() .map(|pixel| if *pixel == '#' { 1 } else { 0 }) .sum::() }) .sum() } pub fn process_part2(_input: &str) -> i32 { 0 } fn parse_instruction(inst: &str) -> Option { if inst.contains("rect") { let (_, axb) = inst.split_once(" ").unwrap(); let (a, b) = axb.split_once("x").unwrap(); return Some(Instruction::Rect( a.parse::().unwrap() - 1, b.parse::().unwrap() - 1, )); } if inst.contains("rotate row y") { let (_, abyb) = inst.split_once("=").unwrap(); let (a, b) = abyb.split_once(" by ").unwrap(); return Some(Instruction::ShiftRight( a.parse().unwrap(), b.parse().unwrap(), )); } if inst.contains("rotate column x") { let (_, abyb) = inst.split_once("=").unwrap(); let (a, b) = abyb.split_once(" by ").unwrap(); return Some(Instruction::ShiftDown( a.parse().unwrap(), b.parse().unwrap(), )); } None } #[cfg(test)] mod tests { use super::*; const INPUT: &str = "rect 3x2 rotate column x=1 by 1 rotate row y=0 by 4 rotate column x=1 by 1"; #[test] fn part1() { let result = process_part1(INPUT, (7, 3)); assert_eq!(result, 6); } #[test] fn part2() { let result = process_part2(INPUT); assert_eq!(result, 0); } }