Advent 2023 Day 11

This commit is contained in:
Jos van Goor 2023-12-12 21:40:46 +01:00
parent c7bd0f6e10
commit 7865e624c5
3 changed files with 301 additions and 0 deletions

View file

@ -0,0 +1,73 @@
use std::fs::read_to_string;
fn difference(lhs: usize, rhs: usize) -> usize {
if lhs > rhs {
return lhs - rhs;
}
rhs - lhs
}
fn main() {
let input = read_to_string("input.txt").unwrap();
let mut starmap = input.lines().map(|line| {
let mut vec: Vec<u8> = Vec::new();
vec.extend_from_slice(line.as_bytes());
vec
}).collect::<Vec<_>>();
let mut index = 0;
loop {
if index == starmap.len() {
break;
}
if starmap[index].iter().filter(|ch| { **ch == b'#' }).count() == 0 {
starmap.insert(index, starmap[index].clone());
index += 1;
}
index += 1;
}
index = 0;
loop {
if index >= starmap[0].len() {
break;
}
let mut sum = 0;
for line in &starmap {
if line[index] == b'#' {
sum += 1;
}
}
if sum == 0 {
for line in &mut starmap {
line.insert(index, b'.');
}
index += 1;
}
index += 1;
}
let mut stars: Vec<(usize, usize)> = Vec::new();
for row in 0..starmap.len() {
for col in 0..starmap[row].len() {
if starmap[row][col] == b'#' {
stars.push((row, col));
}
}
}
let mut sum_difference = 0;
for start_idx in 0..stars.len() {
for inner_idx in start_idx..stars.len() {
sum_difference += difference(stars[start_idx].0, stars[inner_idx].0);
sum_difference += difference(stars[start_idx].1, stars[inner_idx].1);
}
}
println!("Sum of ranges: {}", sum_difference);
}