Compare commits
No commits in common. "e062ef5078bdf68a574ea9bf7a2b292dcb8d47c1" and "5666aee5f2f3cf5e53c38fba7f75c04945bd6d6d" have entirely different histories.
e062ef5078
...
5666aee5f2
92
14/Cargo.lock
generated
92
14/Cargo.lock
generated
@ -1,92 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "day14"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"num",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-complex",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-rational",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-complex"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-iter"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-bigint",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
@ -1,9 +0,0 @@
|
||||
[package]
|
||||
name = "day14"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
num = "0.4.1"
|
100
14/input
100
14/input
@ -1,100 +0,0 @@
|
||||
O..#OO..#...O..O#O.O...O##.O#..#O#...O.O.....O..........O..#...#.....O.O.O.O#..O........O...........
|
||||
.....O#.#O..OO.#......O.O..#...#..OO..O..###..#...O.#...#..O..O.#..#O...#O...............##O..O....#
|
||||
..#...O...OO.#........O......O#O#......O.#OO....OOO..#...O....O.O.O#..#OO.##...OOO#.#.OO.#O.#....O..
|
||||
..#...#..#....O...#.#O......O.....#O##.#..#.O..#.....#O.O#...#.......O.....O.##.O.O..#.....O.#.O.OOO
|
||||
O....#..#.O.O..O.#.#....#.#..##O...O#..O.#.O.OOO.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...OO#.#...O.OO.O..#..#..#.O#..#.....#OO....O.###....#O...
|
||||
.......#.O.......O.O####OOO..#.O#..#.O.......OOO..O....#........O##....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...OOO..OO...OO...#..O.OO#..#.O...##.#..O..O..........O#..#O.O#..
|
||||
#..#OO#.O#.#........#.........O......##....#...O....#O.....O..##..#O.OO#......O..O.O.OO...O#O.......
|
||||
#........#.....O.#....O...OO..OO......###..........##O##O.#.....#....###...#.#...O...#.O#.........O.
|
||||
...O.OO.....O..#...O.#.O#.OO#OO..#.#OO#......#.O.O#..#..OO..#O.O.....O.#..........##..O#.OO.....#...
|
||||
.O#..#.....O.#..O.....#O...#....#.O.OO....O...#.#..O......#.O...OO..#.O...O#......##O.OO...#....O.##
|
||||
O#O......O.O....O.O#OO.......OOO..#..#..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..OO.....#........OO.#......O.O....O..#.O#.#.O.#....OO.O#..OO.....#.##.####.O##...O.O.#
|
||||
...O..##O....#....O.O.....#......OO..#OO...#O#..O.......O.#.O.O...O.#.O......#...O...#O...#..##.O...
|
||||
OOO..#.#....#..#.O.#O#......O......OO.......O..#..#.O#.....O.......#.OO.O.O.......#.O#.O............
|
||||
.O##O.....OO..O..O....#.O...#.#.#O.#.O#.O.#...##O..OO.O#......O.OO..#O.OO..O.O...O.O#......#...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..#..OO...#...#.OOO......#.#.....O##.....#.OOO...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.#...#..OO.......#.....#O.##O..
|
||||
......#O.........#..O#..#O....O.#..#.O...OO...O....###....O##O..O.O...O..#O.....O.#.......#...#..#..
|
||||
#..#..#.OO##....O.###.#.....#O#.O....O.#.O......O....#.O..OO....#...O#.O#.....O#O..#.O..OO...OOO.O.#
|
||||
.#....O.......#O..##.#OO#....OO#....#..OO..#.....O.O.OO.O#OO##...O#..#..#..OO..OO.#..O..........O..#
|
||||
.O...O#..OO.O..O.#.O..##.O...O.O.....#O.....O.O#..OO...#.O....#O..#...##..#.#...O.O..OO#O...O..#...O
|
||||
#O.##...O...OO.##.O.....O.......##..#.###...#.O..#.....#O.#....O......#.O#.#.O...O.....OOO.O.....O#O
|
||||
...O.#....#....#..OO...#...##..O.O.O......O.#..O..OO....O...#..OO......O.#.O.OOOO...O.#OOOOO.OO#.#..
|
||||
.......O#.O#.O..O.#.O..#.OO.O..#.#.##.....O.#.O##...##..O...O...O.O.......#O.#......#......O.......#
|
||||
...#..#O.......#O...O..OO....OOOO......OO...#....O............#.........#O...#.#.......OO.....O..#O.
|
||||
O...OO###.....O.OO......#O.O...O..#..##..O..O...O#.#......O#O.O.OO.#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#...OO.....OO.......#
|
||||
O.O..O...#O#...#.#....#......OO..O#...O..OOO..O..O.....#...#...O.......#.....#O..O.....O#..O.O...O.#
|
||||
...##..OOO..OO#.O#OOO.#......O...O.......##........OO..O.#....O....##..##O#...#.#..O.#O........#.O..
|
||||
.......##O#........O......O......#.............O..O..#.O....#.#OO##...#O...O.O.O#.#......##..O.##O#.
|
||||
OO.OO##.O...O#..O..O#...#.###....##.O...O.OO.#..#.O.#...#...##...#.....O..O..O.O.#..#..O....#....##.
|
||||
O.#...##...#.#OO..##...#......O.OO.#O#O......OO...O#OOO...#...O.............OO.O.O.#..#O......#.O.#.
|
||||
.O.O.....O....#.......O......#O.O..##.O#...O....#O....O..#.OO..O#.O.#.....O.O##...........O#..#O...#
|
||||
....#OO....#..###..O.O#O......##.#.#.O#O.#.##........##.O...#.OO.......O......O.....#.#O.#.O..#...O#
|
||||
.....O..#..#...#.###.##....#O.O.O..O....OOO.##...O##..O.#...##..#..O......##....#.O....#O........#.#
|
||||
...#OO#......O...O#.#..........OOO...#.##O.O..##..#.#.O......O#.....OO......O.#O..###..O...OOO..O..O
|
||||
.#.O.#.O.O.O.O.#..OO.O...OO...O..O........OO.........#.....##.....O#..#...#.#.#.O...O.......O#...OO.
|
||||
.....OO.O....O....O.#..##OO...#O#.......O....#....O.O..........O##.#...O..O.#..#..O....OO...#.......
|
||||
....##...O.#.....##O...##.....O..O...#.##.O##...O....O..O...##.....O....#.OO...O..OO#.O....OO......O
|
||||
.O....#.....O.#O.#OO.O.##..O.....O..O...#.##.O##...O...O#OO#.O.OO...O.O.#O#....#.O#.O...O....O......
|
||||
..O#O#.#.#.#....##.#O.O.O....#.##O..#O..O..O....O.....O.O..#...OO#O...#OO...#O..O...OO#O#...#..#....
|
||||
.#O......O.O.O#..OO.OOO.#......O...#.O......OOO#....O.#..O.......O.......#.....#....#O.....O#O....O.
|
||||
.....O..O..O#........#............O....O#OO.O..O.#O..O.OO...OO....O.#.O..#.OO.......O.O.OO..OO.##O..
|
||||
..O#O......#.............#..#.O.O.........O.#OO.OO..##.O#O........#.....O..OO...O.##.##.O.O#....OO.O
|
||||
O..#..#....#.....#O.O..O.O..O.#.O.O.#OO..O##O..#.#.O...O....OOO.O.#...........#...#...#O...#O.#.O...
|
||||
OO.....O#..#.#...#...#...##..O...O.....#O..O#O..OO.....##.#.#.OO.....O##O.....#O.O.#.#...OO...O#.#..
|
||||
....#...O#..O.OO#........O..#..O....#.O.....OO#..#....O..OO.........O..#..#...O...OO.......O.......#
|
||||
##.O#.........O#.OO..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.OO.O#O.#..#.#..O..
|
||||
...O#.......O#......O#........O..O..#..O#.O...O#..#...O......#...#.#O....O..#.#....OOO.O.......O....
|
||||
..O....#.....O.##..O..##O....#O..O.###.....O#..OOO..##........#O.O..O.....#......#O#.........#OO..#.
|
||||
#.##...#.#...#.....#O.O.#..##.O..O.O.....#.#OO.........O.#...O....#......##.#.OOOO.O.OO...O.OO.O....
|
||||
..#....#...OO..#O.....#..#O..##....#......#O.#..O.#OO.......OO.O....#.......OO..#.#..OO...O.....#OO.
|
||||
O.....O.#.....#..#.O#......#O#...#....O......O#O.O#.O....#.OO##........O...O#..O#.OOOO.#O#O..##....#
|
||||
.O........OO.#.....O#O..OO.O#O#..O.......#.O..##.#.#.....#.OOOO...O..OOO.OO#....O....#....O.#....OOO
|
||||
.O#O.O.......O#O...O.#O...O.OO#O..O.#..#O...O.O.#.#OO....##..##..#......#..O#......#O.O.O#O#.#...#..
|
||||
..O..O#.#..O#.O..OOOO...O..OOO#O......##.O..O..O..OO#O.#....O.......#..O#.O..O..#..O...OO.....##O#..
|
||||
...OOO..O........OOO....#..#.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.#OO.#..#O...O...O..#O...#...O#..O.............#.....O.#O....#..#...#.#...OO.O.OOO.O....##O#O.#
|
||||
OO..O...O....##...#.O...#.#....O.#....O..O##..O.......#O.#...O..##...OO.O#O.....OO...#....#.......O.
|
||||
.#O.O....#.#O........#.#..O....O.......#..O#.....O..O.O..##..O.O.O...O..O...OO......O.OO...#.#....OO
|
||||
.#.O..#O.....OO..O..#.O#.O.OO.#....#O....#.OO.....O.OO..#.O.O..OO.....O..##..O..#.#..O......O..OO..O
|
||||
.O.......OO#.#.O.OO..#OO.OO..#O..O##O..#......#.#O.O.#O..#.O.#..#O.OOO..OO...#.#.#.#.OO.O...#O##O...
|
||||
O#...O.O.O........O#..OO.#.#.....#..O##....#.#...OO##..#..O..#..O.OO.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.O##....O........OO#O..O..#.......O.#OOOO........#O...#
|
||||
O...#...#.O...#....O#O#......O#.OO#..#....#.#O...O#...OO##...O#.OO....####.......#.......O.........#
|
||||
#O..OO.OO....#.....#....#O.#..OO.#....##.....#...OO.O#O......O.O.O........##..O..O....#..O..O#.O....
|
||||
#O#.OOO.#O#.#.....#O.O....O.#.....OO...##....##O.#....O.O.#O.##O..#O#.O....O...O.#O.#.OO..#...OO..O.
|
||||
....O.........O.O.....#.#...O.O....O..#.#...#..#..#..OO..OO.#..O..O....O........O#.O.O.O#.....#O.O.#
|
||||
#...#O...#OOO...#..O.#..O..O...#.....OO#...##....#.##.##O.OOOO.#O.#...OO##.OO....#...OOO..###...O.##
|
||||
..........#...#.#.....#O...O#OO....OO#...#..##..O..O...#.#.#..#.......#...OO....#O.......O..O....O.O
|
||||
..O......#..OO..O#O.......#O...OO......#..O..OO..#.#O#OO..#.......OO#OO.O...#..O...O..##..OOOO.#...O
|
||||
..OO.O#.#....O#O#O#..O....##..O.O#.O.O..O...O....O...#..O...#O.O..O#.#O..........O.........O..O.O.#.
|
||||
...#....O#.#.#..OO....O.#.......#O#....OOOO.#O#O.O##.OO.#.#O##.....O.#OO#....O...#..OO#.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..#O...O......#......O...#....O...#.O.....O#.#..OO
|
||||
O..#.....#O..........#..OO........OOO.O..O#O#...#OO....O.#...OO..O..#....O.O.#.O#........#O.#.#...#O
|
||||
O...#..OOOO...#.....#........##..OOO#OO#..#..#.#.O.#.O.O......O.....#.#O#..##....O.#O#O.O.#...O.....
|
||||
.O.#.O....O.OOO..##.....O#.OO........................O..O..#.OOO......#OOO......#...OO........#..OOO
|
||||
.###...O##..#.O#..O#..#.O#.#.O..O.##..O...#.O..O.#.#.....OO..O.#O.O....OO.O.O..#....#O.OO#.O..O.....
|
||||
.......O.O.....O.....O#.....O.....OO#.O..#...O..O..##.O..OO.O....#O......###..#...O........#....O.OO
|
||||
O..O..O...O.O.O.O.O.O...OO........OO..OO..O.#O.#.O....#..OOO.O.#...OO.#...O......OO.OO#.#...O#...##.
|
||||
O#O.##...O#.#..O.#.O..#..#....#..O##...##....OO.#..##OO....##......O....#OO.O...#OO..OO#...O.OO.#.#.
|
||||
..O.#OOO....O....#....#..O#O..O..O...O...O.........#..#.#O..#.#...OO.....##.O.......O#..O#.#...OO...
|
||||
.O.O.O.O..O.##...#..........O.O..............#....#.#O...OO.#..OO...#....O.O..#.......O.#..#..##.O##
|
||||
..OO.#.....O..O.OO.#.##.O....#...#O....##..O#.O.....O.....#....OO#....##..#O..OO...#O#O#....#.O.O.O#
|
||||
..O.#O....O.#..#.#.OO.##..OO......#...#....#O...OO....#.O..#.OO#.......O.#.#......O#O...#..OO.....##
|
||||
#.........O....#..#O.....##....#O......##..O....OO.....O.....#..#O...O...O..#.OOO.O..#......#.....#.
|
||||
O.##.O.#OO...#..#O.OO.....#.#.....O..##.O.#......#O##......OO..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...#.OO....#..O.....O.......OOO#...##...OO.##....#O..#..#O.#.O#O..O...O.#..O.#O.O#...
|
300
14/src/main.rs
300
14/src/main.rs
@ -1,300 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::{Display, Write};
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader, Lines};
|
||||
use std::time::Instant;
|
||||
|
||||
// BOILERPLATE
|
||||
type InputIter = Lines<BufReader<File>>;
|
||||
|
||||
fn get_input() -> InputIter {
|
||||
let f = File::open("input").unwrap();
|
||||
let br = BufReader::new(f);
|
||||
br.lines()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let start = Instant::now();
|
||||
let ans1 = problem1(get_input());
|
||||
let duration = start.elapsed();
|
||||
println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64());
|
||||
|
||||
let start = Instant::now();
|
||||
let ans2 = problem2(get_input());
|
||||
let duration = start.elapsed();
|
||||
println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64());
|
||||
}
|
||||
|
||||
// PARSE
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Platform {
|
||||
rows: Vec<Vec<char>>,
|
||||
}
|
||||
|
||||
impl<T: BufRead> From<Lines<T>> for Platform {
|
||||
fn from(lines: Lines<T>) -> Self {
|
||||
Self {
|
||||
rows: lines.map(|line| line.unwrap().chars().collect()).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Direction {
|
||||
North,
|
||||
South,
|
||||
East,
|
||||
West,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum Axis {
|
||||
Rows,
|
||||
Columns,
|
||||
}
|
||||
|
||||
impl Direction {
|
||||
fn reverse_direction(&self) -> bool {
|
||||
match self {
|
||||
Direction::North | Direction::West => false,
|
||||
Direction::South | Direction::East => true,
|
||||
}
|
||||
}
|
||||
fn axis(&self) -> Axis {
|
||||
match self {
|
||||
Direction::North | Direction::South => Axis::Columns,
|
||||
Direction::East | Direction::West => Axis::Rows,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Platform {
|
||||
fn width(&self) -> usize {
|
||||
self.rows[0].len()
|
||||
}
|
||||
fn height(&self) -> usize {
|
||||
self.col(0).len()
|
||||
}
|
||||
fn col(&self, col: usize) -> Vec<char> {
|
||||
self.rows.iter().map(|row| row[col]).collect()
|
||||
}
|
||||
fn replace_col(&mut self, idx: usize, col: Vec<char>) {
|
||||
for row_idx in 0..col.len() {
|
||||
self.rows[row_idx][idx] = col[row_idx];
|
||||
}
|
||||
}
|
||||
fn roll(&mut self, dir: &Direction) {
|
||||
match dir.axis() {
|
||||
Axis::Columns => self.roll_columns(&dir),
|
||||
Axis::Rows => self.roll_rows(&dir),
|
||||
}
|
||||
}
|
||||
fn score(&self, dir: &Direction) -> u64 {
|
||||
match dir.axis() {
|
||||
Axis::Columns => self.score_columns(dir),
|
||||
Axis::Rows => self.score_rows(dir),
|
||||
}
|
||||
}
|
||||
fn roll_columns(&mut self, dir: &Direction) {
|
||||
assert_eq!(dir.axis(), Axis::Columns);
|
||||
|
||||
for col in 0..self.width() {
|
||||
self.roll_column(col, dir);
|
||||
}
|
||||
}
|
||||
// This code could definitely be reused for rows
|
||||
fn roll_column(&mut self, idx: usize, dir: &Direction) {
|
||||
let mut col = self.col(idx);
|
||||
if dir.reverse_direction() {
|
||||
for col_idx in (0..col.len()).rev() {
|
||||
match col[col_idx] {
|
||||
'.' | '#' => continue,
|
||||
'O' if col_idx == col.len() => continue,
|
||||
'O' => {
|
||||
// Find the first # rock or the edge of the map, we can't look beyond this for a resting position
|
||||
let upper_limit = (col_idx..col.len())
|
||||
.find(|c| col[*c] == '#')
|
||||
.unwrap_or(col.len());
|
||||
|
||||
if let Some(empty_pos) =
|
||||
(col_idx..upper_limit).filter(|i| col[*i] == '.').last()
|
||||
{
|
||||
col.swap(col_idx, empty_pos);
|
||||
}
|
||||
}
|
||||
_ => panic!("invalid character"),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for col_idx in 0..col.len() {
|
||||
match col[col_idx] {
|
||||
'.' | '#' => continue,
|
||||
'O' if col_idx == 0 => continue,
|
||||
'O' => {
|
||||
// Find the first # rock or the edge of the map, we can't look beyond this for a resting position
|
||||
let lower_limit = (0..col_idx).rev().find(|c| col[*c] == '#').unwrap_or(0);
|
||||
|
||||
if let Some(empty_pos) = (lower_limit..col_idx)
|
||||
.rev()
|
||||
.filter(|i| col[*i] == '.')
|
||||
.last()
|
||||
{
|
||||
col.swap(col_idx, empty_pos);
|
||||
}
|
||||
}
|
||||
_ => panic!("invalid character"),
|
||||
}
|
||||
}
|
||||
}
|
||||
self.replace_col(idx, col);
|
||||
}
|
||||
fn score_columns(&self, dir: &Direction) -> u64 {
|
||||
// TODO: implement reverse direction - this was not required for the problem
|
||||
(0..self.rows.len())
|
||||
.map(|row| {
|
||||
let row_score = self.rows.len() - row;
|
||||
self.rows[row].iter().filter(|c| **c == 'O').count() * row_score
|
||||
})
|
||||
.sum::<usize>() as u64
|
||||
}
|
||||
fn roll_rows(&mut self, dir: &Direction) {
|
||||
assert_eq!(dir.axis(), Axis::Rows);
|
||||
|
||||
for row in 0..self.height() {
|
||||
self.roll_row(row, dir);
|
||||
}
|
||||
}
|
||||
fn roll_row(&mut self, idx: usize, dir: &Direction) {
|
||||
let mut row = self.rows[idx].clone();
|
||||
if dir.reverse_direction() {
|
||||
for row_idx in (0..row.len()).rev() {
|
||||
match row[row_idx] {
|
||||
'.' | '#' => continue,
|
||||
'O' if row_idx == row.len() => continue,
|
||||
'O' => {
|
||||
// Find the first # rock or the edge of the map, we can't look beyond this for a resting position
|
||||
let upper_limit = (row_idx..row.len())
|
||||
.find(|c| row[*c] == '#')
|
||||
.unwrap_or(row.len());
|
||||
|
||||
if let Some(empty_pos) =
|
||||
(row_idx..upper_limit).filter(|i| row[*i] == '.').last()
|
||||
{
|
||||
row.swap(row_idx, empty_pos);
|
||||
}
|
||||
}
|
||||
_ => panic!("invalid character"),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for row_idx in 0..row.len() {
|
||||
match row[row_idx] {
|
||||
'.' | '#' => continue,
|
||||
'O' if row_idx == 0 => continue,
|
||||
'O' => {
|
||||
// Find the first # rock or the edge of the map, we can't look beyond this for a resting position
|
||||
let lower_limit = (0..row_idx).rev().find(|c| row[*c] == '#').unwrap_or(0);
|
||||
|
||||
if let Some(empty_pos) = (lower_limit..row_idx)
|
||||
.rev()
|
||||
.filter(|i| row[*i] == '.')
|
||||
.last()
|
||||
{
|
||||
row.swap(row_idx, empty_pos);
|
||||
}
|
||||
}
|
||||
_ => panic!("invalid character"),
|
||||
}
|
||||
}
|
||||
}
|
||||
self.rows[idx] = row;
|
||||
}
|
||||
|
||||
fn roll_cycle(&mut self) {
|
||||
self.roll(&Direction::North);
|
||||
self.roll(&Direction::West);
|
||||
self.roll(&Direction::South);
|
||||
self.roll(&Direction::East);
|
||||
}
|
||||
|
||||
// find the first loop, return the iteration count when we first saw it and when we saw it again
|
||||
fn find_loop(&mut self) -> (usize, usize) {
|
||||
let mut first_seen: HashMap<Vec<Vec<char>>, usize> = HashMap::new();
|
||||
first_seen.insert(self.rows.clone(), 0);
|
||||
let mut i = 0;
|
||||
loop {
|
||||
self.roll_cycle();
|
||||
i += 1;
|
||||
if let Some(first_idx) = first_seen.insert(self.rows.clone(), i) {
|
||||
return (first_idx, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
fn score_rows(&self, dir: &Direction) -> u64 {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Platform {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for row in &self.rows {
|
||||
for c in row {
|
||||
f.write_char(*c)?;
|
||||
}
|
||||
writeln!(f)?;
|
||||
}
|
||||
writeln!(f)
|
||||
}
|
||||
}
|
||||
|
||||
// PROBLEM 1 solution
|
||||
|
||||
fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
|
||||
let mut p = Platform::from(input);
|
||||
p.roll(&Direction::North);
|
||||
p.score(&Direction::North)
|
||||
}
|
||||
|
||||
// PROBLEM 2 solution
|
||||
fn problem2<T: BufRead>(input: Lines<T>) -> u64 {
|
||||
const ITERATIONS: usize = 1000000000;
|
||||
let mut p = Platform::from(input);
|
||||
let first_loop = p.find_loop();
|
||||
let loop_length = first_loop.1 - first_loop.0;
|
||||
let cycles_to_skip = ((ITERATIONS - first_loop.0) / loop_length) * loop_length;
|
||||
let iterations_remaining = ITERATIONS - first_loop.0 - cycles_to_skip;
|
||||
for _ in 0..iterations_remaining {
|
||||
p.roll_cycle();
|
||||
}
|
||||
|
||||
p.score(&Direction::North)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::*;
|
||||
use std::io::Cursor;
|
||||
|
||||
const EXAMPLE: &str = &"O....#....
|
||||
O.OO#....#
|
||||
.....##...
|
||||
OO.#O....O
|
||||
.O.....O#.
|
||||
O.#..O.#.#
|
||||
..O..#O..O
|
||||
.......O..
|
||||
#....###..
|
||||
#OO..#....";
|
||||
|
||||
#[test]
|
||||
fn problem1_example() {
|
||||
let c = Cursor::new(EXAMPLE);
|
||||
assert_eq!(problem1(c.lines()), 136);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn problem2_example() {
|
||||
let c = Cursor::new(EXAMPLE);
|
||||
assert_eq!(problem2(c.lines()), 64);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user