-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.rs
109 lines (90 loc) · 2.05 KB
/
day12.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
99
100
101
102
103
104
105
106
107
108
109
use crate::solutions::Solution;
use crate::utils::grid::Grid;
pub struct Day12;
impl Solution for Day12 {
fn part_one(&self, input: &str) -> String {
Grid::<char>::from(input)
.get_all_regions()
.iter()
.map(|region| region.perimeter() * region.area())
.sum::<usize>()
.to_string()
}
fn part_two(&self, input: &str) -> String {
Grid::<char>::from(input)
.get_all_regions()
.iter()
.map(|region| region.corners() * region.area())
.sum::<usize>()
.to_string()
}
}
#[cfg(test)]
mod tests {
use crate::solutions::year2024::day12::Day12;
use crate::solutions::Solution;
const EXAMPLE_1: &str = r#"AAAA
BBCD
BBCC
EEEC"#;
#[test]
fn part_one_example_1() {
let result = (10 * 4) + (10 * 4) + (8 * 4) + (3 * 8) + 4;
assert_eq!(result.to_string(), Day12.part_one(EXAMPLE_1));
}
#[test]
fn part_two_example_1() {
assert_eq!("80", Day12.part_two(EXAMPLE_1));
}
const EXAMPLE_2: &str = r#"OOOOO
OXOXO
OOOOO
OXOXO
OOOOO"#;
#[test]
fn part_one_example_2() {
let result = 4 * 4 + (21 * 36);
assert_eq!(result.to_string(), Day12.part_one(EXAMPLE_2));
}
#[test]
fn part_two_example_2() {
assert_eq!("436", Day12.part_two(EXAMPLE_2));
}
const EXAMPLE_3: &str = r#"RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE"#;
#[test]
fn part_one_example_3() {
assert_eq!("1930", Day12.part_one(EXAMPLE_3));
}
#[test]
fn part_two_example_3() {
assert_eq!("1206", Day12.part_two(EXAMPLE_3));
}
#[test]
fn part_two_e_shape() {
const EXAMPLE: &str = r#"EEEEE
EXXXX
EEEEE
EXXXX
EEEEE"#;
assert_eq!("236", Day12.part_two(EXAMPLE));
}
#[test]
fn part_two_last_example() {
const EXAMPLE: &str = r#"AAAAAA
AAABBA
AAABBA
ABBAAA
ABBAAA
AAAAAA"#;
assert_eq!("368", Day12.part_two(EXAMPLE));
}
}