|
| 1 | +package adventofcode |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "golang.org/x/exp/slices" |
| 8 | +) |
| 9 | + |
| 10 | +func day18Part1(input []string) int { |
| 11 | + // prepare a grid as input, lets assume maximum length is 500 |
| 12 | + |
| 13 | + var ( |
| 14 | + gridRow = 1000 |
| 15 | + gridColumn = 1000 |
| 16 | + startX = 300 |
| 17 | + startY = 300 |
| 18 | + ) |
| 19 | + matrix := make([][]byte, gridRow) |
| 20 | + for i := 0; i < len(matrix); i++ { |
| 21 | + matrix[i] = make([]byte, gridColumn) |
| 22 | + } |
| 23 | + |
| 24 | + matrix[startX][startY] = '#' |
| 25 | + |
| 26 | + for _, line := range input { |
| 27 | + parsedInput := strings.Fields(line) |
| 28 | + |
| 29 | + // do not need to handle trench color in p1 |
| 30 | + dir := parsedInput[0] |
| 31 | + length, _ := strconv.Atoi(parsedInput[1]) |
| 32 | + |
| 33 | + for i := 0; i < length; i++ { |
| 34 | + switch dir { |
| 35 | + case "R": |
| 36 | + startY += 1 |
| 37 | + case "D": |
| 38 | + startX += 1 |
| 39 | + case "U": |
| 40 | + startX -= 1 |
| 41 | + case "L": |
| 42 | + startY -= 1 |
| 43 | + } |
| 44 | + matrix[startX][startY] = '#' |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Next need to dig out the interior area |
| 49 | + index := 1 |
| 50 | + indexArray := []byte{} |
| 51 | + for i := 0; i < len(matrix); i++ { |
| 52 | + for j := 0; j < len(matrix[0]); j++ { |
| 53 | + if matrix[i][j] == 0 { |
| 54 | + if traverse18(matrix, byte(index), i, j) { |
| 55 | + indexArray = append(indexArray, byte(index)) |
| 56 | + } |
| 57 | + index += 1 |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + // we have filled all the array we want, now recalculate |
| 62 | + res := 0 |
| 63 | + for i := 0; i < len(matrix); i++ { |
| 64 | + for j := 0; j < len(matrix[0]); j++ { |
| 65 | + if matrix[i][j] == '#' || slices.Contains(indexArray, matrix[i][j]) { |
| 66 | + res += 1 |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + return res |
| 71 | +} |
| 72 | + |
| 73 | +// bool will return whether it's inside the trench or outside |
| 74 | +func traverse18(grid [][]byte, ch byte, x int, y int) bool { |
| 75 | + if x < 0 || x > len(grid)-1 || y < 0 || y > len(grid[0])-1 { |
| 76 | + return false |
| 77 | + } |
| 78 | + if (grid[x][y]) == '#' { |
| 79 | + return true |
| 80 | + } |
| 81 | + // this has been visited in the past |
| 82 | + if grid[x][y] != 0 { |
| 83 | + return true |
| 84 | + } |
| 85 | + |
| 86 | + // mark this has been visited |
| 87 | + grid[x][y] = ch |
| 88 | + isInsideTrench := true |
| 89 | + indexes := []location{location{-1, 0}, location{1, 0}, location{0, -1}, location{0, 1}} |
| 90 | + for _, idx := range indexes { |
| 91 | + // well, should traverse firstly and then judge |
| 92 | + // if use isInsideTrench && traverse18(grid, ch, x+idx.x, y+idx.y) |
| 93 | + // then many directions won't be explored, still work but cause a waste of time |
| 94 | + isInsideTrench = traverse18(grid, ch, x+idx.x, y+idx.y) && isInsideTrench |
| 95 | + |
| 96 | + } |
| 97 | + return isInsideTrench |
| 98 | +} |
0 commit comments