-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12p2.odin
110 lines (92 loc) · 2.5 KB
/
12p2.odin
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
110
package main
import "core:fmt"
import "core:strings"
D12P2 :: proc() {
input_string := #load("inputs/12.txt", string)
lines := strings.split(input_string, "\n", context.temp_allocator)
fencing_cost := get_garden_fencing_cost_p2(lines)
fmt.printf("Fencing cost: %d\n", fencing_cost)
}
get_garden_fencing_cost_p2 :: proc(input: []string) -> int {
garden := parse_garden(input)
defer {
for row in garden {
delete(row)
}
delete(garden)
}
fence_cost := map_fences_p2(&garden)
return fence_cost
}
map_fences_p2 :: proc(garden: ^[dynamic][dynamic]rune) -> (fence_cost: int) {
visited := map[vec2]bool{}
defer delete(visited)
for row, y in garden {
for _, x in row {
if visited[vec2{x, y}] {
continue
}
// Find all connected positions with the same value
current_region := map[vec2]bool{}
defer delete(current_region)
pot := garden[y][x]
queue := [dynamic]vec2{vec2{x, y}}
defer delete(queue)
for len(queue) > 0 {
pos := pop(&queue)
if pos in visited || pos in current_region {
continue
}
if garden[pos.y][pos.x] != pot {
continue
}
current_region[pos] = true
visited[pos] = true
neighbours := [dynamic]vec2 {
vec2{pos.x - 1, pos.y},
vec2{pos.x + 1, pos.y},
vec2{pos.x, pos.y - 1},
vec2{pos.x, pos.y + 1},
}
defer delete(neighbours)
for n in neighbours {
if n.x < 0 || n.x >= len(row) || n.y < 0 || n.y >= len(garden^) {
continue
}
if garden[n.y][n.x] == pot {
append(&queue, n)
}
}
}
// Count corners for this region
corners := 0
for pos in current_region {
// Outer corners
left := vec2{pos.x - 1, pos.y} not_in current_region
right := vec2{pos.x + 1, pos.y} not_in current_region
up := vec2{pos.x, pos.y - 1} not_in current_region
down := vec2{pos.x, pos.y + 1} not_in current_region
// Count outer corners
corners += int(left && up)
corners += int(right && up)
corners += int(left && down)
corners += int(right && down)
// Count inner corners
if !left && !up {
corners += int(vec2{pos.x - 1, pos.y - 1} not_in current_region)
}
if !right && !up {
corners += int(vec2{pos.x + 1, pos.y - 1} not_in current_region)
}
if !left && !down {
corners += int(vec2{pos.x - 1, pos.y + 1} not_in current_region)
}
if !right && !down {
corners += int(vec2{pos.x + 1, pos.y + 1} not_in current_region)
}
}
fence_cost += len(current_region) * corners
}
}
return fence_cost
}