y2015d18
This commit is contained in:
parent
0d2fd4b451
commit
16ed2a8cae
@ -14,8 +14,8 @@ fn part1() {
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/15_input.txt")).unwrap();
|
||||
//let root = env!("CARGO_MANIFEST_DIR");
|
||||
//let content = fs::read_to_string(format!("{root}/resources/15_input.txt")).unwrap();
|
||||
// change comments in part1
|
||||
//println!("{}", d15::process_part2(&content));
|
||||
}
|
||||
|
@ -10,11 +10,11 @@ fn main() {
|
||||
fn part1() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/18_input.txt")).unwrap();
|
||||
println!("{}", d18::process_part1(&content));
|
||||
println!("{}", d18::process_part1(&content, 100));
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/18_input.txt")).unwrap();
|
||||
println!("{}", d18::process_part2(&content));
|
||||
println!("{}", d18::process_part2(&content, 100));
|
||||
}
|
||||
|
@ -1,26 +1,179 @@
|
||||
pub fn process_part1(input: &str) -> i32 {
|
||||
0
|
||||
use core::panic;
|
||||
|
||||
pub fn process_part1(input: &str, steps: u32) -> i32 {
|
||||
let mut grid = input
|
||||
.lines()
|
||||
.map(|line| line.chars().map(Light::from).collect::<Vec<Light>>())
|
||||
.collect::<Vec<Vec<Light>>>();
|
||||
let height = grid.len();
|
||||
let width = grid[0].len();
|
||||
let mut new_grid = vec![vec![Light::Off; width]; height];
|
||||
for _step in 0..steps {
|
||||
for (y, grid_line) in grid.iter().enumerate() {
|
||||
for (x, light) in grid_line.iter().enumerate() {
|
||||
let neighbors = Light::get_neighbors(grid.clone(), (x, y));
|
||||
new_grid[y][x] = light.new_state(neighbors);
|
||||
}
|
||||
}
|
||||
grid = new_grid.clone();
|
||||
}
|
||||
grid.iter()
|
||||
.map(|line| {
|
||||
line.iter()
|
||||
.map(|&light| if light == Light::On { 1 } else { 0 })
|
||||
.sum::<i32>()
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn process_part2(input: &str) -> i32 {
|
||||
0
|
||||
pub fn process_part2(input: &str, steps: u32) -> i32 {
|
||||
let mut grid = input
|
||||
.lines()
|
||||
.map(|line| line.chars().map(Light::from).collect::<Vec<Light>>())
|
||||
.collect::<Vec<Vec<Light>>>();
|
||||
let height = grid.len();
|
||||
let width = grid[0].len();
|
||||
let corners = vec![
|
||||
(0, 0),
|
||||
(width - 1, 0),
|
||||
(0, height - 1),
|
||||
(width - 1, height - 1),
|
||||
];
|
||||
for (corner_x, corner_y) in corners.clone() {
|
||||
grid[corner_y][corner_x] = Light::On;
|
||||
}
|
||||
let mut new_grid = vec![vec![Light::Off; width]; height];
|
||||
for _step in 0..steps {
|
||||
for (y, grid_line) in grid.iter().enumerate() {
|
||||
for (x, light) in grid_line.iter().enumerate() {
|
||||
let neighbors = Light::get_neighbors(grid.clone(), (x, y));
|
||||
new_grid[y][x] = light.new_state(neighbors);
|
||||
}
|
||||
}
|
||||
grid = new_grid.clone();
|
||||
for (corner_x, corner_y) in corners.clone() {
|
||||
grid[corner_y][corner_x] = Light::On;
|
||||
}
|
||||
}
|
||||
grid.iter()
|
||||
.map(|line| {
|
||||
line.iter()
|
||||
.map(|&light| if light == Light::On { 1 } else { 0 })
|
||||
.sum::<i32>()
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Light {
|
||||
On,
|
||||
Off,
|
||||
}
|
||||
|
||||
impl Light {
|
||||
fn new_state(self, neighbors: Vec<Light>) -> Self {
|
||||
let mut light = self;
|
||||
match self {
|
||||
Light::On => {
|
||||
if ![2, 3].contains(&Self::count_on(neighbors)) {
|
||||
light = Light::Off;
|
||||
}
|
||||
}
|
||||
Light::Off => {
|
||||
if Self::count_on(neighbors) == 3 {
|
||||
light = Light::On;
|
||||
}
|
||||
}
|
||||
};
|
||||
light
|
||||
}
|
||||
|
||||
fn count_on(neighbors: Vec<Light>) -> u32 {
|
||||
neighbors
|
||||
.iter()
|
||||
.map(|&light| if light == Light::On { 1 } else { 0 })
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn get_neighbors(grid: Vec<Vec<Light>>, coords: (usize, usize)) -> Vec<Light> {
|
||||
let relative_coords = vec![
|
||||
(-1, -1),
|
||||
(0, -1),
|
||||
(1, -1),
|
||||
(-1, 0),
|
||||
(1, 0),
|
||||
(-1, 1),
|
||||
(0, 1),
|
||||
(1, 1),
|
||||
];
|
||||
let height = grid.len();
|
||||
let width = grid[0].len();
|
||||
let (x, y) = coords;
|
||||
let mut neighbors = Vec::new();
|
||||
for (relative_x, relative_y) in relative_coords {
|
||||
let neighbor_x = x.checked_add_signed(relative_x);
|
||||
if neighbor_x.is_none() {
|
||||
continue;
|
||||
}
|
||||
let neighbor_x = neighbor_x.unwrap();
|
||||
let neighbor_y = y.checked_add_signed(relative_y);
|
||||
if neighbor_y.is_none() {
|
||||
continue;
|
||||
}
|
||||
let neighbor_y = neighbor_y.unwrap();
|
||||
if neighbor_x >= width || neighbor_y >= height {
|
||||
continue;
|
||||
}
|
||||
neighbors.push(grid[neighbor_y][neighbor_x]);
|
||||
}
|
||||
neighbors
|
||||
}
|
||||
}
|
||||
|
||||
impl From<char> for Light {
|
||||
fn from(value: char) -> Self {
|
||||
match value {
|
||||
'#' => Light::On,
|
||||
'.' => Light::Off,
|
||||
_ => panic!("Wrong character"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn print_grid(grid: Vec<Vec<Light>>) {
|
||||
grid.iter().for_each(|line| {
|
||||
line.iter().for_each(|&light| {
|
||||
if light == Light::On {
|
||||
print!("#");
|
||||
} else {
|
||||
print!(".");
|
||||
}
|
||||
});
|
||||
println!();
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const INPUT: &str = "{{EXAMPLE}}";
|
||||
const INPUT: &str = ".#.#.#
|
||||
...##.
|
||||
#....#
|
||||
..#...
|
||||
#.#..#
|
||||
####..";
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
let result = process_part1(INPUT);
|
||||
assert_eq!(result, 0);
|
||||
let result = process_part1(INPUT, 4);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2() {
|
||||
let result = process_part2(INPUT);
|
||||
assert_eq!(result, 0);
|
||||
let result = process_part2(INPUT, 5);
|
||||
assert_eq!(result, 17);
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,9 @@ pub mod d12;
|
||||
pub mod d13;
|
||||
pub mod d14;
|
||||
pub mod d15;
|
||||
pub mod d16;
|
||||
pub mod d17;
|
||||
pub mod d18;
|
||||
pub mod d2;
|
||||
pub mod d3;
|
||||
pub mod d4;
|
||||
@ -13,13 +16,3 @@ pub mod d6;
|
||||
pub mod d7;
|
||||
pub mod d8;
|
||||
pub mod d9;
|
||||
|
||||
pub mod d16;
|
||||
|
||||
pub mod d17;
|
||||
|
||||
pub mod d18;
|
||||
|
||||
pub mod d18;
|
||||
|
||||
pub mod d18;
|
||||
|
Loading…
Reference in New Issue
Block a user