2018 Day 06 Complete

This commit is contained in:
2018-12-06 11:22:55 -06:00
parent 20051e984a
commit 5f920c039d
10 changed files with 742 additions and 35 deletions

185
2018/day06/day06.go Normal file
View File

@@ -0,0 +1,185 @@
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
)
const MaxInt = int(^uint(0) >> 1)
func main() {
inp := StdinToStringSlice()
start(inp)
part1()
part2()
}
var points []RegionPos
var boundaries []RegionPos
var allPoints []RegionPos
var maxX, maxY, minX, minY int
func start(inp []string) {
maxX, maxY = 0, 0
minX, minY = MaxInt, MaxInt
for _, v := range inp {
pts := strings.Split(v, ", ")
x, y := Atoi(pts[0]), Atoi(pts[1])
p := RegionPos{x: x, y: y}
points = append(points, p)
if x < minX {
minX = x
}
if x > maxX {
maxX = x
}
if y < minY {
minY = y
}
if y > maxY {
maxY = y
}
}
// Find the points that are the closest to the boundaries
for x := minX; x <= maxX; x++ {
boundaries = append(boundaries, *NewRegionPos(x, minY))
boundaries = append(boundaries, *NewRegionPos(x, maxY))
}
for y := minY; y <= maxY; y++ {
boundaries = append(boundaries, *NewRegionPos(minX, y))
boundaries = append(boundaries, *NewRegionPos(maxX, y))
}
for i := range boundaries {
closest := boundaries[i].findClosest(points)
if len(closest) == 1 {
for j := range points {
if points[j].x == closest[0].x && points[j].y == closest[0].y {
points[j].ignored = true
}
}
} else {
boundaries[i].ignored = true
}
}
}
func part1() {
for x := minX; x <= maxX; x++ {
for y := minY; y <= maxY; y++ {
p := NewRegionPos(x, y)
closest := p.findClosest(points)
if len(closest) == 1 {
c := closest[0]
for i := range points {
if points[i].ignored {
continue
}
if points[i].x == c.x && points[i].y == c.y {
points[i].regionSize++
break
}
}
}
}
}
var largestSize int
for _, v := range points {
if !v.ignored {
if v.regionSize > largestSize {
largestSize = v.regionSize
}
}
}
fmt.Println("= Part 1 =")
fmt.Println(largestSize)
}
func part2() {
minDistance := 10000
var regionCount int
for y := minY - minDistance; y <= maxY+minDistance; y++ {
for x := minX - minDistance; x <= maxX+minDistance; x++ {
p := NewRegionPos(x, y)
if p.sumOfDistances(minDistance, points) < minDistance {
regionCount++
}
}
}
fmt.Println("= Part 2 =")
fmt.Println(regionCount)
}
func manhattanDistance(x1, y1, x2, y2 int) int {
return int(math.Abs(float64(x1)-float64(x2)) + math.Abs(float64(y1)-float64(y2)))
}
type RegionPos struct {
x, y int
regionSize int
closestPos *RegionPos
ignored bool
}
func NewRegionPos(x, y int) *RegionPos {
b := &RegionPos{}
b.x = x
b.y = y
return b
}
func (p *RegionPos) distanceToPos(o *RegionPos) int {
return manhattanDistance(p.x, p.y, o.x, o.y)
}
func (b *RegionPos) sumOfDistances(d int, list []RegionPos) int {
var ret int
for _, p := range list {
ret += b.distanceToPos(&p)
}
return ret
}
func (b *RegionPos) findClosest(list []RegionPos) []RegionPos {
var ret []RegionPos
minD := MaxInt
for _, p := range list {
test := b.distanceToPos(&p)
if test < minD {
minD = test
ret = ret[:0]
ret = append(ret, p)
} else if test == minD {
ret = append(ret, p)
}
}
return ret
}
func (b *RegionPos) string() string {
return fmt.Sprintf("%d;%d", b.x, b.y)
}
func StdinToStringSlice() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}

1
2018/day06/go.mod Normal file
View File

@@ -0,0 +1 @@
module day06

50
2018/day06/input Normal file
View File

@@ -0,0 +1,50 @@
224, 153
176, 350
353, 241
207, 59
145, 203
123, 210
113, 203
191, 241
172, 196
209, 249
260, 229
98, 231
305, 215
258, 141
337, 282
156, 140
325, 197
179, 279
283, 233
317, 150
305, 245
67, 109
251, 140
245, 59
173, 105
59, 173
257, 70
269, 110
102, 162
179, 180
324, 112
357, 311
317, 245
239, 112
321, 220
133, 97
334, 99
117, 102
133, 112
222, 316
68, 296
150, 287
263, 263
66, 347
128, 118
63, 202
68, 236
264, 122
77, 243
92, 110

122
2018/day06/problem Normal file
View File

@@ -0,0 +1,122 @@
Advent of Code
--- Day 6: Chronal Coordinates ---
The device on your wrist beeps several times, and once again you feel like you're falling.
"Situation critical," the device announces. "Destination indeterminate. Chronal interference detected. Please specify new target coordinates."
The device then produces a list of coordinates (your puzzle input). Are they places it thinks are safe or dangerous? It recommends you check manual page 729. The Elves did not give you a manual.
If they're dangerous, maybe you can minimize the danger by finding the coordinate that gives the largest distance from the other points.
Using only the Manhattan distance, determine the area around each coordinate by counting the number of integer X,Y locations that are closest to that coordinate (and aren't tied in distance to any other coordinate).
Your goal is to find the size of the largest area that isn't infinite. For example, consider the following list of coordinates:
1, 1
1, 6
8, 3
3, 4
5, 5
8, 9
If we name these coordinates A through F, we can draw them on a grid, putting 0,0 at the top left:
..........
.A........
..........
........C.
...D......
.....E....
.B........
..........
..........
........F.
This view is partial - the actual grid extends infinitely in all directions. Using the Manhattan distance, each location's closest coordinate can be determined, shown here in lowercase:
aaaaa.cccc
aAaaa.cccc
aaaddecccc
aadddeccCc
..dDdeeccc
bb.deEeecc
bBb.eeee..
bbb.eeefff
bbb.eeffff
bbb.ffffFf
Locations shown as . are equally far from two or more coordinates, and so they don't count as being closest to any.
In this example, the areas of coordinates A, B, C, and F are infinite - while not shown here, their areas extend forever outside the visible grid. However, the areas of coordinates D and E are finite: D is closest to 9 locations, and E is closest to 17 (both including the coordinate's location itself).
Therefore, in this example, the size of the largest area is 17.
What is the size of the largest area that isn't infinite?
Your puzzle answer was 3989.
--- Part Two ---
On the other hand, if the coordinates are safe, maybe the best you can do is try to find a region near as many coordinates as possible.
For example, suppose you want the sum of the Manhattan distance to all of the coordinates to be less than 32. For each location, add up the distances to all of the given coordinates; if the total of those distances is less than 32, that location is within the desired region. Using the same coordinates as above,
the resulting region looks like this:
..........
.A........
..........
...###..C.
..#D###...
..###E#...
.B.###....
..........
..........
........F.
In particular, consider the highlighted location 4,3 located at the top middle of the region. Its calculation is as follows, where abs() is the absolute value function:
 Distance to coordinate A: abs(4-1) + abs(3-1) =  5
 Distance to coordinate B: abs(4-1) + abs(3-6) =  6
 Distance to coordinate C: abs(4-8) + abs(3-3) =  4
 Distance to coordinate D: abs(4-3) + abs(3-4) =  2
 Distance to coordinate E: abs(4-5) + abs(3-5) =  3
 Distance to coordinate F: abs(4-8) + abs(3-9) = 10
 Total distance: 5 + 6 + 4 + 2 + 3 + 10 = 30
Because the total distance to all coordinates (30) is less than 32, the location is within the region.
This region, which also includes coordinates D and E, has a total size of 16.
Your actual region will need to be much larger than this example, though, instead including all locations with a total distance of less than 10000.
What is the size of the region containing all locations which have a total distance to all given coordinates of less than 10000?
Your puzzle answer was 49715.
Both parts of this puzzle are complete! They provide two gold stars: **
References
Visible links
. https://adventofcode.com/
. https://adventofcode.com/2018/about
. https://adventofcode.com/2018/events
. https://teespring.com/adventofcode
. https://adventofcode.com/2018/settings
. https://adventofcode.com/2018/auth/logout
. Advent of Code Supporter
https://adventofcode.com/2018/support
. https://adventofcode.com/2018
. https://adventofcode.com/2018
. https://adventofcode.com/2018/support
. https://adventofcode.com/2018/sponsors
. https://adventofcode.com/2018/leaderboard
. https://adventofcode.com/2018/stats
. https://adventofcode.com/2018/sponsors
. https://en.wikipedia.org/wiki/Taxicab_geometry
. https://en.wikipedia.org/wiki/Integer
. https://en.wikipedia.org/wiki/Taxicab_geometry
. https://en.wikipedia.org/wiki/Absolute_value
. https://adventofcode.com/2018
. https://adventofcode.com/2018/day/6/input

6
2018/day06/testinput Normal file
View File

@@ -0,0 +1,6 @@
1, 1
1, 6
8, 3
3, 4
5, 5
8, 9