summaryrefslogtreecommitdiff
path: root/2024/go/src/day6/main.go
blob: f1505e9f4c7f663a765812833caf5c8922f60396 (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
package main

import (
	"fmt"
	"os"
	"slices"
	"strings"
)

type Orientation = int

const (
	Up    Orientation = 0
	Right Orientation = 1
	Down  Orientation = 2
	Left  Orientation = 3
)

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

}

func turn90(orientation Orientation) Orientation {
	orientations := [4]Orientation{Up, Right, Down, Left}
	return orientations[(orientation+1)%4]
}

type Grid = []string

func isValid(grid *Grid, x int, y int) bool {
	n, m := len(*grid), len((*grid)[0])
	return x >= 0 && x < n && y >= 0 && y < m

}

func replaceCharGrid(grid *Grid, x int, y int, ch rune) {
	str := (*grid)[x]
	runes := []rune(str)
	runes[y] = ch
	(*grid)[x] = string(runes)
}

func simulateTrail(grid *Grid, gPos [2]int) int {
	set := make(map[[2]int]struct{})
	set[gPos] = struct{}{}
	curOrientation := getOrientation((*grid)[gPos[0]][gPos[1]])
	dx := [4]int{-1, 0, 1, 0} // Up, Right, Down, Left
	dy := [4]int{0, 1, 0, -1}
	for {
		nx := gPos[0] + dx[curOrientation]
		ny := gPos[1] + dy[curOrientation]
		if isValid(grid, nx, ny) {
			curPoint := (*grid)[nx][ny]
			if !isObstacle(curPoint) {
				gPos = [2]int{nx, ny}
				replaceCharGrid(grid, nx, ny, 'X')
				set[gPos] = struct{}{}
			} else {
				curOrientation = turn90(curOrientation)
			}
		} else {
			break
		}
	}
	return len(set)
}

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

func getOrientation(ch byte) Orientation {
	if ch == '^' {
		return Up
	} else if ch == '>' {
		return Right
	} else if ch == '<' {
		return Left
	} else {
		return Down
	}
}

func isGaurd(ch byte) bool {
	return slices.Contains([]string{"^", ">", "<", "V"}, string(ch))
}

func isObstacle(ch byte) bool {
	return ch == '#'
}

func solve_part_one(data string) int {
	grid := parseInput(data)
	var guardPosition [2]int
	for i, row := range grid {
		for j, ch := range row {
			if isGaurd(byte(ch)) {
				guardPosition = [2]int{i, j}
			}
		}
	}
	trailCount := simulateTrail(&grid, guardPosition)
	return trailCount
}

func isStuck(grid *Grid, gPos [2]int) bool {
	n, m := len(*grid), len((*grid)[0])
	visited := make([]bool, n*m*4)
	curOrientation := getOrientation((*grid)[gPos[0]][gPos[1]])
	visited[((gPos[0]*n)+gPos[1])*4+curOrientation] = true
	dx := [4]int{-1, 0, 1, 0} // Up, Right, Down, Left
	dy := [4]int{0, 1, 0, -1}
	for {
		nx := gPos[0] + dx[curOrientation]
		ny := gPos[1] + dy[curOrientation]
		if isValid(grid, nx, ny) {
			curPoint := (*grid)[nx][ny]
			if !isObstacle(curPoint) {
				gPos = [2]int{nx, ny}
				if visited[((nx*n)+ny)*4+curOrientation] {
					return true
				}
				visited[((nx*n)+ny)*4+curOrientation] = true
			} else {
				curOrientation = turn90(curOrientation)
			}
		} else {
			break
		}
	}
	return false
}

func solve_part_two(data string) int {
	grid := parseInput(data)
	var guardPosition [2]int
	for i, row := range grid {
		for j, ch := range row {
			if isGaurd(byte(ch)) {
				guardPosition = [2]int{i, j}
			}
		}
	}
	n, m := len(grid), len(grid[0])
	ans := 0
	for i := range n {
		for j := range m {
			gridCopy := make([]string, len(grid))
			copy(gridCopy, grid)
			if i != guardPosition[0] || j != guardPosition[1] {
				replaceCharGrid(&gridCopy, i, j, '#')
				if isStuck(&gridCopy, guardPosition, n*m) {
					ans++
				}
			}
		}
	}
	return ans
}

func main() {
	test := FileRead("../input/day6.test")
	prod := FileRead("../input/day6.prod")
	fmt.Println("Part_1 test: ", solve_part_one(test))
	fmt.Println("Part_1 prod: ", solve_part_one(prod))
	fmt.Println("Part_2 test: ", solve_part_two(test))
	fmt.Println("Part_2 prod: ", solve_part_two(prod))
}