day 14 part 1
This commit is contained in:
parent
df59ba47a8
commit
f91c30e11d
3 changed files with 133 additions and 1442 deletions
25
example.txt
25
example.txt
|
@ -1,15 +1,10 @@
|
|||
#.##..##.
|
||||
..#.##.#.
|
||||
##......#
|
||||
##......#
|
||||
..#.##.#.
|
||||
..##..##.
|
||||
#.#.##.#.
|
||||
|
||||
#...##..#
|
||||
#....#..#
|
||||
..##..###
|
||||
#####.##.
|
||||
#####.##.
|
||||
..##..###
|
||||
#....#..#
|
||||
O....#....
|
||||
O.OO#....#
|
||||
.....##...
|
||||
OO.#O....O
|
||||
.O.....O#.
|
||||
O.#..O.#.#
|
||||
..O..#O..O
|
||||
.......O..
|
||||
#....###..
|
||||
#OO..#....
|
||||
|
|
79
src/main.rs
79
src/main.rs
|
@ -1,72 +1,35 @@
|
|||
use anyhow::Result;
|
||||
use std::{collections::HashSet, io::stdin, vec};
|
||||
use std::io::stdin;
|
||||
|
||||
type Grid = Vec<Vec<char>>;
|
||||
|
||||
fn vertical_mirror_points(grid: &Grid) -> HashSet<usize> {
|
||||
let mut s = HashSet::new();
|
||||
for x in 1..grid[0].len() {
|
||||
let mut differences = 0;
|
||||
for row in grid {
|
||||
for i in 0..x {
|
||||
if x + i >= row.len() {
|
||||
continue;
|
||||
}
|
||||
if row[x - i - 1] != row[x + i] {
|
||||
differences += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if differences == 1 {
|
||||
s.insert(x);
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn horizontal_mirror_points(grid: &Grid) -> HashSet<usize> {
|
||||
let mut s = HashSet::new();
|
||||
for y in 1..grid.len() {
|
||||
let mut differences = 0;
|
||||
for x in 0..grid[0].len() {
|
||||
for i in 0..y {
|
||||
if y + i >= grid.len() {
|
||||
continue;
|
||||
}
|
||||
if grid[y - i - 1][x] != grid[y + i][x] {
|
||||
differences += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if differences == 1 {
|
||||
s.insert(y);
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut grids: Vec<Grid> = vec![];
|
||||
grids.push(vec![]);
|
||||
let mut grid: Grid = vec![];
|
||||
for line in stdin().lines().map_while(Result::ok) {
|
||||
if line.is_empty() {
|
||||
grids.push(vec![]);
|
||||
continue;
|
||||
grid.push(line.chars().collect());
|
||||
}
|
||||
grids.last_mut().unwrap().push(line.chars().collect());
|
||||
|
||||
let height = grid.len();
|
||||
let width = grid[0].len();
|
||||
let mut total_load = 0;
|
||||
for x in 0..width {
|
||||
let mut column_load = 0;
|
||||
let mut load = height;
|
||||
for (y, row) in grid.iter().enumerate() {
|
||||
match row[x] {
|
||||
'O' => {
|
||||
column_load += load;
|
||||
load -= 1;
|
||||
}
|
||||
let mut sum = 0;
|
||||
for (i, grid) in grids.iter().enumerate() {
|
||||
for v in vertical_mirror_points(grid) {
|
||||
println!("grid {i}: v = {v}");
|
||||
sum += v;
|
||||
'#' => {
|
||||
load = height - y - 1;
|
||||
}
|
||||
for h in horizontal_mirror_points(grid) {
|
||||
println!("grid {i}: h = {h}");
|
||||
sum += 100 * h;
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
println!("{sum}");
|
||||
total_load += column_load;
|
||||
}
|
||||
println!("{total_load}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue