clippy
This commit is contained in:
parent
50bd733e55
commit
a81b6e6760
|
@ -1,8 +1,8 @@
|
|||
#![feature(test, try_blocks)]
|
||||
extern crate test;
|
||||
use aoc2023::{boilerplate, common::*};
|
||||
use fnv::{FnvHashMap, FnvHasher};
|
||||
use std::{hash::Hasher, mem::transmute};
|
||||
use fnv::FnvHashMap;
|
||||
use std::mem::transmute;
|
||||
|
||||
const DAY: usize = 14;
|
||||
type Parsed = Vec<Vec<Tile>>;
|
||||
|
@ -36,15 +36,15 @@ fn tilt_north(grid: &mut Parsed) {
|
|||
}
|
||||
|
||||
fn tilt_west(grid: &mut Parsed) {
|
||||
for y in 0..grid.len() {
|
||||
for x in 0..grid[y].len() {
|
||||
if grid[y][x] == Tile::Round {
|
||||
for line in grid.iter_mut() {
|
||||
for x in 0..line.len() {
|
||||
if line[x] == Tile::Round {
|
||||
let mut i = 0;
|
||||
while let Some(Tile::Empty) = try { grid[y][x.checked_sub(i + 1)?] } {
|
||||
while let Some(Tile::Empty) = try { line[x.checked_sub(i + 1)?] } {
|
||||
i += 1;
|
||||
}
|
||||
grid[y][x] = Tile::Empty;
|
||||
grid[y][x - i] = Tile::Round;
|
||||
line[x] = Tile::Empty;
|
||||
line[x - i] = Tile::Round;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -66,15 +66,15 @@ fn tilt_south(grid: &mut Parsed) {
|
|||
}
|
||||
|
||||
fn tilt_east(grid: &mut Parsed) {
|
||||
for y in 0..grid.len() {
|
||||
for x in (0..grid[y].len()).rev() {
|
||||
if grid[y][x] == Tile::Round {
|
||||
for line in grid.iter_mut() {
|
||||
for x in (0..line.len()).rev() {
|
||||
if line[x] == Tile::Round {
|
||||
let mut i = 0;
|
||||
while let Some(Tile::Empty) = try { grid[y].get(x + i + 1)? } {
|
||||
while let Some(Tile::Empty) = try { line.get(x + i + 1)? } {
|
||||
i += 1;
|
||||
}
|
||||
grid[y][x] = Tile::Empty;
|
||||
grid[y][x + i] = Tile::Round;
|
||||
line[x] = Tile::Empty;
|
||||
line[x + i] = Tile::Round;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -92,15 +92,12 @@ fn part1(parsed: &Parsed) -> usize {
|
|||
|
||||
fn part2(parsed: &Parsed) -> usize {
|
||||
let mut grid = parsed.clone();
|
||||
let mut weights = FnvHashMap::default();
|
||||
let mut grids = FnvHashMap::default();
|
||||
let mut n = 0;
|
||||
let cycle = loop {
|
||||
let mut hasher = FnvHasher::default();
|
||||
for l in grid.iter() {
|
||||
hasher.write(unsafe { transmute(l.as_slice()) });
|
||||
}
|
||||
let hash = hasher.finish();
|
||||
if let Some((_, old_n)) = weights.insert(hash, (weight(&grid), n)) {
|
||||
// I tried not cloning the grid and instead inserting only a grid hash and the weight here,
|
||||
// but that’s only ~15% faster and ugly, so we’ll just do it naively here.
|
||||
if let Some(old_n) = grids.insert(grid.clone(), n) {
|
||||
break n - old_n;
|
||||
}
|
||||
n += 1;
|
||||
|
@ -112,7 +109,8 @@ fn part2(parsed: &Parsed) -> usize {
|
|||
};
|
||||
const REPETITIONS: usize = 1000000000;
|
||||
let steps = (REPETITIONS - n) % cycle;
|
||||
weights.into_iter().find_map(|(_, (w, n2))| (n2 == n - cycle + steps).then_some(w)).unwrap()
|
||||
let cycle_start = n - cycle;
|
||||
grids.into_iter().find_map(|(g, n2)| (n2 == cycle_start + steps).then(|| weight(&g))).unwrap()
|
||||
}
|
||||
|
||||
boilerplate! {
|
||||
|
|
Loading…
Reference in New Issue
Block a user