Compare commits

...

3 commits

Author SHA1 Message Date
f458030577 day 12 part 2 2023-12-15 13:18:24 -05:00
92b37dbfce day 12 part 1 2023-12-15 13:17:29 -05:00
68c0f3c3a3 day 12 part 1 (dirty) 2023-12-15 12:59:27 -05:00
3 changed files with 1110 additions and 201 deletions

View file

@ -1,10 +1,6 @@
...#...... ???.### 1,1,3
.......#.. .??..??...?##. 1,1,3
#......... ?#?#?#?#?#?#?#? 1,3,1,6
.......... ????.#...#... 4,1,1
......#... ????.######..#####. 1,6,5
.#........ ?###???????? 3,2,1
.........#
..........
.......#..
#...#.....

1140
input.txt

File diff suppressed because it is too large Load diff

View file

@ -1,65 +1,118 @@
use std::{cmp::max, collections::HashSet, io::stdin}; use std::{collections::HashMap, io::stdin};
use anyhow::{bail, Result}; use anyhow::Result;
fn main() -> Result<()> { fn matches(conditions: &str, group: &str) -> bool {
let mut grid = vec![]; for (g, c) in group.chars().zip(conditions.chars()) {
let mut galaxies = HashSet::new(); if c == '?' {
let mut occupied_columns = HashSet::new(); continue;
let mut occupied_rows = HashSet::new(); }
let mut max_x = 0; if g == c {
let mut max_y = 0; continue;
for (y, line) in stdin().lines().map_while(Result::ok).enumerate() { }
let cells: Vec<char> = line.chars().collect(); return false;
max_y = max(max_y, y); }
for (x, c) in cells.iter().enumerate() { true
max_x = max(max_x, x); }
if *c == '#' {
galaxies.insert((x, y)); type Cache = HashMap<String, i64>;
occupied_columns.insert(x);
occupied_rows.insert(y); fn arrangements_inner(conditions: &str, groups: &[String], cache: &mut Cache) -> i64 {
let g = &groups[0];
let w = conditions.len() - groups.iter().skip(1).map(|s| s.len()).sum::<usize>();
let positions = w - g.len() + 1;
let mut sum = 0;
for i in 0..positions {
if !matches(&conditions[i..], g) {
continue;
}
if conditions[..i].contains('#') {
continue;
}
let after = if (i + g.len()) < conditions.len() {
conditions.chars().nth(i + g.len()).unwrap()
} else {
'.'
};
if after == '#' {
continue;
}
if groups.len() == 1 {
if conditions[i + g.len()..].contains('#') {
continue;
}
sum += 1;
} else {
let cache_key = format!("{},{:?}", &conditions[i + g.len()..], &groups[1..]);
if let Some(v) = cache.get(&cache_key) {
sum += v;
} else {
let a = arrangements_inner(&conditions[i + g.len()..], &groups[1..], cache);
cache.insert(cache_key, a);
sum += a;
} }
} }
grid.push(cells);
} }
sum
}
let mut empty_columns: Vec<usize> = HashSet::from_iter(0..max_x) fn arrangements(conditions: &str, groups: &[usize]) -> i64 {
.difference(&occupied_columns) let mut group_strings = vec![];
.copied() group_strings.push("#".repeat(groups[0]).to_owned());
.collect(); for g in groups.iter().skip(1) {
let mut empty_rows: Vec<usize> = HashSet::from_iter(0..max_y) group_strings.push(".".to_owned() + &"#".to_owned().repeat(*g));
.difference(&occupied_rows)
.copied()
.collect();
empty_columns.sort();
empty_rows.sort();
let mut expanded_galaxies = HashSet::new();
let expansion = 1_000_000;
for (x, y) in galaxies.into_iter() {
let Err(col) = empty_columns.binary_search(&x) else {
bail!("x={x} found in empty columns");
};
let new_x = x + col * (expansion - 1);
let Err(row) = empty_rows.binary_search(&y) else {
bail!("y={y} found in empty rows");
};
let new_y = y + row * (expansion - 1);
expanded_galaxies.insert((new_x, new_y));
} }
let mut cache: Cache = Cache::new();
arrangements_inner(conditions, &group_strings, &mut cache)
}
fn main() -> Result<()> {
let mut sum = 0; let mut sum = 0;
for a in expanded_galaxies.iter() { for line in stdin().lines().map_while(Result::ok) {
for b in expanded_galaxies.iter() { let mut split = line.split_whitespace();
let dx = i64::abs(a.0 as i64 - b.0 as i64); let conditions = split.next().unwrap();
let dy = i64::abs(a.1 as i64 - b.1 as i64); let groups: Vec<usize> = split
// println!("{a:?} -> {b:?} = {}", dx + dy); .next()
sum += dx + dy; .unwrap()
} .split(',')
.map(str::parse)
.map_while(Result::ok)
.collect();
let conditions = [conditions].repeat(5).join("?");
let groups = groups.repeat(5);
let a = arrangements(&conditions, &groups);
sum += a;
} }
sum /= 2;
println!("{sum}"); println!("{sum}");
Ok(()) Ok(())
} }
#[cfg(test)] #[cfg(test)]
mod tests {} mod tests {
use super::*;
#[test]
fn test_arrangements() {
assert_eq!(arrangements(".??.?#??##???.", &[1, 6]), 5);
assert_eq!(arrangements("???.###", &[1, 1, 3]), 1);
assert_eq!(arrangements(".??..??...?##.", &[1, 1, 3]), 4);
assert_eq!(arrangements("?#?#?#?#?#?#?#?", &[1, 3, 1, 6]), 1);
assert_eq!(arrangements("????.#...#...", &[4, 1, 1]), 1);
assert_eq!(arrangements("????.######..#####.", &[1, 6, 5]), 4);
assert_eq!(arrangements("?###????????", &[3, 2, 1]), 10);
assert_eq!(arrangements("????#?#?..?#?", &[4, 1]), 2);
}
#[test]
fn test_matches() {
assert!(matches("?", "#"));
assert!(matches("?", "."));
assert!(matches("#", "#"));
assert!(matches(".", "."));
assert!(!matches("#", "."));
assert!(!matches(".", "#"));
}
}