diff options
| author | omagdy <omar.professional8777@gmail.com> | 2024-12-10 20:50:53 +0200 |
|---|---|---|
| committer | omagdy <omar.professional8777@gmail.com> | 2024-12-10 20:50:53 +0200 |
| commit | 549b9010bd6e4ac9aedc5543a5b75c9b4c190ffe (patch) | |
| tree | a7a5d7118f2d1fee7074559e0eb84c83c4ac4cb4 /2024/go/src | |
| parent | 9efb5b409fa624c40aaca777a41ff3fddf0da3f5 (diff) | |
| download | aoc-549b9010bd6e4ac9aedc5543a5b75c9b4c190ffe.tar.xz aoc-549b9010bd6e4ac9aedc5543a5b75c9b4c190ffe.zip | |
Day 10 done.
Diffstat (limited to '2024/go/src')
| -rw-r--r-- | 2024/go/src/day10/main.go | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/2024/go/src/day10/main.go b/2024/go/src/day10/main.go new file mode 100644 index 0000000..428b9b0 --- /dev/null +++ b/2024/go/src/day10/main.go @@ -0,0 +1,121 @@ +package main + +import ( + "fmt" + "os" + "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 Point struct { + x int + y int +} + +func (p Point) inBounds(w, h int) bool { + return p.x >= 0 && p.x < w && p.y >= 0 && p.y < h +} + +type Grid = [][]byte + +func bfs(grid Grid, start Point, uniq bool) int { + cnt := 0 + w, h := len(grid), len(grid[0]) + var queue []Point + visited := make(map[Point]bool) + visited[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] + queue = queue[1:] // pop from front + for i := 0; i < 4; i++ { + nx := curPoint.x + dx[i] + ny := curPoint.y + dy[i] + newPoint := Point{x: nx, y: ny} + if uniq { + if newPoint.inBounds(w, h) && !visited[newPoint] && int(grid[newPoint.x][newPoint.y]) == int(grid[curPoint.x][curPoint.y])+1 { + visited[newPoint] = true + queue = append(queue, newPoint) + if grid[newPoint.x][newPoint.y] == byte('9') { + cnt++ + } + } + } else { + if newPoint.inBounds(w, h) && int(grid[newPoint.x][newPoint.y]) == int(grid[curPoint.x][curPoint.y])+1 { + queue = append(queue, newPoint) + if grid[newPoint.x][newPoint.y] == byte('9') { + cnt++ + } + } + + } + } + } + return cnt +} + +// String returns a string representation of the grid +func printGrid(g Grid) { + for _, row := range g { + for _, r := range row { + fmt.Printf("%v ", string(r)) + } + fmt.Println() + } +} + +func parseInput(data string) Grid { + var grid Grid + for _, line := range strings.Split(data, "\n") { + if len(line) > 0 { + row := []byte(line) + grid = append(grid, row) + } + } + return grid +} + +func solve_part_one(data string) int { + grid := parseInput(data) + ans := 0 + for i, row := range grid { + for j, byte := range row { + if byte == '0' { + ans += bfs(grid, Point{i, j}, true) + } + } + } + return ans +} + +func solve_part_two(data string) int { + grid := parseInput(data) + ans := 0 + for i, row := range grid { + for j, byte := range row { + if byte == '0' { + ans += bfs(grid, Point{i, j}, false) + } + } + } + return ans +} + +func main() { + test := FileRead("../input/day10.test") + prod := FileRead("../input/day10.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)) +} |
