Skip to content
Snippets Groups Projects
Commit 726c9763 authored by Falk Rehse's avatar Falk Rehse
Browse files

Attemp at Day 14-2

Unfortunately way to slow
parent 8f67be2d
No related branches found
No related tags found
No related merge requests found
use std::{
collections::{hash_map::DefaultHasher, HashMap},
hash::{Hash, Hasher},
};
use crate::util::*;
pub fn solve_from_str(input: &str) -> usize {
let mut grid = to_grid_flipped_axis(input);
let height = grid.len();
let mut hashes = HashMap::new();
let total_cycles = 10;
let mut skipped = false;
let mut i: usize = 0;
while i < total_cycles {
tilt_up(&mut grid);
tilt_left(&mut grid);
tilt_down(&mut grid);
tilt_right(&mut grid);
if !skipped {
let mut hasher = DefaultHasher::new();
Hash::hash_slice(&grid, &mut hasher);
let cycle_hash = hasher.finish();
if hashes.contains_key(&cycle_hash) {
let previous = hashes.get(&cycle_hash).unwrap();
dbg!(previous);
dbg!(i);
let period_length = i - previous;
while i + period_length < total_cycles {
i += period_length;
}
skipped = true;
dbg!(i);
} else {
hashes.insert(cycle_hash, i);
i += 1;
}
} else {
i += 1;
}
}
print_grid_flipped_axis(&grid);
grid.iter()
.enumerate()
.map(|(i, line)| get_line_value(height - i, line))
.sum()
}
fn tilt_up(grid: &mut Vec<Vec<char>>) {
let width = grid[0].len();
let height = grid.len();
for y in 0..height {
for x in 0..width {
if grid[y][x] == 'O' {
let mut y_valid = y;
for y_new in (0..y).rev() {
if grid[y_new][x] == '.' {
y_valid = y_new;
} else {
break;
}
}
grid[y][x] = '.';
grid[y_valid][x] = 'O';
}
}
}
}
fn tilt_down(grid: &mut Vec<Vec<char>>) {
let width = grid[0].len();
let height = grid.len();
for y in (0..height).rev() {
for x in (0..width).rev() {
if grid[y][x] == 'O' {
let mut y_valid = y;
for y_new in (y + 1)..height {
if grid[y_new][x] == '.' {
y_valid = y_new;
} else {
break;
}
}
grid[y][x] = '.';
grid[y_valid][x] = 'O';
}
}
}
}
fn tilt_left(grid: &mut Vec<Vec<char>>) {
let width = grid[0].len();
let height = grid.len();
for x in 0..width {
for y in 0..height {
if grid[y][x] == 'O' {
let mut x_valid = x;
for x_new in (0..x).rev() {
if grid[y][x_new] == '.' {
x_valid = x_new;
} else {
break;
}
}
grid[y][x] = '.';
grid[y][x_valid] = 'O';
}
}
}
}
fn tilt_right(grid: &mut Vec<Vec<char>>) {
let width = grid[0].len();
let height = grid.len();
for x in (0..width).rev() {
for y in (0..height).rev() {
if grid[y][x] == 'O' {
let mut x_valid = x;
for x_new in (x + 1)..width {
if grid[y][x_new] == '.' {
x_valid = x_new;
} else {
break;
}
}
grid[y][x] = '.';
grid[y][x_valid] = 'O';
}
}
}
}
fn get_line_value(multiplier: usize, line: &Vec<char>) -> usize {
line.iter()
.map(|char| if *char == 'O' { multiplier } else { 0 })
.sum()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment