This commit is contained in:
JonOfUs 2023-12-14 11:27:30 +01:00
parent 4f06bb8fe5
commit 3027ffbdd7
4 changed files with 245 additions and 1 deletions

10
res/14/example Normal file
View file

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

100
res/14/input Normal file
View file

@ -0,0 +1,100 @@
#..O...O...O....##O#O#O..O.#...O..O..#O.#O.#.O....##.OOOO....O.O.OO..O##..O..O....#O#....#..O.#.#.O.
OO.....O.O...##..#.#.......#...#.O.#.#..#...#...#....##O......#O#........#...O......O.#......#O.O...
.O..##.OO.OO#OO.......O..O.#...#O.O...O.O.#..OO.#...O.OO.#.O.#....O.O..O.O#O...O...#.O..O...#......O
...........#...#.O..O.#.O.#..O#.O.OO.....##O..##..OO..O..#OO..O...#.#..............OOO.......#...O..
..##.O#.....OO.O#O.#.##.....O...#...#..O#O#.....#..O..O................OO..........#....#..O#....#..
..OO.....O..#.....O...#O.O.O...#....##...#....O.OO#.#..#O..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.....#.#..OO...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...O.O#..........O#..O......O.....O......O...O....O.#.....##..#O..#.....
...O#..#.O..#O..#O##.....#....O..#O...O.....#....O....OO#..OO.OOO......#..#...O#O...#..O#.#.#...O...
#....O.#.....#O...#...........O.......O.#...O.....O#.....#.....OO#O......O.O..O.......#OO..O...OO.O.
......OOOO.#...O..#.#.......OO.....O........#O...O.O.....#...#.#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.O#O#.........O#OO..O....O#..OOOO.O#.#####O#.#O#..O##.OO...##..O#O..###OO..
.O#O#O#O.O.O.....OOO..OO.O#...#..#O....#..O#OO.O..#..O.OOO..#OO.OO....O#.....O.#......#.OO..O.###.O#
O#..O.#.#..#O..#.#.O#..OO.....O..OO...O.#O.#..OO#O..OO..#..O.O.#....O.OO.##O.....O.#.O.#...O...#...#
#..O#..O...#......O..O#O.....##.OOO..#.O.#..#.##..O..O#O##........#..O.....#.....O..O...#.#...#.#...
..#.O.#........O.....OO..O#.#....O..O.OO##O#####.O..##...#..O.O....O.#OO#.......O....O.O..........OO
O.#.###...O#.....#.#....#.......OO.#.#......#...O..#.......#.#OO..O....O.O###.OO..#...##.........O..
..O.#O...O...#..#...O..O..#...#O..#.O....O..#O...#...O..#...O....O......#...OO..#.O##O.O....OOO#O...
.OO.#O..#.O#...O...O.O...#..#OOO....#.OO..#.O.....O.#O....O......#..O#..OO#.#...#.....O..#..O..OO.#O
.....O.#.O...O.......##...O.OOO.O...O.OOOO........O.#..O.OOO#.#....##....O..####O.......#.O.#O.#OOO.
####.##..O...O..#O..O.##.O..#.#.O......O#....#..OO.#.##O#O....O..#.##.#.O.#O.O#..O..##OO....O..#.O.O
.#.O..#..##......O....O..#...O..O.OO......O#........#..O.###.OO##.....##.#..O#.O..#OO.##.#O..O....O.
....O#.O#....O#O....##.....#...#..O.OO.OO....#.#...OOO#.#...O..O.OO...O....OOO.O.....O.OOOO...#..O.#
...#O..O#.OOO..O..O.....#.#...O.#O.....O#....O#..O#..#....#OO..O..O.O#.##......#O...#..O......#.#O.#
...OO.O.......O..........##.....##....OO....#.O.O.O#......#......#.#......O.OOO....O.O.#.O..#...##.O
.O..O.#....OO..OOO.......#......O....O...O.......OO..OO....O....O.O.OO..O.#...#OO.O#.O.#.....#......
.O.O.##....###O.##.O..#.OOO...........O...O..#.O#.O#O.O.##.O#..O#............O......##...OO.O..O...#
..O.....OO.OO.O#.O......O..O#..O.O#.....O..O.........O...O#.O.##.#O#.....#.#.O.#.O...#.....OOO.#...#
OOO..O.#....O.O...O.O.O...#.O.O...#.......#....#..##...##..##OO....##..#OO.#..#OO.#OO.O.#O.#....O...
..O#.....O....O.O..O.#O#...OO#.OOO##...O#.....#..O#O.OO#.##.O..O.....O...O...O.....#......OO...O.##.
O#.......O#.....#....#.#.....O.O.#..#O..O.....#....O.#.#..O...##...O..#O.O.....O.OO....O....#.#..#.#
#OO.....OO.#.O.O..........#..#O.##O.#.#.#..O...#.#....#.OO...OO#.O..#.........OO.#..O.#O.OO....#O##.
O.##...#...O.#O.O#..#O....OOO....O.#...#O#..O.O.#..O.##....O#..OOO....##..O.....##......O.O.#...#...
..O.#..O.#.###O.#O.O..O.#.O....#.OOO...OO...O.OOO..OOO..O.O.#.O#.O.#O...O...#..#.O###..#.#...OO.OOO.
...#..#.#.......OO#O..O##.#.O..#.O....O##..O..O..#..OO..OO..O.O.OO#O...#.O..#OO...O...#...#..#.##.#.
..O....O.......O....O#O....#....#.####...O..O.#......O..#....O..#...#..O..#.O.O..O#.OO#.OO.O...O.O.O
......#.O...O........#O.O.#..O..O.O.O....OO..##..O#..O#.#...O........O...#.O....#...##.OO#OO...O....
O.O.#.O...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#OOOO.#....
.OO......O.O.OO.O#O...O...O#....#..#.#.##O..O.#.##.OO#...#..O.....OOO#..#............#....#..#..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...O...........#O........O......O.....#O....#..OOO#...O.O#..OO##O.#.O#OOO
.#..#...O###..OO.....O.#..O.O..###..O..#O.#.........#O..OO......O....O.#O.#...OO....#O...#.OO....#OO
.......#.O...OOOO..#O.#..#O..O.##O..#.O#....O.#O#.#.......#O##.O..OOO..#....O.O###.O#..O..#.O.......
OO...#......#O#..O.#O#....##.....O....O...OO.#..OO.O.....#..#......O..O....#....#.....#..#....O.O.O.
........#.OO.#.O...O#.##...#...#.#.#....O.O...OOO.O..#..O..O.O.OO.#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....#O.OO.O.......O...#OO..#.#OO.......#.O.#...O..#OO...#.......O...#.O...OO.............O..O..
.O......O.....O..O.OO..O.#.O###.......O..O......#.O.....OO........O.#.......#..#O..O.OO.#.....#.OO#.
.#........OO.O.#.#O.#.....#...#...O...#..O.....#...O.#..#O....O..#..O#........#.O....OO.#.....O.#..O
#.#.........O..##.O#..###.O..#.....O#O#.#..#O.#.....#O#.#.....#.#.O...O#.##.........#..O#.........O.
......#..OOO#..#.....O.#..OO.OO.###.#.O..........O..OO..#......#...O.#.....O.O........O..O#O..#.#...
O.........O...#..O...#..O.#...OO....#..##OO...#..........O.OOO.....O..O#O....#..#.OOO.#......O......
.O#..O..#O.O.O.#O#..O.O..OO#..O...#....O..O.....O....#...#..#OO.OO.O..#...#O#.OO.#...#........O.....
.O.#O...O...#...........O....O....##..OO...O.#..O..O#..OO#.O.#...#O...##O#.#......O........#.#.....O
O..#OO.O.OO..O.....O..#O..O........#O.O............#.O.....O..#.##.....##........OO.O..O..O.#.O.....
..O.#....O...#...#OO.##.O....#O.O#...O..O...#O#O.O.OO..O...........###......#....#.O..O.#O.O...O..#O
.O.......#...OO....OO..OO.#O.....O.#........O.....O#O.#..O...#..........##O..O.O.....#....#O.##OO..O
#.#..#..OO#..#...O.....#OO#..O.#.O.O#.....OOO.OO.#O.O..OO.O.OO.O#O.O..#..O.##...#.#.O.........#.....
.#O.#OO.O.#O.#.###....O...#.#.OO.#OO....#..##OO#...#..O#...#....#..OO..#..##...OO.......O....O.#...O
....O#.....O.O#.......O..O.OOO#.#.......OO..#.......O...#O..#..#.OOO.OO...O##..........O.....O.O#OO.
...#.O...O.#.OO.O.#........O.##OO....##O..#O.#....O..#.##......#O.O.OO...OO.....O.##........O.....##
#.OO#..#...O....O.......O#.#.#..#OOO#.O........##..O..O.......O...O..O..#...O#..#..#...OO.#..##.#O.O
....O...O#.......###O...#OO..O#..OO#O.....O........O.O.O#OO...O...O#......##.O.##O...O..O##.#O..O..O
..OO..OOO.#.O....#......O...#O....O....OOO#..#....O#.O.#.#.#...O.#......#.##..#O...O...#..#.O.#.#.#O
.#OO.#..O###OO.#.O..O.....#.#...O..O#..###...#.O..O....#........O.....OO..#O.OO...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.........#.OO#OO.#.......O.
..O#OO..OO.....O.#..O.#O........#OO#..OO.O...O..O.##O...O.O#..O#...O.OO.#.O...O....#..OO.....O.OO...
........#OO.........O##..O.OO...O#....O#O.O.O..O..O...#O.O.#..O.O......O.O#.........#.O.O.#....OO..O
.#.O....#..O.O..O##.#....O.......#.O.#......#.....##.O.......OOO#..#..###......O###.O#...#..OO.O...#
#.O#......OO#....O.#O...#..O....O..#O#OO.....O.....#.#.O##...O.O#...OOOO#.O.#.#....O.#O..#.#O#...O..
OO..OO#..O.O.#......#..O....#....#.......#O.#O#O#.O........#....#.O.OO.#..#.O.O.#O#..O.O.O.#.O.O.#..
......O....#O.O..#.#.....#.O.O.O#..O.O.#....#.....#O..#..O#..OO....O..OO#...O..O............#O..OO.O
....#......O...#.O...#O...OO...O###......O.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...O#...OO......O...O.O#......O...OO#O.O.O...#O..OO....O.OO.#....O.O.#..##.#..#O..
....O...#.#.OO#..O.O..O.OO.....#O...O..#......O......#....O.....OO.O#......#O.#.#..O......#...##O.O.
..O.OO...#O#...#O....OOO.#.O.##..O..O..O#..O..O.#...OO...O.....O.O....O...O....#.O.....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.OO##...#.#..O.......#...O....#.#.....O.....#O..#......#O.##
.O.O.......#O.O...OO..#..#...#..O.....O...##O....#..#..#O..O...O#.........O..#.....O..OOO....O#..##.
OOO.OO..O#.....#.O........O..##.....O...O#.O.#....O......#OO.....#.#.........O..#O...##O....#.#..O..
..#.#..O...#.OO.O...O..#.#....#.##...#.O..O.#.....#...O.#.#.....#OO.....#.O...O....O....O..#.O.....O
...#.......O..O..#.....#.OOO......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.#...O#...#OO.##.O#O..OO###...O.#...#O.O..O##OO....#....O...#..O.#O#.#O#O.....##O#.
...#..#..OOOO..O#.....#O..................#..O..........#..............O...........O...........O....
O....O#O.O..O###....##.OOO.O#O.OO.#.O.O......#.OO.O.....O.O..O.OO..#..#........O..#..O..O.O.#.#O..O.
..###..O.#....O....#..##.O....#..OO....OOO.....#..#.#O.O..#...#.#...O#..OOO#O#.......O..O.O.#O.#O.O.
.#...O....#...#....##..O....#.#O.O#.O...........OOO.............O....#.#..O....#O.O...O.O..O..##...#
....#...O.........#..O...#O.............O...OO..#..O.#OO##.O....O....O....#O..#......OO..OO##O..###.
.......O...#O#O...##...O......#....##.#.#.......O#.O...OO..#.##.OOOO..#...O..O...###.#........O..O..
.......O......O..#.O.O..O..##OO.............O#...........OOO..OOO.O.#O##..O...#O....O#....O........#
O#..O..OO.OO.O....O...#......#..#...O.#OOO....#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#.#O.......##..O..OO#..#.......O..OOO..O..OO...O...#OO......#.OO........###O...##.....

134
src/days/d14.rs Normal file
View file

@ -0,0 +1,134 @@
use std::{fs, collections::HashMap};
pub fn solve() {
let path = "res/14/input";
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let rocks_init: Vec<Vec<char>> = contents.lines()
.map(|line| line.chars().collect::<Vec<_>>())
.collect();
let mut rocks = rocks_init.clone();
let mut result: usize = 0;
for x in 0..rocks.len() {
for y in 0..rocks[0].len() {
if rocks[x][y] == 'O' {
let mut new_x = x;
while new_x > 0 {
new_x -= 1;
if rocks[new_x][y] == '#' || rocks[new_x][y] == 'O' {
new_x += 1;
break;
}
}
rocks[x][y] = '.';
rocks[new_x][y] = 'O';
result += rocks.len() - new_x;
}
}
}
println!("Result 1: {result}");
let mut counter = 1;
let mut cache: HashMap<Vec<Vec<char>>, usize> = HashMap::new();
let mut cache_used = false;
while counter < 4000000000 {
if counter % 4 == 0 || counter % 4 == 1 {
for x in 0..rocks.len() {
for y in 0..rocks[0].len() {
if rocks[x][y] == 'O' {
match counter % 4 {
0 => { // north
let mut new_x = x;
while new_x > 0 {
new_x -= 1;
if rocks[new_x][y] == '#' || rocks[new_x][y] == 'O' {
new_x += 1;
break;
}
}
rocks[x][y] = '.';
rocks[new_x][y] = 'O';
},
_ => { // west
let mut new_y = y;
while new_y > 0 {
new_y -= 1;
if rocks[x][new_y] == '#' || rocks[x][new_y] == 'O' {
new_y += 1;
break;
}
}
rocks[x][y] = '.';
rocks[x][new_y] = 'O';
}
}
}
}
}
} else {
for x in (0..rocks.len()).rev() {
for y in (0..rocks[0].len()).rev() {
if rocks[x][y] == 'O' {
match counter % 4 {
3 => { // east
let mut new_y = y;
while new_y < rocks[0].len()-1 {
new_y += 1;
if rocks[x][new_y] == '#' || rocks[x][new_y] == 'O' {
new_y -= 1;
break;
}
}
rocks[x][y] = '.';
rocks[x][new_y] = 'O';
},
_ => { // south
let mut new_x = x;
while new_x < rocks.len()-1 {
new_x += 1;
if rocks[new_x][y] == '#' || rocks[new_x][y] == 'O' {
new_x -= 1;
break;
}
}
rocks[x][y] = '.';
rocks[new_x][y] = 'O';
}
}
}
}
}
}
counter += 1;
if counter % 4 == 0 && !cache_used{
let cycle_start = cache.get(&rocks);
match cycle_start {
None => { cache.insert(rocks.clone(), counter); },
Some(x) => {
let cycle = counter - x;
counter += (((4000000000-counter)/cycle) as usize) * cycle;
cache_used = true;
}
}
}
}
result = rocks.iter().enumerate()
.map(|(i_row, row)| {
row.iter().filter(|c| **c == 'O').count() * (rocks.len() - i_row)
})
.sum();
println!("Result 2: {result}");
}

View file

@ -2,7 +2,7 @@ pub mod days;
#[macro_use] extern crate scan_fmt; #[macro_use] extern crate scan_fmt;
fn main() { fn main() {
days::d13::solve() days::d14::solve()
//_all_days() //_all_days()
} }