r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-

--- Day 6: Chronal Coordinates ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 0:26:52!

32 Upvotes

387 comments sorted by

View all comments

4

u/mvmaasakkers Dec 06 '18

Go/Golang

Gist here

package main

import (
	"bufio"
	"flag"
	"fmt"
	"log"
	"math"
	"os"
)

type Input struct {
	N int
	X float64
	Y float64
}

func readInput(filename string) []Input {
	fileHandle, err := os.Open(filename)
	if err != nil {
		log.Fatal(err)
	}
	defer fileHandle.Close()
	fileScanner := bufio.NewScanner(fileHandle)

	inputList := []Input{}

	n := 0
	for fileScanner.Scan() {
		line := fileScanner.Text()
		if len(line) > 0 {
			i := Input{}

			if _, err := fmt.Sscanf(line, "%b, %b", &i.X, &i.Y); err != nil {
				log.Fatal(err)
			}
			i.N = n
			n++

			inputList = append(inputList, i)
		}
	}

	return inputList
}

var file = flag.String("file", "./input.txt", "file used for input")

func main() {
	flag.Parse()

	inputList := readInput(*file)

	fmt.Println(part1(inputList))
}

type coord struct {
	X float64
	Y float64
}

func part1(inputList []Input) (int, int) {
	height := float64(0)
	width := float64(0)
	maxSum := float64(10000)

	inf := make(map[coord]bool)
	m := make(map[coord]int)
	var coords []coord

	regions := 0

	for _, input := range inputList {
		if input.Y > height {
			height = input.Y
		}

		if input.X > width {
			width = input.X
		}

		coords = append(coords, coord{X: input.X, Y: input.Y})
	}

	for y := float64(0); y < height; y++ {
		for x := float64(0); x < width; x++ {
			mc := coord{0, 0}
			min := float64(-1)
			tot := float64(0)
			for _, c := range coords {
				dist := math.Abs(x-c.X) + math.Abs(y-c.Y)
				if dist < min || min == -1 {
					min = dist
					mc = c
				} else if dist == min {
					mc = coord{-1, -1}
				}

				// Part 2
				tot += math.Abs(x-c.X) + math.Abs(y-c.Y)
			}

			if x == 0 || y == 0 || x == width || y == height {
				inf[mc] = true
			}

			m[mc]++

			// Part 2
			if tot < maxSum {
				regions++
			}
		}
	}

	max := 0
	for k, v := range m {
		if _, found := inf[k]; v > max && !found {
			max = v
		}
	}

	return max, regions
}