This commit is contained in:
2024-12-12 14:22:17 +01:00
parent 2299375c7c
commit 548f1b3d73
6 changed files with 293 additions and 0 deletions

27
y2016/src/bin/d8.rs Normal file
View File

@@ -0,0 +1,27 @@
use std::{fs, time::Instant};
use utils::time::get_elapsed_string;
use y2016::days::d8;
fn main() {
let now = Instant::now();
println!("Part 1:");
part1();
println!("Ran in {}", get_elapsed_string(now.elapsed()));
let now = Instant::now();
println!("Part 2:");
part2();
println!("Ran in {}", get_elapsed_string(now.elapsed()));
}
fn part1() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/8_input.txt")).unwrap();
println!("{}", d8::process_part1(&content, (50, 6)));
}
fn part2() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/8_input.txt")).unwrap();
println!("{}", d8::process_part2(&content));
}

100
y2016/src/days/d8.rs Normal file
View File

@@ -0,0 +1,100 @@
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::<i32>()
})
.sum()
}
pub fn process_part2(input: &str) -> i32 {
0
}
fn parse_instruction(inst: &str) -> Option<Instruction> {
if inst.contains("rect") {
let (_, axb) = inst.split_once(" ").unwrap();
let (a, b) = axb.split_once("x").unwrap();
return Some(Instruction::Rect(
a.parse::<u32>().unwrap() - 1,
b.parse::<u32>().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);
}
}

View File

@@ -11,3 +11,5 @@ pub mod d5;
pub mod d6;
pub mod d7;
pub mod d8;