|
1 | 1 | package day6 |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +type direction uint |
| 9 | + |
| 10 | +const ( |
| 11 | + Up direction = iota |
| 12 | + Right |
| 13 | + Down |
| 14 | + Left |
| 15 | +) |
| 16 | + |
| 17 | +type room struct { |
| 18 | + layout [][]square |
| 19 | + guard guard |
| 20 | +} |
| 21 | + |
| 22 | +type square struct { |
| 23 | + visited bool |
| 24 | + obstacle bool |
| 25 | +} |
| 26 | + |
| 27 | +type guard struct { |
| 28 | + direction direction |
| 29 | + location |
| 30 | +} |
| 31 | + |
| 32 | +type location struct { |
| 33 | + x int |
| 34 | + y int |
| 35 | +} |
| 36 | + |
3 | 37 | func Solve(input string) uint { |
4 | | - return 41 |
| 38 | + rm := parseInput(input) |
| 39 | + return solve(rm) |
| 40 | +} |
| 41 | + |
| 42 | +func parseInput(input string) room { |
| 43 | + var rm room |
| 44 | + for j, l := range strings.Split(input, "\n") { |
| 45 | + rm.layout = slices.Insert(rm.layout, j, make([]square, 0)) |
| 46 | + for i, ch := range l { |
| 47 | + sq := square{} |
| 48 | + if ch == '#' { |
| 49 | + sq.obstacle = true |
| 50 | + } else if ch != '.' { |
| 51 | + rm.guard = guard{ |
| 52 | + direction: parseDirection(ch), |
| 53 | + location: location{ |
| 54 | + x: i, |
| 55 | + y: j, |
| 56 | + }, |
| 57 | + } |
| 58 | + sq.visited = true |
| 59 | + } |
| 60 | + rm.layout[j] = slices.Insert(rm.layout[j], i, sq) |
| 61 | + } |
| 62 | + } |
| 63 | + return rm |
| 64 | +} |
| 65 | + |
| 66 | +func parseDirection(input rune) direction { |
| 67 | + switch input { |
| 68 | + case '>': |
| 69 | + return Right |
| 70 | + case '<': |
| 71 | + return Left |
| 72 | + case 'v': |
| 73 | + return Down |
| 74 | + } |
| 75 | + return Up |
| 76 | +} |
| 77 | + |
| 78 | +func solve(rm room) uint { |
| 79 | + var moves, unique uint = 0, 1 |
| 80 | + nextLocation := rm.guard.location |
| 81 | + for { |
| 82 | + moves++ |
| 83 | + x, y := adjustLocation(rm.guard.direction) |
| 84 | + nextLocation.x += x |
| 85 | + nextLocation.y += y |
| 86 | + |
| 87 | + if offGrid(nextLocation, rm) { |
| 88 | + break |
| 89 | + } |
| 90 | + |
| 91 | + nextSquare := rm.layout[nextLocation.y][nextLocation.x] |
| 92 | + if nextSquare.obstacle { |
| 93 | + rm.guard.direction += 1 |
| 94 | + if rm.guard.direction > Left { |
| 95 | + rm.guard.direction = Up |
| 96 | + } |
| 97 | + nextLocation = rm.guard.location |
| 98 | + } else { |
| 99 | + if !nextSquare.visited { |
| 100 | + unique++ |
| 101 | + rm.layout[nextLocation.y][nextLocation.x].visited = true |
| 102 | + } |
| 103 | + rm.guard.location = nextLocation |
| 104 | + } |
| 105 | + } |
| 106 | + return unique |
| 107 | +} |
| 108 | + |
| 109 | +func adjustLocation(input direction) (int, int) { |
| 110 | + switch input { |
| 111 | + case Right: |
| 112 | + return 1, 0 |
| 113 | + case Left: |
| 114 | + return -1, 0 |
| 115 | + case Down: |
| 116 | + return 0, 1 |
| 117 | + case Up: |
| 118 | + return 0, -1 |
| 119 | + } |
| 120 | + return 0, 0 |
| 121 | +} |
| 122 | + |
| 123 | +func offGrid(loc location, rm room) bool { |
| 124 | + return loc.x < 0 || loc.y < 0 || loc.x >= len(rm.layout) || loc.y >= len(rm.layout[0]) |
5 | 125 | } |
0 commit comments