tangled
alpha
login
or
join now
bwc9876.dev
/
advent
0
fork
atom
Advent of Code solutions
0
fork
atom
overview
issues
pulls
pipelines
Day 10 & 11
bwc9876.dev
1 year ago
1e6757ea
443eb038
verified
This commit was signed with the committer's
known signature
.
bwc9876.dev
SSH Key Fingerprint:
SHA256:DanMEP/RNlSC7pAVbnXO6wzQV00rqyKj053tz4uH5gQ=
+187
-12
6 changed files
expand all
collapse all
unified
split
years
2024
src
day_10.rs
day_11.rs
examples
day_10
1.txt
2.txt
day_11
1.txt
2.txt
+69
-6
years/2024/src/day_10.rs
···
1
1
+
use std::collections::HashSet;
1
2
2
2
-
use advent_core::{Day, day_stuff, ex_for_day};
3
3
+
use advent_core::{day_stuff, ex_for_day, Day};
4
4
+
use utils::{dir::CARDINALS, pos::Position};
3
5
4
6
pub struct Day10;
5
7
8
8
+
#[derive(Clone, Debug)]
9
9
+
pub struct Tile(usize);
10
10
+
11
11
+
impl From<char> for Tile {
12
12
+
fn from(value: char) -> Self {
13
13
+
Self(value.to_string().parse().unwrap())
14
14
+
}
15
15
+
}
16
16
+
17
17
+
pub type Grid = utils::grid::Grid<Tile>;
18
18
+
19
19
+
fn get_asc(g: &Grid, pos: Position, num: usize) -> impl Iterator<Item = Position> + use<'_> {
20
20
+
g.relatives(pos, &CARDINALS)
21
21
+
.filter_map(move |(_, r_pos, t)| if t.0 == num + 1 { Some(r_pos) } else { None })
22
22
+
}
23
23
+
6
24
impl Day for Day10 {
25
25
+
day_stuff!(10, "36", "81", Grid);
7
26
8
8
-
day_stuff!(10, "", "");
27
27
+
fn part_1(input: Self::Input) -> Option<String> {
28
28
+
let starts = input
29
29
+
.iter()
30
30
+
.filter_map(|(p, t)| if t.0 == 0 { Some(p) } else { None });
31
31
+
32
32
+
let mut tot = 0;
33
33
+
34
34
+
for start in starts {
35
35
+
let mut num = 0;
36
36
+
let mut nxt = vec![start];
37
37
+
while num != 9 {
38
38
+
nxt = nxt
39
39
+
.into_iter()
40
40
+
.flat_map(|o| get_asc(&input, o, num))
41
41
+
.collect::<Vec<_>>();
42
42
+
num += 1;
43
43
+
}
44
44
+
45
45
+
let hsh = nxt.into_iter().collect::<HashSet<_>>();
46
46
+
47
47
+
tot += hsh.len();
48
48
+
}
49
49
+
50
50
+
Some(tot.to_string())
51
51
+
}
52
52
+
53
53
+
fn part_2(input: Self::Input) -> Option<String> {
54
54
+
let starts = input
55
55
+
.iter()
56
56
+
.filter_map(|(p, t)| if t.0 == 0 { Some(p) } else { None });
57
57
+
58
58
+
let mut tot = 0;
59
59
+
60
60
+
for start in starts {
61
61
+
let mut num = 0;
62
62
+
let mut nxt = vec![start];
63
63
+
while num != 9 {
64
64
+
nxt = nxt
65
65
+
.into_iter()
66
66
+
.flat_map(|o| get_asc(&input, o, num))
67
67
+
.collect::<Vec<_>>();
68
68
+
num += 1;
69
69
+
}
9
70
10
10
-
fn part_1(_input: Self::Input) -> Option<String> {
11
11
-
None
71
71
+
tot += nxt.len();
72
72
+
}
73
73
+
74
74
+
Some(tot.to_string())
12
75
}
13
76
14
14
-
fn part_2(_input: Self::Input) -> Option<String> {
15
15
-
None
77
77
+
fn parse_input(input: &str) -> Self::Input {
78
78
+
Grid::parse(input.trim())
16
79
}
17
80
}
+100
-6
years/2024/src/day_11.rs
···
1
1
+
use std::collections::HashMap;
1
2
2
2
-
use advent_core::{Day, day_stuff, ex_for_day};
3
3
+
use advent_core::{day_stuff, ex_for_day, Day};
3
4
4
5
pub struct Day11;
5
6
6
7
impl Day for Day11 {
8
8
+
day_stuff!(11, "55312", "65601038650482", Vec<u128>);
7
9
8
8
-
day_stuff!(11, "", "");
10
10
+
fn part_1(input: Self::Input) -> Option<String> {
11
11
+
let l = input.len();
12
12
+
let mut stone_map = input
13
13
+
.into_iter()
14
14
+
.fold(HashMap::with_capacity(l), |mut acc, stone| {
15
15
+
acc.entry(stone).and_modify(|c| *c += 1).or_insert(1);
16
16
+
acc
17
17
+
});
18
18
+
19
19
+
for _i in 0..25 {
20
20
+
let mut new_map = HashMap::with_capacity(stone_map.len());
21
21
+
22
22
+
for (num, count) in stone_map {
23
23
+
if num == 0 {
24
24
+
new_map
25
25
+
.entry(1)
26
26
+
.and_modify(|c| *c += count)
27
27
+
.or_insert(count);
28
28
+
} else {
29
29
+
let ss = num.to_string();
30
30
+
let l = ss.len();
31
31
+
if l % 2 == 0 {
32
32
+
let (num1, num2) =
33
33
+
(ss[..l / 2].parse().unwrap(), ss[l / 2..].parse().unwrap());
34
34
+
new_map
35
35
+
.entry(num1)
36
36
+
.and_modify(|c| *c += count)
37
37
+
.or_insert(count);
38
38
+
new_map
39
39
+
.entry(num2)
40
40
+
.and_modify(|c| *c += count)
41
41
+
.or_insert(count);
42
42
+
} else {
43
43
+
new_map
44
44
+
.entry(num * 2024)
45
45
+
.and_modify(|c| *c += count)
46
46
+
.or_insert(count);
47
47
+
}
48
48
+
}
49
49
+
}
9
50
10
10
-
fn part_1(_input: Self::Input) -> Option<String> {
11
11
-
None
51
51
+
stone_map = new_map;
52
52
+
}
53
53
+
54
54
+
Some(stone_map.values().sum::<u128>().to_string())
12
55
}
13
56
14
14
-
fn part_2(_input: Self::Input) -> Option<String> {
15
15
-
None
57
57
+
fn part_2(input: Self::Input) -> Option<String> {
58
58
+
let l = input.len();
59
59
+
let mut stone_map = input
60
60
+
.into_iter()
61
61
+
.fold(HashMap::with_capacity(l), |mut acc, stone| {
62
62
+
acc.entry(stone).and_modify(|c| *c += 1).or_insert(1);
63
63
+
acc
64
64
+
});
65
65
+
66
66
+
for _i in 0..75 {
67
67
+
let mut new_map = HashMap::with_capacity(stone_map.len());
68
68
+
69
69
+
for (num, count) in stone_map {
70
70
+
if num == 0 {
71
71
+
new_map
72
72
+
.entry(1)
73
73
+
.and_modify(|c| *c += count)
74
74
+
.or_insert(count);
75
75
+
} else {
76
76
+
let ss = num.to_string();
77
77
+
let l = ss.len();
78
78
+
if l % 2 == 0 {
79
79
+
let (num1, num2) =
80
80
+
(ss[..l / 2].parse().unwrap(), ss[l / 2..].parse().unwrap());
81
81
+
new_map
82
82
+
.entry(num1)
83
83
+
.and_modify(|c| *c += count)
84
84
+
.or_insert(count);
85
85
+
new_map
86
86
+
.entry(num2)
87
87
+
.and_modify(|c| *c += count)
88
88
+
.or_insert(count);
89
89
+
} else {
90
90
+
new_map
91
91
+
.entry(num * 2024)
92
92
+
.and_modify(|c| *c += count)
93
93
+
.or_insert(count);
94
94
+
}
95
95
+
}
96
96
+
}
97
97
+
98
98
+
stone_map = new_map;
99
99
+
}
100
100
+
101
101
+
Some(stone_map.values().sum::<u128>().to_string())
102
102
+
}
103
103
+
104
104
+
fn parse_input(input: &str) -> Self::Input {
105
105
+
input
106
106
+
.trim()
107
107
+
.split(" ")
108
108
+
.map(|n| n.parse::<u128>().unwrap())
109
109
+
.collect()
16
110
}
17
111
}
+8
years/2024/src/examples/day_10/1.txt
···
1
1
+
89010123
2
2
+
78121874
3
3
+
87430965
4
4
+
96549874
5
5
+
45678903
6
6
+
32019012
7
7
+
01329801
8
8
+
10456732
+8
years/2024/src/examples/day_10/2.txt
···
1
1
+
89010123
2
2
+
78121874
3
3
+
87430965
4
4
+
96549874
5
5
+
45678903
6
6
+
32019012
7
7
+
01329801
8
8
+
10456732
+1
years/2024/src/examples/day_11/1.txt
···
1
1
+
125 17
+1
years/2024/src/examples/day_11/2.txt
···
1
1
+
125 17