|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "../util" |
| 9 | +) |
| 10 | + |
| 11 | +type Coords struct { |
| 12 | + X, Y int |
| 13 | +} |
| 14 | + |
| 15 | +func main() { |
| 16 | + |
| 17 | + scanner, _ := util.ReadFile("input.txt") |
| 18 | + fabric := make([][]int, 1000*1000) |
| 19 | + |
| 20 | + overlappedCells := map[string][]Coords{} |
| 21 | + overlappedClaims := map[int]bool{} |
| 22 | + |
| 23 | + var cleanClaim int |
| 24 | + |
| 25 | + for scanner.Scan() { |
| 26 | + line := scanner.Text() |
| 27 | + id, startCoords, endCoords := parseCoords(line) |
| 28 | + |
| 29 | + for i := startCoords.X; i <= endCoords.X; i++ { |
| 30 | + for j := startCoords.Y; j <= endCoords.Y; j++ { |
| 31 | + |
| 32 | + cell := fabric[i*1000+j] |
| 33 | + cellName := strconv.Itoa(i) + `x` + strconv.Itoa(j) |
| 34 | + |
| 35 | + fabric[i*1000+j] = append(cell, id) |
| 36 | + |
| 37 | + if len(fabric[i*1000+j]) > 1 { |
| 38 | + overlappedCells[cellName] = append(overlappedCells[cellName], Coords{i, j}) |
| 39 | + |
| 40 | + for _, t := range fabric[i*1000+j] { |
| 41 | + overlappedClaims[t] = true |
| 42 | + } |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + for i := 1; i <= 1295; i++ { |
| 51 | + |
| 52 | + fmt.Println(overlappedClaims[i]) |
| 53 | + |
| 54 | + if overlappedClaims[i] { |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + cleanClaim = i |
| 59 | + } |
| 60 | + |
| 61 | + if err := scanner.Err(); err != nil { |
| 62 | + fmt.Println("File scanning error", err) |
| 63 | + } |
| 64 | + |
| 65 | + fmt.Println("Overlapped Cells: ", len(overlappedCells)) |
| 66 | + fmt.Println("Clean Claim: ", cleanClaim) |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +func parseCoords(str string) (int, Coords, Coords) { |
| 71 | + |
| 72 | + var re = regexp.MustCompile(`#(?m)([0-9]{0,4}) @ (?m)([0-9]{0,4}),(?m)([0-9]{0,4}): (?m)([0-9]{0,4})x(?m)([0-9]{0,4})`) |
| 73 | + stringParsed := re.FindStringSubmatch(str) |
| 74 | + |
| 75 | + id, _ := strconv.Atoi(stringParsed[1]) |
| 76 | + leftGap, _ := strconv.Atoi(stringParsed[2]) |
| 77 | + topGap, _ := strconv.Atoi(stringParsed[3]) |
| 78 | + width, _ := strconv.Atoi(stringParsed[4]) |
| 79 | + height, _ := strconv.Atoi(stringParsed[5]) |
| 80 | + |
| 81 | + startCoords := Coords{leftGap + 1, topGap + 1} |
| 82 | + endCoords := Coords{leftGap + width, topGap + height} |
| 83 | + |
| 84 | + return id, startCoords, endCoords |
| 85 | +} |
0 commit comments