1use crate::util::grid::*;
12use crate::util::point::*;
13
14type Input = (u64, u64);
15
16pub fn parse(input: &str) -> Input {
17 let grid = Grid::parse_with_border(input);
19
20 let mut todo = Vec::new();
21 let mut seen = grid.same_size_with(false);
22
23 let mut part_one = 0;
24 let mut part_two = 0;
25
26 for y in 1..grid.height - 1 {
28 for x in 1..grid.width {
29 let start = Point::new(x, y);
30 let same = |point| grid[point] == grid[start];
31
32 if seen[start] {
34 continue;
35 }
36
37 let mut area = 0;
39 let mut perimeter = 0;
40 let mut sides = 0;
41
42 todo.push(start);
43 seen[start] = true;
44
45 while let Some(point) = todo.pop() {
46 area += 1;
47
48 for direction in ORTHOGONAL {
49 let next = point + direction;
50
51 if !same(next) {
52 let clockwise = direction.clockwise();
53 perimeter += 1;
54 sides += u64::from(!same(point + clockwise) || same(next + clockwise));
55 } else if !seen[next] {
56 todo.push(next);
57 seen[next] = true;
58 }
59 }
60 }
61
62 part_one += area * perimeter;
63 part_two += area * sides;
64 }
65 }
66
67 (part_one, part_two)
68}
69
70pub fn part1(input: &Input) -> u64 {
71 input.0
72}
73
74pub fn part2(input: &Input) -> u64 {
75 input.1
76}