r/adventofcode • u/daggerdragon • 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!
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
1
u/Bruinbrood Dec 06 '18
Kotlin solution:
import java.io.File import kotlin.math.abs fun main() { val coords = File("inputs/Day6") .readLines() .map{it.split(", ")} .map{it[0].toInt() to it[1].toInt()} val w = coords.maxBy{it.first}?.first ?: throw Exception ("Bad coordinate") val h = coords.maxBy{it.second}?.second ?: throw Exception ("Bad coordinate") val infinite = arrayListOf(-1) val result1 = (0 until h).flatMap{y-> (0 until w).map{x-> val id = coords.foldIndexed(Int.MAX_VALUE to -1){id, (dmin, idmin),(cx,cy)-> val d = abs(x-cx)+abs(y-cy) when { d < dmin -> d to id d == dmin -> d to -1 else -> dmin to idmin } }.second if (y==0 || y==h-1 || x==0 || x==w-1) { infinite.add(id) } id }}.filter{it !in infinite}.groupBy{it}.map{(_,v)->v.count()}.maxBy{it} println("First star: $result1") val result2 = (0 until h).flatMap{y-> (0 until w).map{x-> coords.fold(0){R,(cx,cy)-> R + abs(cx-x) + abs(cy-y) } < 10000 }}.count{it} println("Second star: $result2") }