|
| 1 | +package day12 |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | +) |
| 6 | + |
| 7 | +type point struct { |
| 8 | + x, y int |
| 9 | +} |
| 10 | + |
| 11 | +type fence struct { |
| 12 | + x, y int |
| 13 | + direction rune |
| 14 | +} |
| 15 | + |
| 16 | +func (p point) surroundingFences() []fence { |
| 17 | + top := fence{x: p.x, y: p.y, direction: '-'} |
| 18 | + bottom := fence{x: p.x, y: p.y + 1, direction: '-'} |
| 19 | + left := fence{x: p.x, y: p.y, direction: '|'} |
| 20 | + right := fence{x: p.x + 1, y: p.y, direction: '|'} |
| 21 | + return []fence{top, bottom, left, right} |
| 22 | +} |
| 23 | + |
| 24 | +type patch struct { |
| 25 | + area uint |
| 26 | + perimeter uint |
| 27 | +} |
| 28 | + |
| 29 | +func Solve(input string) uint { |
| 30 | + crops := parseInput(input) |
| 31 | + var cost uint = 0 |
| 32 | + for _, crop := range crops { |
| 33 | + uniquePatches, fencesUsed := findUniquePatches(crop) |
| 34 | + details := patchDetails(uniquePatches, fencesUsed) |
| 35 | + for _, d := range details { |
| 36 | + cost += d.area * d.perimeter |
| 37 | + } |
| 38 | + } |
| 39 | + return cost |
| 40 | +} |
| 41 | + |
| 42 | +func parseInput(input string) map[rune][]point { |
| 43 | + crops := make(map[rune][]point) |
| 44 | + for j, line := range strings.Split(input, "\n") { |
| 45 | + for i, plant := range line { |
| 46 | + crops[plant] = append(crops[plant], point{x: i, y: j}) |
| 47 | + } |
| 48 | + } |
| 49 | + return crops |
| 50 | +} |
| 51 | + |
| 52 | +func findUniquePatches(crop []point) (map[uint][]point, map[fence]uint) { |
| 53 | + var nextPatchId uint = 1 |
| 54 | + patches := make(map[uint][]point) |
| 55 | + fencesByPatchId := make(map[fence]uint) |
| 56 | + for _, p := range crop { |
| 57 | + usedNewPatchId := true |
| 58 | + currentPatchId := nextPatchId |
| 59 | + newlyUsedFences := make([]fence, 0) |
| 60 | + |
| 61 | + for _, f := range p.surroundingFences() { |
| 62 | + if fencesByPatchId[f] == 0 { |
| 63 | + newlyUsedFences = append(newlyUsedFences, f) |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + if fencesByPatchId[f] != currentPatchId { |
| 68 | + patches[fencesByPatchId[f]] = append(patches[fencesByPatchId[f]], patches[currentPatchId]...) |
| 69 | + delete(patches, currentPatchId) |
| 70 | + currentPatchId = fencesByPatchId[f] |
| 71 | + usedNewPatchId = false |
| 72 | + } |
| 73 | + delete(fencesByPatchId, f) |
| 74 | + } |
| 75 | + |
| 76 | + for _, f := range newlyUsedFences { |
| 77 | + fencesByPatchId[f] = currentPatchId |
| 78 | + } |
| 79 | + |
| 80 | + patches[currentPatchId] = append(patches[currentPatchId], p) |
| 81 | + |
| 82 | + if usedNewPatchId { |
| 83 | + nextPatchId++ |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return patches, fencesByPatchId |
| 88 | +} |
| 89 | + |
| 90 | +func patchDetails(patches map[uint][]point, fencesUsed map[fence]uint) []patch { |
| 91 | + details := make([]patch, len(patches)) |
| 92 | + i := 0 |
| 93 | + for _, points := range patches { |
| 94 | + details[i].area = uint(len(points)) |
| 95 | + for _, p := range points { |
| 96 | + for _, f := range p.surroundingFences() { |
| 97 | + if fencesUsed[f] > 0 { |
| 98 | + details[i].perimeter++ |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + i++ |
| 103 | + } |
| 104 | + return details |
| 105 | +} |
0 commit comments