Compare commits

...

2 Commits

Author SHA1 Message Date
Mathis 186f9802b7 2023-14: Implement part two 2023-12-23 21:52:39 +01:00
Mathis ae9f6e1829 2023-14: Implement part one 2023-12-23 21:52:23 +01:00
7 changed files with 387 additions and 0 deletions

100
input/2023/day_14/input.txt Normal file
View File

@ -0,0 +1,100 @@
.OO..##.###..#O.O..O#.OOO.....#..O##O#.OO...O..##.O.O.......##O...O...O#O.O.#..O..O.#O..OO...#......
....OO..#.#...O#.O..#O..#OO...OO..O......O#........O...#..OO..#.#.O#..OO...O####.O...O.O.#.O..O.O..O
.O#.......O.OO#OO##O#O..O.O....#.OO..O...O.OO........O..#...O.O......##....O.....##.O.#.#.O..O......
..##.#.....#..#.O#O...O..O...OOO.##O.O##...O#.OO.#....OOO.OO.#O.O.O#...OO.OO.....O#..#...O...OO#...O
OO..#..OO#..#..O..#O..O.O##O#OOO.#...OO.#....#..#....O.####O.##..O......#O..OOO#O.#...#.O...OO...OO#
#.O.#O.#.#..O.#.............#.#.OO......#.O.O##O###........#.....#.....#.....O.O.OO.O....O.......O.#
O#...#...........#..OO...O..#.....#...O.#.......O#.....##O#.OO##.......#..O......#..O..O.#O.#.#..O.#
OO...O##..O.#.......O#.........OO#..O..#O..#OO.O........#.O.O.O....OO#...#.#.#O...##O.#OO.#.....#..O
#OO...O...O...O....O...............O.#O..OO##O##O.....#.##...OO.O.#.##.##.O..#..#.O..#.#..O.........
.O..O.#...##..#.#OO....#.O#...OO#.......O..OO.#...OO##..O..O...O.O...O.#.O#.....O..OO.O.#.OO..#.##..
...OO.O.O.O#....#...O...#....OO.#O.#O..O.OO..OOO.O....#O.#...#.O#.O............#......O.O....#OO##.#
...#O......#O#..O....#.##..O.O.O#..#O.#O...O...#..#O..##.O#..O#....O##.O.#OOO.#........O##.OO.O..OO#
#OOO....#O...O.O....O.......O...O..#....##.#..#.O#O#.O#....#.#.#O#....#.#.....##.#O..OOO....#.O.....
#OO.O...#O.O....O..#..O.#....#....O#..O...##..#..OO#.....#O.O#O..O....#OO....##OO....#...OOOO.OO###.
O..#..O#.O.#..OO.#.OO...O..O......#........#O......O.OO.#....#..O#O.......OO##O..##OOOO.#.#OOO.....O
...#OO.#OO.O.##.O#.OO#...#..#.O##O#...#.#O...#.....#..O..O....O.OO.O.#..O.#.O...#.O#..O....O.#...O#.
#....O.....O.O...#..O....#..O..O.OO...#....O..O...#.....#...#....O.#.OO...OO#...O....#O.O..O.OO.O..O
..O#.....#O#....#.....OOO.#.......OO.O..#.#....#O......O.#.O.#..O...#.O...O..O.##O.O#O..#.O......#.#
....#O#.#..O#.OO.#.#.O.#......#OO...O#OO.........OOO..##...#..##O#..OOO...O..O..O##O...#..#.O.O..#..
..O#.O.......OOO..##..#...#....#.#..#O.OO..O..##..OO#.O...O..#OO...##O.#O..OO.....#O..#.....#O.....#
...OO.O.#.O.O..##O#...OO...##.O..OOO##.O.O.#..#.OO..O...O.#....O.O.....O##.#O.O.O#.#.......OO#......
O...#.O...O...#..OO...O..#..#........O...#...#......O#.#.....O.#.........#.#......O#O.O.........#...
.......O#...#...........O.#O#.O#.#.O.O..O..OO.#......##.OO..#......OO#....#O#OO.O.#..O#..O.OO..O..##
.O.....O.#.........O...O...O...O...OOOO..O....OO....#.#....O#.#..#.OO.OO....#.......#...#OO.....O.OO
O....OO....O..O.OO.###..O##.OO.#......O.O...##...#O...O..O...##.O.O...O....O.#OO.....O.....O.O.OO.OO
#O......OO....##..O..###.O....#.#.#.OO.............O.O.OOOO#.O.O..#O...O#..O......O.O.O.#.#.......O.
O#O.....#...#..O#.#....#..###..O..O...#....O....##.....#...O#.#.O.O......O..OOOOO....OO..#...#.OO#..
..O........OO....##...#O.....#.O.....O#.O..#.O.#.O......O#...OOO....O...#..OO....O..#...O.O..##.....
O...O..O.O....O.OO.O.OO..OO...#O...O.#.O..#O...#...#...O..##...#...OO#.....##..O#O.O....#.....OO#..O
...O#O..O..##.OO.......#.#O...O..#.#...#.O.#O##OOO.OOO...#.....O....##.....OOO.....O..O.O#.O....O...
O#..#O.O....#.........O#.OO#...OO......#......O.#...............O##OO.....O.....O.O..##..O#....#.OO.
#O..O.OO....##O......O.O...O#O..#.O..........O....#........O#.#..O..O............#..#...OO#.###..#.#
...#...#.#O....O.O.....OO.O.O...O.#.#.O...OO..O#....O#O......O.O..##.#.O.....#..O.O.#.#O...O.#......
.O...O#.O.......O.#....O.#...#.#..#O.....#..#..#.#O#..#.......O..#.#..O.OO.#OO.OOO..O....#.....OO#.O
O#.#..#.O..O..#O.......O.#..#....#.O..#...#......O...O##....O..#...O....#O..#.....OO.O.O#..O.#..#O..
#.#.....O...#O.#..O...#O..##..#O..#O...O.#OO...#....OO#..O.#O#O........OOO...O##..#..OO.O.O.O.O.....
..OO...#..OOO.O#..#...O..O.......O.#.#.O#.##OO....#..O....#..#.O.#O...O.O#O........O#OOOO#.#..O#.#..
.O....#.O#OOO.....O.#O............O...O#...O...O..O.#..O.O.....O#.O....O......O...O.O..OOO..O.##.O..
#...#........#..........###.O....OO.#.....#OOOO.OO#.OO...##...OO...O#.......O...#.#..#O..#.....#O#O.
O.O.#...O.#.....OO...O.O#......#........#..O...O..O.#..OO#.....#...O.##O#....O#..O#...##.O.O.#.#....
............OO.....O........##O.O...#..#.O.#...OO..#.O.#O.............O..#O.....#...O.#....O..O#..#.
.#..#.O.#......O..O##O.O.#..O..#O.O..O.O........O.......#.#.#O.#..O...O...O....O.O#...#..OO..O....OO
#..#..O....OO#.#.O.#......O...O.O#O...O#...O...#...#O.#....#..#O...O...........#.#.#..O##..#........
..O......O.#.OO.#O...O..#O...OO.#.....##...#.#....O....OO.#O.O.#....OO..#....#.#...#.O.....OO#..O...
#..........#...O.O....##....OO..O..O..#...#..###....##..O#..#.........#.....O#...#.#.......#.##.##O.
###.O.#..#.O.O.O..O..O...O...#O...O##.#..#O..O...#....O.....#..##......OO.#O...O#..O.O.O......#...OO
....O#........O.O#.O#O#...O.......#..O.#..#........O.....O#..O..O.OOO....OO.....#....O#.#..O.....##.
#.#O...#.O#O..#.#OOOO#.......#....O...O#.##.O....#..O.......O.##.#.OO...O...#O..O..#O.#......O.O.##.
.#O...OO#......O...#..##............O...##...O.#O....#..OO......O.O##OO..OO....##O.##..O............
O.O..OO.O.#.#....#..O.O.O.OOO...OO..#....O....O.#.#....#.O#.#.....O#O...O.OO..O.#O.#..OO#.O#........
#..#.O.##.##...#OO..O...OOOO#O##..#...#.##....#O.#O#.O.O#.#.O#...#.OO.......O........O..#.#.OOO#....
##....O...O#....O...O.#OO.O..O.....O#O#.#...OO.........#.......O.#.#..O..#O...#..O.#...#.##...O..O.O
.........O...O.O...#..O.O..OO.#...O.#.O........O.O.O....O...O..O..........O....O......O..#O.O..#O...
.......#.#....O#OOO.O.O..O.##...O..###.......O#.##...O.O....#.OOO..O...#.#.......#O.OO...O...OO.O...
.O...O...O....#.O.#...#..##O....#..O....O#..O.#.O..#...........#..O..##OO.......##..#.....O#...O#..#
...O....O....#....O..####.#O#.....#O.O..#..#O.OO...#...O.##.#.#....#...O...O..#.....OO.O....#..O..O.
.OO#.#...#....O.#O#.##....OO............O.....#.OO#O.#O.#..#...#.O....O.O..#..##.......O..O.#..##O.O
O...#.........#.O....O..O.....#.......#.OO...OO.#O#.#...OOO.O.O#.O.#...#O......#....O.......#.O.#.O.
.....O.............O.#..#.#..#.#...#..OOO....#.OO.O...O..#.##O.#O...O.O.OO......#....OO..O..........
O....#....#.#....#.........#...O.O#....OO.#..#O..#....OO...#....O...O#...O.......OOO...#......#...#O
..O..##O.#O.OOO..#..O.#....#.OOOO..O.O.....##O.....O.#.O.###..##....O..OOOO..#.#OO.O.OO.#O#..#...#.O
O#...O......#..#.....#..##.#OO#O#.....O...##....OO...O##...O....O.#...#O..#.O#..#O.O..#...#...OOO..O
O...##.O..O..O#....#..O........O....O#...O..O......O...OO...OO.#OO.O..#....O#O....O..#..#..OO.OO....
O..#.#O....O..O.O..O#...O.O...O..#.#.#..##.#O......#...OOO.#O...#..O.....O###O.....#.......O#.O.O...
OOO#..#.#...O...O..O.O..O#O.....#O.#..O....O...O.#..O.#...O#.......#.....#O.#.#..O...##..#.....O...O
O#...O#....O.#.#......O.....O.##.......#.O.......#.#.#...#..#O....##O......O..O.#.#..O..#..O.O#O.O..
O.O...O##.O#.....O......#...O.#...O.O..#........O..O..O#.O..OO#O.OO.........O...O..#.O......O...O#.O
..#......OO.#....O...O..O..##...#O#O#O..O..O......O..O.#O#..O......O..........OO..OOO#..#.OO....O...
.......##......#.O..#....#.O#O..O#...O#....O.OOOO..#.O.O..O..O..O.........O...O#...O..#O..#.#..O...O
.O#..OOOO.O......#O......OOO#.#.O.#O...##...O.O...##.O#.O.#.......#.......##...#O.O...#.#..O..##O..O
....OO......#.#.....O.#.OOO.O#.#...O.##.O..OO....#OO..O..O.O.O#..OO....O.O..#.O.##..O...OO..OO..OO#.
........#O.....#....O#.#OOO.O...........OO....#.#.....OO#..#OO..#..#.....O#O..OO..OO..O......#..O.O.
O#O....#.#......O#...#.##O..OO.O.....#O..#..O..O......O##..#OO...OOOO.....O#....O..O.....OOO...#O..O
...O..#..O......OO.#.....#.O...O.O.#.O..OO..#....#...#....O.#O.O...O.O......O......O..#...#O###.OO..
#O###.....#....#.#O..#...#O..OO....#.O...#.##OO.#.#.#.....#....O.###...#....#.#...O.......O...O..#.O
..#..O.O..#.........O#..OOOO###..O##.#........O...#.#O#..#...O...O#....O..#.#...O.O..OO.....O.......
#...O..#.......O........O....#.O......#...##.##......O.....O.##.O..O.....##...#OOO..O...O##.O.O.....
......#.OO....###.......#.###..OO#.OOO#O#.OOO#..O....OO........#...O...O#.OO.#....O#.......#..#..#..
#..#OOO....#.#.O.O..........#O....O#...O..O..#...O....O#..##.O..O.#O...#.#.#..O..O.O.#O..O..O...#.#.
.#...O.O##.....#..O.....OOO.#.....#OOO#.#..O.....O.#.O.O.#.#.O.O#.........O....#...#O.O##.##..O.O#.O
OOO...#O.OO....#.OOO..O....O.OO.O.#.O.#.#...#....OO........O..#..OO....#O....#.#O##.OO#O#.##OO.O.O.#
...O#..OO.....O#...#O........O....O#O....OO...#....O###..##.O..#..#.#..O..#O.OO..O........##O#.#..OO
.O...#..#....O.##...#.........##.#..O.O.....#.##..O.#O.#..O#...#....#.#.........##.O...O...OO#..O..#
O....##.O...O........O#..#...OO#O..OO..#.........##......O.......OO..O...O.O..#..........#.O..#...O.
###.#.........#OO....##...O#...O....O........#...#.O.#O.#.....##..O..#O.##......#.#.O.......O...O...
..#.OOOO..O......O#O.O.#.O.#..#.O...O..O.O..##O.O.......#...O#..#..O.#.#.O#.##...O......#.#...#.....
O.#.OOO.#......O....O..O...O#........#....#.......#O..O.OO..#.O#.OO..#.OOO..#O.....#OOOO..#.........
##.O...####OO#...O#.OO...O...O.......O...OO..####...OOOO..O##O....OOO....#..O.O........O....#.#.#O..
O.O....O...O#...O.#....O.....OO#O#OO..O##.O..O...O.#.....#O...O..#O...O#.....O##...O.OO...#.........
....#OO..OO..#O#......O#.O..#O..#..OO..#....#.....#.....O.O..O#....O.O..O.#..O#O###O....O...OO...#.#
#..OO.##O..OO.O..O...#....O.....##O..#............##.O.#...O.#O..#O..#...#...O.O....#O.....O.....O..
..O#..#...##.#.#.O#.OOO.O#.#....OO.#....#...#...#.....#O.....O..O...O.....#.O.O.O#...#O.#...##.O..O#
#....#.O...O#...##O.#....O........O...O#.#.#...#O#..O..O.#O.....O..O....#....O.O#O#....#O#..#......#
.O.O#.........O.....##.O.#..............O...#.#OOO.O.OO..O..O..O##....#.......OO....O....#OO#..OO...
##.O#...OO...O.O...........O..O.##O..#.#...#..O..#.##....#..###O..##........OOO.#.O.#......#......#.
#......O#..O#.#.#..#O##..OO#O...##O.#...#.OO...#.O..O##O.O...........O.##...###O..#.#.O.O..OO.O..O..
#.....#.##.O.O.#....O..#O......O.O#...#..O.#O#OO...#.OO...O.O##.O...O.......#...##....#O.....O.##O#.
...O.OO.O...O#..OO.O....#.#.O....O...#..#O.O..OO.#....O.O#.O#..O..O...O..O........#..#..#..#..OO.O#.
.#O.O....#....#.OO.#.O...O......O....O#.....##...#O...##...#.#OO..O#O..O##.#.#..O...O.....#..O##.O..
OOO....OOOO...#.#...#.....###...O......O.#.O.....O...#.O##...##O.#.#...O....O.O...#.O...#O....O.....

View File

@ -0,0 +1,10 @@
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....

View File

@ -0,0 +1,20 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Rock {
R, // Round
S, // Square
N, // None
}
#[allow(dead_code)]
pub fn print_platform(platform : &Vec<Vec<Rock>>) {
for row in platform.clone() {
for c in row {
match c {
Rock::R => print!("O"),
Rock::S => print!("#"),
Rock::N => print!("."),
}
}
print!("\n");
}
}

View File

@ -0,0 +1,25 @@
pub mod part_one;
pub mod part_two;
pub mod common;
use crate::utils::solution::Solution;
use crate::utils::input_reader;
pub struct DaySolution ;
impl Solution for DaySolution {
fn input_path(&self) -> &'static str {
"input/2023/day_14/input.txt"
}
fn part_one(&self) -> String {
input_reader::read_input_file(self.input_path(), part_one::part_one)
}
fn part_two(&self) -> String {
input_reader::read_input_file(self.input_path(), part_two::part_two)
// String::from("Not implemented")
}
}

View File

@ -0,0 +1,54 @@
use super::common::Rock;
pub fn part_one(input_lines: Vec<String>) -> String {
let mut platform: Vec<Vec<Rock>> = Vec::new();
for line in input_lines {
let mut row: Vec<Rock> = Vec::new();
for c in line.chars() {
match c {
'.' => row.push(Rock::N),
'#' => row.push(Rock::S),
'O' => row.push(Rock::R),
_ => panic!("Invalid character: {}", c),
}
}
platform.push(row);
}
tilt(&mut platform);
let result = count_load(&platform);
result.to_string()
// String::from("Not implemented")
}
fn tilt(platform : &mut Vec<Vec<Rock>>) {
for i in 0..platform.len() {
for j in 0..platform[i].len() {
if platform[i][j] == Rock::R {
let mut k = i;
while k >= 1 && platform[k - 1][j] == Rock::N {
k -= 1;
}
platform[i][j] = Rock::N;
platform[k][j] = Rock::R;
}
}
}
}
fn count_load(platform : &Vec<Vec<Rock>>) -> usize {
let mut count = 0;
for i in 0..platform.len() {
for j in 0..platform[i].len() {
if platform[i][j] == Rock::R {
count += platform.len() - i;
}
}
}
count
}

View File

@ -0,0 +1,176 @@
use std::collections::HashMap;
use super::common::Rock;
pub fn part_two(input_lines: Vec<String>) -> String {
let n = 1000000000;
let mut platform: Vec<Vec<Rock>> = Vec::new();
for line in input_lines {
let mut row: Vec<Rock> = Vec::new();
for c in line.chars() {
match c {
'.' => row.push(Rock::N),
'#' => row.push(Rock::S),
'O' => row.push(Rock::R),
_ => panic!("Invalid character: {}", c),
}
}
platform.push(row);
}
let mut memory: HashMap<String, usize> = HashMap::new();
let mut i = 0;
while i < n {
// println!("{}", i);
platform = cycle(platform.clone());
let hashed_platform = hash_platform(&platform);
if memory.contains_key(&hashed_platform) {
let cycle = &memory[&hashed_platform];
// println!("Cycle found at {} - {}, lenght: {}", cycle, i, i - cycle);
let cycle_length = i - cycle;
let remaining_cycles = (n - i) / cycle_length;
i = i + remaining_cycles * cycle_length + 1;
continue;
} else {
memory.insert(hashed_platform, i);
}
i += 1;
}
let result = count_load(&platform);
result.to_string()
// String::from("Not implemented")
}
fn tilt_north(mut platform : Vec<Vec<Rock>>) -> Vec<Vec<Rock>> {
for i in 0..platform.len() {
for j in 0..platform[i].len() {
if platform[i][j] == Rock::R {
let mut k = i;
while k >= 1 && platform[k - 1][j] == Rock::N {
k -= 1;
}
platform[i][j] = Rock::N;
platform[k][j] = Rock::R;
}
}
}
platform
}
fn tilt_west(mut platform : Vec<Vec<Rock>>) -> Vec<Vec<Rock>> {
for i in 0..platform.len() {
for j in 0..platform[i].len() {
if platform[i][j] == Rock::R {
let mut k = j;
while k >= 1 && platform[i][k - 1] == Rock::N {
k -= 1;
}
platform[i][j] = Rock::N;
platform[i][k] = Rock::R;
}
}
}
platform
}
fn tilt_south(mut platform : Vec<Vec<Rock>>) -> Vec<Vec<Rock>> {
for i in (0..platform.len()).rev() {
for j in (0..platform[i].len()).rev() {
if platform[i][j] == Rock::R {
let mut k = i;
while k + 1 < platform.len() && platform[k + 1][j] == Rock::N {
k += 1;
}
platform[i][j] = Rock::N;
platform[k][j] = Rock::R;
}
}
}
platform
}
fn tilt_east(mut platform : Vec<Vec<Rock>>) -> Vec<Vec<Rock>>{
for i in (0..platform.len()).rev() {
for j in (0..platform[i].len()).rev() {
if platform[i][j] == Rock::R {
let mut k = j;
while k + 1 < platform[i].len() && platform[i][k + 1] == Rock::N {
k += 1;
}
platform[i][j] = Rock::N;
platform[i][k] = Rock::R;
}
}
}
platform
}
fn cycle(mut platform: Vec<Vec<Rock>>) -> Vec<Vec<Rock>> {
platform = tilt_north(platform);
// println!("Tilt North:");
// print_platform(&platform);
platform = tilt_west(platform);
// println!("Tilt West:");
// print_platform(&platform);
platform = tilt_south(platform);
// println!("Tilt South:");
// print_platform(&platform);
platform = tilt_east(platform);
// println!("Tilt East:");
// print_platform(&platform);
// println!();
return platform.to_vec();
}
fn count_load(platform : &Vec<Vec<Rock>>) -> usize {
let mut count = 0;
for i in 0..platform.len() {
for j in 0..platform[i].len() {
if platform[i][j] == Rock::R {
count += platform.len() - i;
}
}
}
count
}
fn hash_platform(platform : &Vec<Vec<Rock>>) -> String {
let mut hash = String::new();
for i in 0..platform.len() {
for j in 0..platform[i].len() {
hash.push(match platform[i][j] {
Rock::N => '.',
Rock::S => '#',
Rock::R => 'O',
});
}
hash.push('\n');
}
hash
}

View File

@ -11,6 +11,7 @@ pub mod day_10_pipe_maze;
pub mod day_11_cosmic_expantion; pub mod day_11_cosmic_expantion;
pub mod day_12_hot_springs; pub mod day_12_hot_springs;
pub mod day_13_point_of_incidence; pub mod day_13_point_of_incidence;
pub mod day_14_parabolic_reflector_dish;
pub fn run(day: &str) { pub fn run(day: &str) {
let solution: Box<dyn Solution> = match day { let solution: Box<dyn Solution> = match day {
@ -25,6 +26,7 @@ pub fn run(day: &str) {
"11" => Box::new(day_11_cosmic_expantion::DaySolution), "11" => Box::new(day_11_cosmic_expantion::DaySolution),
"12" => Box::new(day_12_hot_springs::DaySolution), "12" => Box::new(day_12_hot_springs::DaySolution),
"13" => Box::new(day_13_point_of_incidence::DaySolution), "13" => Box::new(day_13_point_of_incidence::DaySolution),
"14" => Box::new(day_14_parabolic_reflector_dish::DaySolution),
_ => panic!("Invalid day specified"), _ => panic!("Invalid day specified"),
}; };