mirror of
https://github.com/Noettore/AdventOfCode.git
synced 2025-10-15 19:56:39 +02:00
50
2018/day_06/input
Normal file
50
2018/day_06/input
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
81, 46
|
||||||
|
330, 289
|
||||||
|
171, 261
|
||||||
|
248, 97
|
||||||
|
142, 265
|
||||||
|
139, 293
|
||||||
|
309, 208
|
||||||
|
315, 92
|
||||||
|
72, 206
|
||||||
|
59, 288
|
||||||
|
95, 314
|
||||||
|
126, 215
|
||||||
|
240, 177
|
||||||
|
78, 64
|
||||||
|
162, 168
|
||||||
|
75, 81
|
||||||
|
271, 258
|
||||||
|
317, 223
|
||||||
|
210, 43
|
||||||
|
47, 150
|
||||||
|
352, 116
|
||||||
|
316, 256
|
||||||
|
269, 47
|
||||||
|
227, 343
|
||||||
|
125, 290
|
||||||
|
245, 310
|
||||||
|
355, 301
|
||||||
|
251, 282
|
||||||
|
353, 107
|
||||||
|
254, 298
|
||||||
|
212, 128
|
||||||
|
60, 168
|
||||||
|
318, 254
|
||||||
|
310, 303
|
||||||
|
176, 345
|
||||||
|
110, 109
|
||||||
|
217, 338
|
||||||
|
344, 330
|
||||||
|
231, 349
|
||||||
|
259, 208
|
||||||
|
201, 57
|
||||||
|
200, 327
|
||||||
|
354, 111
|
||||||
|
166, 214
|
||||||
|
232, 85
|
||||||
|
96, 316
|
||||||
|
151, 288
|
||||||
|
217, 339
|
||||||
|
62, 221
|
||||||
|
307, 68
|
133
2018/day_06/main.go
Normal file
133
2018/day_06/main.go
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type point struct {
|
||||||
|
x int
|
||||||
|
y int
|
||||||
|
id int
|
||||||
|
}
|
||||||
|
|
||||||
|
type grid struct {
|
||||||
|
points [][]int
|
||||||
|
coordinates []point
|
||||||
|
currentID int
|
||||||
|
invalidIDs map[int]bool
|
||||||
|
maxX int
|
||||||
|
maxY int
|
||||||
|
minX int
|
||||||
|
minY int
|
||||||
|
maxArea int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p1 point) manhattanDistance(p2 point) int {
|
||||||
|
return int(math.Abs(float64(p1.x-p2.x)) + math.Abs(float64(p1.y-p2.y)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var currentGrid grid
|
||||||
|
|
||||||
|
file, err := os.Open("./input")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scan := bufio.NewScanner(file)
|
||||||
|
for scan.Scan() {
|
||||||
|
var p point
|
||||||
|
|
||||||
|
line := scan.Text()
|
||||||
|
fmt.Sscanf(line, "%d, %d", &p.x, &p.y)
|
||||||
|
p.id = currentGrid.currentID
|
||||||
|
currentGrid.currentID++
|
||||||
|
|
||||||
|
currentGrid.coordinates = append(currentGrid.coordinates, p)
|
||||||
|
|
||||||
|
if p.x > currentGrid.maxX {
|
||||||
|
currentGrid.maxX = p.x
|
||||||
|
}
|
||||||
|
if p.y > currentGrid.maxY {
|
||||||
|
currentGrid.maxY = p.y
|
||||||
|
}
|
||||||
|
if p.x < currentGrid.minX || currentGrid.minX == 0 {
|
||||||
|
currentGrid.minX = p.x
|
||||||
|
}
|
||||||
|
if p.y < currentGrid.minY || currentGrid.minY == 0 {
|
||||||
|
currentGrid.minY = p.y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
currentGrid.points = make([][]int, currentGrid.maxX)
|
||||||
|
for i := range currentGrid.points {
|
||||||
|
currentGrid.points[i] = make([]int, currentGrid.maxY)
|
||||||
|
}
|
||||||
|
|
||||||
|
for x := 0; x < currentGrid.maxX; x++ {
|
||||||
|
for y := 0; y < currentGrid.maxY; y++ {
|
||||||
|
minDist := -1
|
||||||
|
minDistID := -1
|
||||||
|
for i, c := range currentGrid.coordinates {
|
||||||
|
dist := c.manhattanDistance(point{x: x, y: y})
|
||||||
|
if i == 0 {
|
||||||
|
minDist = dist
|
||||||
|
minDistID = c.id
|
||||||
|
} else if dist < minDist {
|
||||||
|
minDist = dist
|
||||||
|
minDistID = c.id
|
||||||
|
} else if dist == minDist {
|
||||||
|
minDistID = -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentGrid.points[x][y] = minDistID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
currentGrid.invalidIDs = make(map[int]bool)
|
||||||
|
for x := 0; x < currentGrid.maxX; x++ {
|
||||||
|
currentGrid.invalidIDs[currentGrid.points[x][0]] = true
|
||||||
|
currentGrid.invalidIDs[currentGrid.points[x][currentGrid.maxY-1]] = true
|
||||||
|
}
|
||||||
|
for y := 0; y < currentGrid.maxY; y++ {
|
||||||
|
currentGrid.invalidIDs[currentGrid.points[0][y]] = true
|
||||||
|
currentGrid.invalidIDs[currentGrid.points[currentGrid.maxX-1][y]] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
for id := 0; id < currentGrid.currentID; id++ {
|
||||||
|
count := 0
|
||||||
|
for x := 0; x < currentGrid.maxX; x++ {
|
||||||
|
for y := 0; y < currentGrid.maxY; y++ {
|
||||||
|
if currentGrid.points[x][y] == id {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if count > currentGrid.maxArea {
|
||||||
|
_, invalidID := currentGrid.invalidIDs[id]
|
||||||
|
if !invalidID {
|
||||||
|
currentGrid.maxArea = count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("Part One: %v\n", currentGrid.maxArea)
|
||||||
|
|
||||||
|
count := 0
|
||||||
|
for x := 0; x < currentGrid.maxX; x++ {
|
||||||
|
for y := 0; y < currentGrid.maxY; y++ {
|
||||||
|
sum := 0
|
||||||
|
for _, c := range currentGrid.coordinates {
|
||||||
|
sum += c.manhattanDistance(point{x: x, y: y})
|
||||||
|
}
|
||||||
|
if sum < 10000 {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("Part Two: %v\n", count)
|
||||||
|
}
|
Reference in New Issue
Block a user