summaryrefslogtreecommitdiff
path: root/2024/go/src/day12/main.go
blob: 89e60f7049aef9e2455b524a1984c8e94e048205 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main

import (
	"fmt"
	"os"
	"sort"
	"strings"
)

func FileRead(path string) string {
	file, err := os.ReadFile(path)
	if err != nil {
		fmt.Println("Couldn't Read file: ", err)
	}
	return string(file)
}

type Grid = [][]rune

func printGrid(grid [][]rune) {
	for _, row := range grid {
		for _, cell := range row {
			fmt.Print(string(cell))
		}
		fmt.Println()
	}
}

func parseInput(data string) Grid {
	lines := strings.Split(data, "\n")
	var grid Grid
	for _, line := range lines {
		if len(line) > 0 {
			runes := []rune(line)
			grid = append(grid, runes)
		}
	}
	return grid
}

func inBounds(x, y, n, m int) bool {
	return x >= 0 && x < n && y >= 0 && y < m
}

func abs(x, y int) int {
	if x-y < 0 {
		return -(x - y)
	} else {
		return x - y
	}
}

func bfs(grid Grid, start Point, seen *map[Point]bool) (int, int) {
	perimeter := 1
	area := 0
	w, h := len(grid), len(grid[0])
	var queue []Point
	(*seen)[start] = true
	queue = append(queue, start)
	dx := []int{1, -1, 0, 0} // down, up, right, left
	dy := []int{0, 0, 1, -1}
	for len(queue) > 0 {
		curPoint := queue[0]
		curPlant := grid[curPoint[0]][curPoint[1]]
		queue = queue[1:]
		for i := 0; i < 4; i++ {
			nx := curPoint[0] + dx[i]
			ny := curPoint[1] + dy[i]
			newPoint := Point{nx, ny}
			if inBounds(nx, ny, w, h) && grid[nx][ny] == curPlant {
				if !(*seen)[newPoint] {
					perimeter++
					queue = append(queue, newPoint)
					(*seen)[newPoint] = true
				}
			} else {
				area++
			}
		}
	}
	return perimeter, area
}

func getDir(dx, dy int) string {
	if dx == 0 && dy == 1 {
		return "R"
	} else if dx == 0 && dy == -1 {
		return "L"
	} else if dx == -1 && dy == 0 {
		return "U"
	} else if dx == 1 && dy == 0 {
		return "D"
	}
	return "unreachable"
}

func merge(walls [][4]int, dir int) int {
	cnt := 0
	for i := 0; i < len(walls)-1; i++ {
		cur := walls[i]
		next := walls[i+1]
		if dir == 0 { // x direction
			if cur[0] == next[0] && abs(cur[1], next[1]) == 1 && cur[2] == next[2] && cur[3] == next[3] {
				cnt++
			}
		} else if dir == 1 { // y direction
			if cur[1] == next[1] && abs(cur[0], next[0]) == 1 && cur[2] == next[2] && cur[3] == next[3] {
				cnt++
			} else {
			}
		}
	}
	return cnt
}

func printWalls(walls [][4]int) {
	for _, wall := range walls {
		fmt.Printf("(%d, %d) - %s / ", wall[0], wall[1], getDir(wall[2], wall[3]))
	}
	fmt.Println()
}

func bfs2(grid Grid, start Point, seen *map[Point]bool) (int, int) {
	perimeter := 1
	area := 0
	w, h := len(grid), len(grid[0])
	var queue []Point
	var walls [][4]int
	pointsX := make(map[int]map[Point]struct{})
	pointsX[start[0]] = make(map[Point]struct{})
	pointsX[start[0]][start] = struct{}{}
	(*seen)[start] = true
	queue = append(queue, start)
	dx := []int{1, -1, 0, 0} // down, up, right, left
	dy := []int{0, 0, 1, -1}
	for len(queue) > 0 {
		curPoint := queue[0]
		curPlant := grid[curPoint[0]][curPoint[1]]
		queue = queue[1:]
		for i := 0; i < 4; i++ {
			nx := curPoint[0] + dx[i]
			ny := curPoint[1] + dy[i]
			newPoint := Point{nx, ny}
			if inBounds(nx, ny, w, h) && grid[nx][ny] == curPlant {
				if !(*seen)[newPoint] {
					perimeter++
					queue = append(queue, newPoint)
					(*seen)[newPoint] = true
				}
			} else {
				area++
				walls = append(walls, [4]int{curPoint[0], curPoint[1], dx[i], dy[i]})
				if _, exists := pointsX[newPoint[0]]; !exists {
					pointsX[newPoint[0]] = make(map[Point]struct{})
				}
				pointsX[newPoint[0]][newPoint] = struct{}{}
			}
		}
	}

	sort.Slice(walls, func(i, j int) bool {
		if walls[i][0] != walls[j][0] {
			return walls[i][0] < walls[j][0] // Sort by the first element
		}
		if walls[i][2] != walls[j][2] {
			return walls[i][2] < walls[j][2] // Sort by the third element
		}
		if walls[i][3] != walls[j][3] {
			return walls[i][3] < walls[j][3] // Sort by the third element
		}
		return walls[i][1] < walls[j][1] // Sort by the second element
	})

	area -= merge(walls, 0)

	sort.Slice(walls, func(i, j int) bool {
		if walls[i][1] != walls[j][1] {
			return walls[i][1] < walls[j][1] // Sort by the second element
		}
		if walls[i][2] != walls[j][2] {
			return walls[i][2] < walls[j][2] // If second is equal, sort by the third element
		}
		if walls[i][3] != walls[j][3] {
			return walls[i][3] < walls[j][3]