tangled
alpha
login
or
join now
bwc9876.dev
/
advent
0
fork
atom
Advent of Code solutions
0
fork
atom
overview
issues
pulls
pipelines
Day 7
bwc9876.dev
3 months ago
62c99d3e
db044414
verified
This commit was signed with the committer's
known signature
.
bwc9876.dev
SSH Key Fingerprint:
SHA256:DanMEP/RNlSC7pAVbnXO6wzQV00rqyKj053tz4uH5gQ=
+115
-5
3 changed files
expand all
collapse all
unified
split
years
2025
src
day_7.rs
examples
day_7
1.txt
2.txt
+83
-5
years/2025/src/day_7.rs
···
1
1
+
use std::collections::{HashMap, HashSet, VecDeque};
1
2
2
3
use advent_core::{Day, day_stuff, ex_for_day};
4
4
+
use utils::{dir::Direction, pos::Position, tiles};
3
5
4
6
pub struct Day7;
5
7
8
8
+
tiles!(Tile, [
9
9
+
'S' => Start,
10
10
+
'^' => Splitter,
11
11
+
'.' => Empty,
12
12
+
]);
13
13
+
14
14
+
type Grid = utils::grid::Grid<Tile>;
15
15
+
6
16
impl Day for Day7 {
17
17
+
day_stuff!(7, "21", "40", Grid);
7
18
8
8
-
day_stuff!(7, "", "");
19
19
+
fn part_1(input: Self::Input) -> Option<String> {
20
20
+
let (start, _) = input.iter().find(|(_, t)| **t == Tile::Start).unwrap();
21
21
+
let mut queue = VecDeque::<Position>::new();
22
22
+
let mut seen = HashSet::<Position>::with_capacity(input.size().1);
23
23
+
24
24
+
queue.push_back(start);
25
25
+
26
26
+
let mut splits = 0;
9
27
10
10
-
fn part_1(_input: Self::Input) -> Option<String> {
11
11
-
None
28
28
+
while let Some(pos) = queue.pop_front() {
29
29
+
if seen.contains(&pos) {
30
30
+
continue;
31
31
+
}
32
32
+
seen.insert(pos);
33
33
+
let next_pos = Position::new(pos.x, pos.y + 1);
34
34
+
match input.get(next_pos) {
35
35
+
Some(Tile::Splitter) => {
36
36
+
splits += 1;
37
37
+
queue.push_back(Position::new(pos.x + 1, pos.y));
38
38
+
queue.push_back(Position::new(pos.x - 1, pos.y));
39
39
+
}
40
40
+
Some(Tile::Empty) => {
41
41
+
queue.push_back(next_pos);
42
42
+
}
43
43
+
_ => {}
44
44
+
}
45
45
+
}
46
46
+
47
47
+
Some(splits.to_string())
12
48
}
13
49
14
14
-
fn part_2(_input: Self::Input) -> Option<String> {
15
15
-
None
50
50
+
fn part_2(input: Self::Input) -> Option<String> {
51
51
+
let (start, _) = input.iter().find(|(_, t)| **t == Tile::Start).unwrap();
52
52
+
let mut queue = VecDeque::<Position>::new();
53
53
+
let mut seen = HashMap::<Position, usize>::new();
54
54
+
55
55
+
seen.insert(start, 1);
56
56
+
queue.push_front(start);
57
57
+
58
58
+
let mut splits = 0;
59
59
+
60
60
+
while let Some(pos) = queue.pop_front() {
61
61
+
let amnt = seen.get(&pos).copied().unwrap_or_default();
62
62
+
let next_pos = pos.move_dir(Direction::South);
63
63
+
match input.get(next_pos) {
64
64
+
Some(Tile::Splitter) => {
65
65
+
for (next_pos, _) in next_pos.relatives(&[Direction::West, Direction::East]) {
66
66
+
if let Some(curr) = seen.get_mut(&next_pos) {
67
67
+
*curr += amnt;
68
68
+
} else {
69
69
+
seen.insert(next_pos, amnt);
70
70
+
queue.push_back(next_pos);
71
71
+
}
72
72
+
}
73
73
+
}
74
74
+
Some(Tile::Empty) => {
75
75
+
if let Some(curr) = seen.get_mut(&next_pos) {
76
76
+
*curr += amnt;
77
77
+
} else {
78
78
+
seen.insert(next_pos, amnt);
79
79
+
queue.push_back(next_pos);
80
80
+
}
81
81
+
}
82
82
+
None => {
83
83
+
splits += amnt;
84
84
+
}
85
85
+
_ => {}
86
86
+
}
87
87
+
}
88
88
+
89
89
+
Some(splits.to_string())
90
90
+
}
91
91
+
92
92
+
fn parse_input(input: &str) -> Self::Input {
93
93
+
Grid::parse(input)
16
94
}
17
95
}
+16
years/2025/src/examples/day_7/1.txt
···
1
1
+
.......S.......
2
2
+
...............
3
3
+
.......^.......
4
4
+
...............
5
5
+
......^.^......
6
6
+
...............
7
7
+
.....^.^.^.....
8
8
+
...............
9
9
+
....^.^...^....
10
10
+
...............
11
11
+
...^.^...^.^...
12
12
+
...............
13
13
+
..^...^.....^..
14
14
+
...............
15
15
+
.^.^.^.^.^...^.
16
16
+
...............
+16
years/2025/src/examples/day_7/2.txt
···
1
1
+
.......S.......
2
2
+
...............
3
3
+
.......^.......
4
4
+
...............
5
5
+
......^.^......
6
6
+
...............
7
7
+
.....^.^.^.....
8
8
+
...............
9
9
+
....^.^...^....
10
10
+
...............
11
11
+
...^.^...^.^...
12
12
+
...............
13
13
+
..^...^.....^..
14
14
+
...............
15
15
+
.^.^.^.^.^...^.
16
16
+
...............