y2016d8
This commit is contained in:
parent
2299375c7c
commit
548f1b3d73
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -88,6 +88,7 @@ dependencies = [
|
|||||||
name = "y2016"
|
name = "y2016"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"itertools",
|
||||||
"md5",
|
"md5",
|
||||||
"utils",
|
"utils",
|
||||||
]
|
]
|
||||||
|
@ -6,3 +6,4 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
md5 = { workspace = true }
|
md5 = { workspace = true }
|
||||||
utils = { workspace = true }
|
utils = { workspace = true }
|
||||||
|
itertools = { workspace = true }
|
||||||
|
162
y2016/resources/8_input.txt
Normal file
162
y2016/resources/8_input.txt
Normal file
@ -0,0 +1,162 @@
|
|||||||
|
rect 1x1
|
||||||
|
rotate row y=0 by 6
|
||||||
|
rect 1x1
|
||||||
|
rotate row y=0 by 3
|
||||||
|
rect 1x1
|
||||||
|
rotate row y=0 by 5
|
||||||
|
rect 1x1
|
||||||
|
rotate row y=0 by 4
|
||||||
|
rect 2x1
|
||||||
|
rotate row y=0 by 5
|
||||||
|
rect 2x1
|
||||||
|
rotate row y=0 by 2
|
||||||
|
rect 1x1
|
||||||
|
rotate row y=0 by 5
|
||||||
|
rect 4x1
|
||||||
|
rotate row y=0 by 2
|
||||||
|
rect 1x1
|
||||||
|
rotate row y=0 by 3
|
||||||
|
rect 1x1
|
||||||
|
rotate row y=0 by 3
|
||||||
|
rect 1x1
|
||||||
|
rotate row y=0 by 2
|
||||||
|
rect 1x1
|
||||||
|
rotate row y=0 by 6
|
||||||
|
rect 4x1
|
||||||
|
rotate row y=0 by 4
|
||||||
|
rotate column x=0 by 1
|
||||||
|
rect 3x1
|
||||||
|
rotate row y=0 by 6
|
||||||
|
rotate column x=0 by 1
|
||||||
|
rect 4x1
|
||||||
|
rotate column x=10 by 1
|
||||||
|
rotate row y=2 by 16
|
||||||
|
rotate row y=0 by 8
|
||||||
|
rotate column x=5 by 1
|
||||||
|
rotate column x=0 by 1
|
||||||
|
rect 7x1
|
||||||
|
rotate column x=37 by 1
|
||||||
|
rotate column x=21 by 2
|
||||||
|
rotate column x=15 by 1
|
||||||
|
rotate column x=11 by 2
|
||||||
|
rotate row y=2 by 39
|
||||||
|
rotate row y=0 by 36
|
||||||
|
rotate column x=33 by 2
|
||||||
|
rotate column x=32 by 1
|
||||||
|
rotate column x=28 by 2
|
||||||
|
rotate column x=27 by 1
|
||||||
|
rotate column x=25 by 1
|
||||||
|
rotate column x=22 by 1
|
||||||
|
rotate column x=21 by 2
|
||||||
|
rotate column x=20 by 3
|
||||||
|
rotate column x=18 by 1
|
||||||
|
rotate column x=15 by 2
|
||||||
|
rotate column x=12 by 1
|
||||||
|
rotate column x=10 by 1
|
||||||
|
rotate column x=6 by 2
|
||||||
|
rotate column x=5 by 1
|
||||||
|
rotate column x=2 by 1
|
||||||
|
rotate column x=0 by 1
|
||||||
|
rect 35x1
|
||||||
|
rotate column x=45 by 1
|
||||||
|
rotate row y=1 by 28
|
||||||
|
rotate column x=38 by 2
|
||||||
|
rotate column x=33 by 1
|
||||||
|
rotate column x=28 by 1
|
||||||
|
rotate column x=23 by 1
|
||||||
|
rotate column x=18 by 1
|
||||||
|
rotate column x=13 by 2
|
||||||
|
rotate column x=8 by 1
|
||||||
|
rotate column x=3 by 1
|
||||||
|
rotate row y=3 by 2
|
||||||
|
rotate row y=2 by 2
|
||||||
|
rotate row y=1 by 5
|
||||||
|
rotate row y=0 by 1
|
||||||
|
rect 1x5
|
||||||
|
rotate column x=43 by 1
|
||||||
|
rotate column x=31 by 1
|
||||||
|
rotate row y=4 by 35
|
||||||
|
rotate row y=3 by 20
|
||||||
|
rotate row y=1 by 27
|
||||||
|
rotate row y=0 by 20
|
||||||
|
rotate column x=17 by 1
|
||||||
|
rotate column x=15 by 1
|
||||||
|
rotate column x=12 by 1
|
||||||
|
rotate column x=11 by 2
|
||||||
|
rotate column x=10 by 1
|
||||||
|
rotate column x=8 by 1
|
||||||
|
rotate column x=7 by 1
|
||||||
|
rotate column x=5 by 1
|
||||||
|
rotate column x=3 by 2
|
||||||
|
rotate column x=2 by 1
|
||||||
|
rotate column x=0 by 1
|
||||||
|
rect 19x1
|
||||||
|
rotate column x=20 by 3
|
||||||
|
rotate column x=14 by 1
|
||||||
|
rotate column x=9 by 1
|
||||||
|
rotate row y=4 by 15
|
||||||
|
rotate row y=3 by 13
|
||||||
|
rotate row y=2 by 15
|
||||||
|
rotate row y=1 by 18
|
||||||
|
rotate row y=0 by 15
|
||||||
|
rotate column x=13 by 1
|
||||||
|
rotate column x=12 by 1
|
||||||
|
rotate column x=11 by 3
|
||||||
|
rotate column x=10 by 1
|
||||||
|
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 2
|
||||||
|
rotate column x=2 by 1
|
||||||
|
rotate column x=1 by 1
|
||||||
|
rotate column x=0 by 1
|
||||||
|
rect 14x1
|
||||||
|
rotate row y=3 by 47
|
||||||
|
rotate column x=19 by 3
|
||||||
|
rotate column x=9 by 3
|
||||||
|
rotate column x=4 by 3
|
||||||
|
rotate row y=5 by 5
|
||||||
|
rotate row y=4 by 5
|
||||||
|
rotate row y=3 by 8
|
||||||
|
rotate row y=1 by 5
|
||||||
|
rotate column x=3 by 2
|
||||||
|
rotate column x=2 by 3
|
||||||
|
rotate column x=1 by 2
|
||||||
|
rotate column x=0 by 2
|
||||||
|
rect 4x2
|
||||||
|
rotate column x=35 by 5
|
||||||
|
rotate column x=20 by 3
|
||||||
|
rotate column x=10 by 5
|
||||||
|
rotate column x=3 by 2
|
||||||
|
rotate row y=5 by 20
|
||||||
|
rotate row y=3 by 30
|
||||||
|
rotate row y=2 by 45
|
||||||
|
rotate row y=1 by 30
|
||||||
|
rotate column x=48 by 5
|
||||||
|
rotate column x=47 by 5
|
||||||
|
rotate column x=46 by 3
|
||||||
|
rotate column x=45 by 4
|
||||||
|
rotate column x=43 by 5
|
||||||
|
rotate column x=42 by 5
|
||||||
|
rotate column x=41 by 5
|
||||||
|
rotate column x=38 by 1
|
||||||
|
rotate column x=37 by 5
|
||||||
|
rotate column x=36 by 5
|
||||||
|
rotate column x=35 by 1
|
||||||
|
rotate column x=33 by 1
|
||||||
|
rotate column x=32 by 5
|
||||||
|
rotate column x=31 by 5
|
||||||
|
rotate column x=28 by 5
|
||||||
|
rotate column x=27 by 5
|
||||||
|
rotate column x=26 by 5
|
||||||
|
rotate column x=17 by 5
|
||||||
|
rotate column x=16 by 5
|
||||||
|
rotate column x=15 by 4
|
||||||
|
rotate column x=13 by 1
|
||||||
|
rotate column x=12 by 5
|
||||||
|
rotate column x=11 by 5
|
||||||
|
rotate column x=10 by 1
|
||||||
|
rotate column x=8 by 1
|
||||||
|
rotate column x=2 by 5
|
||||||
|
rotate column x=1 by 5
|
27
y2016/src/bin/d8.rs
Normal file
27
y2016/src/bin/d8.rs
Normal 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
100
y2016/src/days/d8.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -11,3 +11,5 @@ pub mod d5;
|
|||||||
pub mod d6;
|
pub mod d6;
|
||||||
|
|
||||||
pub mod d7;
|
pub mod d7;
|
||||||
|
|
||||||
|
pub mod d8;
|
||||||
|
Loading…
Reference in New Issue
Block a user