Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bde379e

Browse files
authored
Merge pull request #85 from marvin-hsu/main
day11
2 parents cd2b4e4 + d4b20da commit bde379e

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

‎2023/11/marvinhsu/Cargo.toml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "day11"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
name = "day11"
8+
path = "src/lib.rs"
9+
test = true
10+
crate-type = ["lib"]
11+
doctest = false
12+
13+
[[bin]]
14+
name = "day11"
15+
path = "src/main.rs"
16+
test = false
17+
doctest = false
18+
19+
[dependencies]
20+
tap = "1.0.1"
21+
anyhow = "1.0.75"
22+
itertools = "0.12.0"

‎2023/11/marvinhsu/example‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
...#......
2+
.......#..
3+
#.........
4+
..........
5+
......#...
6+
.#........
7+
.........#
8+
..........
9+
.......#..
10+
#...#.....

‎2023/11/marvinhsu/input‎

Whitespace-only changes.

‎2023/11/marvinhsu/src/lib.rs‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use std::collections::HashSet;
2+
3+
use anyhow::{Context, Ok, Result};
4+
use itertools::Itertools;
5+
pub fn part1(input: &str) -> Result<usize> {
6+
let image = Image::parse_with_distance_factor(input, 2)?;
7+
let sum = sum_galaxies_distance(image);
8+
9+
Ok(sum)
10+
}
11+
12+
pub fn part2(input: &str) -> Result<usize> {
13+
let image = Image::parse_with_distance_factor(input, 1000000)?;
14+
let sum = sum_galaxies_distance(image);
15+
16+
Ok(sum)
17+
}
18+
19+
fn sum_galaxies_distance(image: Image) -> usize {
20+
image
21+
.galaxies
22+
.iter()
23+
.enumerate()
24+
.combinations(2)
25+
.map(|comb| image.get_distance(comb[0].0, comb[1].0))
26+
.sum()
27+
}
28+
29+
struct Image {
30+
galaxies: Vec<(usize, usize)>,
31+
}
32+
33+
impl Image {
34+
fn parse_with_distance_factor(input: &str, distance_factor: usize) -> Result<Self> {
35+
let distance_factor = distance_factor - 1;
36+
37+
let image = input
38+
.lines()
39+
.map(|line| line.chars().collect())
40+
.collect::<Vec<Vec<char>>>();
41+
42+
let mut galaxies = vec![];
43+
44+
let mut image_iter = image.iter().peekable();
45+
46+
let all_empty_column = image_iter
47+
.peek()
48+
.context("empty image")?
49+
.iter()
50+
.enumerate()
51+
.filter(|(column, _)| {
52+
(0..image.len())
53+
.map(|i| image[i][*column])
54+
.all(|c| c == '.')
55+
})
56+
.map(|(column, _)| column)
57+
.collect::<HashSet<usize>>();
58+
59+
let mut current_y = 0;
60+
for row in image_iter {
61+
let mut current_x = 0;
62+
for (x, c) in row.iter().enumerate() {
63+
if c == &'#' {
64+
galaxies.push((current_x, current_y));
65+
}
66+
67+
current_x += 1;
68+
69+
if all_empty_column.contains(&x){
70+
current_x += distance_factor;
71+
}
72+
}
73+
current_y += 1;
74+
75+
if row.iter().all(|c| *c == '.') {
76+
current_y += distance_factor;
77+
}
78+
}
79+
80+
Ok(Self { galaxies })
81+
}
82+
83+
fn get_distance(&self, source: usize, target: usize) -> usize {
84+
let (x1, y1) = self.galaxies[source];
85+
let (x2, y2) = self.galaxies[target];
86+
x1.max(x2) - x1.min(x2) + y1.max(y2) - y1.min(y2)
87+
}
88+
}
89+
90+
#[cfg(test)]
91+
mod tests {
92+
use super::*;
93+
94+
#[test]
95+
fn test_part1() {
96+
let input = include_str!("../example");
97+
assert_eq!(part1(input).unwrap(), 374);
98+
}
99+
100+
#[test]
101+
fn test_get_path() {
102+
let input = include_str!("../example");
103+
let image = Image::parse_with_distance_factor(input, 2).unwrap();
104+
105+
assert_eq!(image.get_distance(4, 8), 9);
106+
assert_eq!(image.get_distance(0, 6), 15);
107+
}
108+
109+
#[test]
110+
fn test_parse() {
111+
let input = include_str!("../example");
112+
let image = Image::parse_with_distance_factor(input, 10).unwrap();
113+
114+
assert_eq!(sum_galaxies_distance(image), 1030);
115+
116+
let image = Image::parse_with_distance_factor(input, 100).unwrap();
117+
assert_eq!(sum_galaxies_distance(image), 8410);
118+
}
119+
}

‎2023/11/marvinhsu/src/main.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use day11::{part1, part2};
2+
3+
fn main() {
4+
let input = include_str!("../input");
5+
println!("part1: {}", part1(input).unwrap());
6+
println!("part2: {}", part2(input).unwrap());
7+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /