Skip to content

Commit

Permalink
Solve day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
ricohageman committed Dec 11, 2023
1 parent f48f89f commit bed70dc
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 12 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `70.7µs` | `851.5µs` |
| [Day 2](./src/bin/02.rs) | `38.6µs` | `48.7µs` |
| [Day 3](./src/bin/03.rs) | `232.8µs` | `246.2µs` |
| [Day 4](./src/bin/04.rs) | `202.3µs` | `195.7µs` |
| [Day 5](./src/bin/05.rs) | `21.1µs` | `74.4µs` |
| [Day 6](./src/bin/06.rs) | `654.0ns` | `504.0ns` |
| [Day 7](./src/bin/07.rs) | `295.7µs` | `287.5µs` |
| [Day 8](./src/bin/08.rs) | `150.2µs` | `496.9µs` |
| [Day 9](./src/bin/09.rs) | `171.6µs` | `167.1µs` |
| [Day 10](./src/bin/10.rs) | `250.2µs` | `258.5µs` |

**Total: 4.06ms**
| [Day 1](./src/bin/01.rs) | `70.8µs` | `842.1µs` |
| [Day 2](./src/bin/02.rs) | `38.9µs` | `48.4µs` |
| [Day 3](./src/bin/03.rs) | `229.2µs` | `245.5µs` |
| [Day 4](./src/bin/04.rs) | `198.9µs` | `194.8µs` |
| [Day 5](./src/bin/05.rs) | `21.0µs` | `74.1µs` |
| [Day 6](./src/bin/06.rs) | `396.0ns` | `335.0ns` |
| [Day 7](./src/bin/07.rs) | `290.3µs` | `301.8µs` |
| [Day 8](./src/bin/08.rs) | `158.9µs` | `500.5µs` |
| [Day 9](./src/bin/09.rs) | `172.2µs` | `166.7µs` |
| [Day 10](./src/bin/10.rs) | `254.4µs` | `257.7µs` |
| [Day 11](./src/bin/11.rs) | `504.5µs` | `492.8µs` |

**Total: 5.06ms**
<!--- benchmarking table --->

---
Expand Down
10 changes: 10 additions & 0 deletions data/examples/11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
71 changes: 71 additions & 0 deletions src/bin/11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use itertools::Itertools;
use rustc_hash::FxHashSet;
use std::cmp::{max, min};
advent_of_code::solution!(11);

fn solve(input: &str, expansion: isize) -> isize {
let galaxies: Vec<(isize, isize)> = input
.lines()
.enumerate()
.flat_map(|(y, line)| {
line.chars()
.enumerate()
.filter(|(_, char)| *char == '#')
.map(move |(x, _)| (x as isize, y as isize))
})
.collect();

let columns_with_galaxies: FxHashSet<isize> = galaxies.iter().map(|(x, _)| *x).collect();

let rows_with_galaxies: FxHashSet<isize> = galaxies.iter().map(|(_, y)| *y).collect();

let columns_without_galaxies: Vec<isize> = (0..*columns_with_galaxies.iter().max().unwrap())
.filter(|x| !columns_with_galaxies.contains(x))
.collect();

let rows_without_galaxies: Vec<isize> = (0..*rows_with_galaxies.iter().max().unwrap())
.filter(|y| !rows_with_galaxies.contains(y))
.collect();

galaxies
.iter()
.map(|(x, y)| {
let dx = columns_without_galaxies.iter().filter(|c| *c < x).count() as isize;

let dy = rows_without_galaxies.iter().filter(|c| *c < y).count() as isize;

((x, dx), (y, dy))
})
.tuple_combinations()
.map(|(((x1, dx1), (y1, dy1)), ((x2, dx2), (y2, dy2)))| {
(x1 - x2).abs()
+ (y1 - y2).abs()
+ ((dx1 - dx2).abs() + (dy1 - dy2).abs()) * (expansion - 1)
})
.sum()
}

pub fn part_one(input: &str) -> Option<isize> {
Some(solve(input, 2))
}

pub fn part_two(input: &str) -> Option<isize> {
Some(solve(input, 1_000_000))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_solve() {
let result = solve(&advent_of_code::template::read_file("examples", DAY), 2);
assert_eq!(result, 374);

let result = solve(&advent_of_code::template::read_file("examples", DAY), 10);
assert_eq!(result, 1030);

let result = solve(&advent_of_code::template::read_file("examples", DAY), 100);
assert_eq!(result, 8410);
}
}

0 comments on commit bed70dc

Please sign in to comment.