-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-11.rs
98 lines (88 loc) · 2.42 KB
/
day-11.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use aoc_2023::{aoc, str_block};
const INPUT: &str = include_str!("day-11.txt");
#[allow(dead_code)]
const INPUT_EX: &str = str_block! {"
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
"};
aoc! {
struct Day11 {
galaxies: Vec<(usize, usize)>,
exp_rows: Vec<usize>,
exp_cols: Vec<usize>,
}
self(input = INPUT) {
let mut galaxies = Vec::new();
let mut exp_rows = Vec::new();
let mut colacc = Vec::new();
for (y, line) in input.lines().enumerate() {
let line = line.as_bytes();
if colacc.len() < line.len() {
colacc.resize(line.len(), true);
}
let mut any_galaxies = false;
for x in line.iter().enumerate().filter_map(|(x, &b)| {
let has_galaxy = b == b'#';
if has_galaxy {
colacc[x] = false;
}
has_galaxy.then_some(x)
}) {
any_galaxies = true;
galaxies.push((x, y));
}
if !any_galaxies {
exp_rows.push(y);
}
}
let exp_cols = colacc.iter().enumerate().filter_map(|(x, c)| c.then_some(x)).collect();
Ok(Self {
galaxies,
exp_rows,
exp_cols,
})
}
1 part1 usize {
Ok(self.dist_all(1))
}
2 part2 usize {
Ok(self.dist_all(999_999))
}
INPUT_EX { 1 part1 = 374 }
INPUT { 1 part1 = 9536038, 2 part2 = 447744640566 }
}
impl Day11 {
fn dist_all(&self, exp: usize) -> usize {
self.galaxies[..self.galaxies.len() - 1]
.iter()
.enumerate()
.map(|(i, &ga)| {
self.galaxies[i + 1..]
.iter()
.map(|&gb| self.dist(ga, gb, exp))
.sum::<usize>()
})
.sum()
}
fn dist(&self, (ax, ay): (usize, usize), (bx, by): (usize, usize), exp: usize) -> usize {
self.dist_part(ax, bx, &self.exp_cols, exp) + self.dist_part(ay, by, &self.exp_rows, exp)
}
fn dist_part(&self, a: usize, b: usize, exps: &[usize], exp: usize) -> usize {
let (a, b) = (a.min(b), a.max(b));
let mut d = b - a;
for i in exps.iter() {
if (a..b).contains(i) {
d += exp;
}
}
d
}
}