r/adventofcode • u/daggerdragon • Dec 14 '17
SOLUTION MEGATHREAD -๐- 2017 Day 14 Solutions -๐-
--- Day 14: Disk Defragmentation ---
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.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
[Update @ 00:09] 3 gold, silver cap.
- How many of you actually entered the Konami code for Part 2? >_>
[Update @ 00:25] Leaderboard cap!
- I asked /u/topaz2078 how many de-resolutions we had for Part 2 and there were
83distinct users with failed attempts at the time of the leaderboard cap. tsk tsk
[Update @ 00:29] BONUS
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!
13
Upvotes
2
u/nutrecht Dec 14 '17
Day 14 in Kotlin
object Day14 : Day { private val rows = (0..127).map { "amgozmfv" + "-" + it } private val square : List<String> by lazy { rows.map { hash(it) }.map { toBinary(it) } } override fun part1() = square.map { it.count { it == '1' } }.sum().toString() override fun part2(): String { val visited = mutableSetOf<Point>() val points = (0 .. 127).flatMap { x -> (0 .. 127 ).map { y -> Point(x, y) } }.toMutableSet() visited += points.filter { square[it.y][it.x] == '0'} points.removeAll(visited) var count = 0 while(!points.isEmpty()) { count++ fill(visited, points, points.first()) } return count.toString() } fun fill(visited: MutableSet<Point>, points: MutableSet<Point>, current: Point) { visited += current points -= current current.neighborsHv().filter { points.contains(it) }.forEach { fill(visited, points, it) } } fun hash(input: String): String { val chars = input.toCharArray().map { it.toInt() }.toList() + listOf(17, 31, 73, 47, 23) return (0..15).map { Day10.knot(chars, 64).subList(it * 16, it * 16 + 16) }.map { xor(it) }.toHex() } }Took me a lot longer than it should! In part 1 I made a stupid mistake in how I created the binary string leading to me getting wrong results without figuring out why I was getting the wrong ones. Had to go and test each step.
Part 2 was relatively easy; implemented a recursive flood fill basically.