Skip to content

Commit e38aaef

Browse files
committed
day21: done
1 parent 1b7d488 commit e38aaef

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ members = [
4040
"day19pt2",
4141
"day20",
4242
"day20pt2",
43+
"day21",
4344
]
4445

4546
[profile.dev]

day21/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "day21"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

day21/src/main.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::collections::HashSet;
2+
3+
fn take_step(
4+
garden: &HashSet<(usize, usize)>,
5+
w: usize,
6+
h: usize,
7+
positions: &HashSet<(usize, usize)>,
8+
) -> HashSet<(usize, usize)> {
9+
let mut new_positions: HashSet<(usize, usize)> = HashSet::new();
10+
for pos in positions {
11+
if pos.0 > 0 && !garden.contains(&(pos.0 - 1, pos.1)) {
12+
new_positions.insert((pos.0 - 1, pos.1));
13+
}
14+
if pos.0 < w - 1 && !garden.contains(&(pos.0 + 1, pos.1)) {
15+
new_positions.insert((pos.0 + 1, pos.1));
16+
}
17+
if pos.1 > 0 && !garden.contains(&(pos.0, pos.1 - 1)) {
18+
new_positions.insert((pos.0, pos.1 - 1));
19+
}
20+
if pos.1 < h - 1 && !garden.contains(&(pos.0, pos.1 + 1)) {
21+
new_positions.insert((pos.0, pos.1 + 1));
22+
}
23+
}
24+
new_positions
25+
}
26+
27+
fn main() {
28+
let mut garden: HashSet<(usize, usize)> = HashSet::new();
29+
let mut positions: HashSet<(usize, usize)> = HashSet::new();
30+
let mut garden_width: usize = 0;
31+
let mut garden_height: usize = 0;
32+
for (y, line) in std::io::stdin().lines().map(Result::unwrap).enumerate() {
33+
garden_height += 1;
34+
for (x, c) in line.chars().enumerate() {
35+
if garden_height == 1 {
36+
garden_width += 1;
37+
}
38+
if c == '#' {
39+
garden.insert((x, y));
40+
}
41+
if c == 'S' {
42+
positions.insert((x, y));
43+
}
44+
}
45+
}
46+
for _ in 0..64 {
47+
positions = take_step(&garden, garden_width, garden_height, &positions);
48+
}
49+
println!("{}", positions.len());
50+
}

0 commit comments

Comments
 (0)