Skip to content

Commit

Permalink
[2023] Complete day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 11, 2023
1 parent c9bffcf commit 25d0063
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
- [Day 08: Haunted Wasteland](aoc_2023/src/day_08.rs)
- [Day 09: Mirage Maintenance](aoc_2023/src/day_09.rs)
- [Day 10: Pipe Maze](aoc_2023/src/day_10.rs)
- [Day 11: Cosmic Expansion](aoc_2023/src/day_11.rs)
<!-- MARKER -->

## [2022](https://adventofcode.com/2022) [![aoc_2022](https://github.com/Basicprogrammer10/advent-of-code/actions/workflows/aoc_2022.yml/badge.svg)](https://github.com/Basicprogrammer10/advent-of-code/actions/workflows/aoc_2022.yml)
Expand Down
123 changes: 123 additions & 0 deletions aoc_2023/src/day_11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use itertools::Itertools;
use nd_vec::{vector, Vec2};

use common::{Answer, Solution};

type Pos = Vec2<usize>;

pub struct Day11;

impl Solution for Day11 {
fn name(&self) -> &'static str {
"Cosmic Expansion"
}

fn part_a(&self, input: &str) -> Answer {
let mut galaxies = parse(input);
galaxies.expand(2);
galaxies.total_distance().into()
}

fn part_b(&self, input: &str) -> Answer {
let mut galaxies = parse(input);
galaxies.expand(1000000);
galaxies.total_distance().into()
}
}

struct Galaxies {
galaxies: Vec<Pos>,
rows: Vec<bool>,
cols: Vec<bool>,
}

fn parse(input: &str) -> Galaxies {
let mut galaxies = Vec::new();
let (height, width) = (input.lines().count(), input.lines().next().unwrap().len());

let mut rows = vec![false; height];
let mut cols = vec![false; width];

for (y, line) in input.lines().enumerate() {
for (x, c) in line.chars().enumerate() {
if c == '#' {
galaxies.push(vector!(x, y));
rows[y] = true;
cols[x] = true;
}
}
}

Galaxies {
galaxies,
rows,
cols,
}
}

impl Galaxies {
fn expand(&mut self, mut multiplier: usize) {
multiplier -= 1;

for (y, _) in self.rows.iter().enumerate().rev().filter(|(_, &row)| !row) {
for pos in &mut self.galaxies {
if pos.y() > y {
*pos += vector!(0, multiplier)
}
}
}

for (x, _) in self.cols.iter().enumerate().rev().filter(|(_, &col)| !col) {
for pos in &mut self.galaxies {
if pos.x() > x {
*pos += vector!(multiplier, 0)
}
}
}
}

fn total_distance(&self) -> usize {
self.galaxies
.iter()
.combinations(2)
.map(|pair| {
let (a, b) = (pair[0], pair[1]);
let dist = (a.x() as isize - b.x() as isize).abs()
+ (a.y() as isize - b.y() as isize).abs();

dist as usize
})
.sum()
}
}

#[cfg(test)]
mod test {
use common::Solution;
use indoc::indoc;

use super::Day11;

const CASE: &str = indoc! {"
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
"};

#[test]
fn part_a() {
assert_eq!(Day11.part_a(CASE), 374.into());
}

#[test]
fn part_b() {
assert_eq!(Day11.part_b(CASE), 82000210.into());
}
}
2 changes: 2 additions & 0 deletions aoc_2023/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod day_07;
mod day_08;
mod day_09;
mod day_10;
mod day_11;
// [import_marker]

#[rustfmt::skip]
Expand All @@ -26,5 +27,6 @@ pub const ALL: &[&dyn Solution] = &[
&day_08::Day08,
&day_09::Day09,
&day_10::Day10,
&day_11::Day11,
// [list_marker]
];

0 comments on commit 25d0063

Please sign in to comment.