r/adventofcode • • Dec 04 '18

SOLUTION MEGATHREAD -πŸŽ„- 2018 Day 4 Solutions -πŸŽ„-

--- Day 4: Repose Record ---


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 4

Transcript:

Today’s puzzle would have been a lot easier if my language supported ___.


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!

39 Upvotes

340 comments sorted by

View all comments

1

u/nibarius Dec 04 '18

Kotlin

[CARD] Today’s puzzle would have been a lot easier if my language supported finding the snooziest minute.

I'm not very fond of using regexp, so I'm usually chaining a couple of substring calls when parsing input to get what I want. To me this is often more readable to me. With my approach part 2 ended up being very easy to solve.

class Day4(input: List<String>) { private val guards = parseInput(input)

    private fun parseInput(input: List<String>): MutableMap<Int, IntArray> {
        val sorted = input.sorted()
        var currentGuard = -1
        var i = 0
    
        // Map of all guards, value is an array where each entry specifies how many days the guard was
        // sleeping during that minute
        val guards = mutableMapOf<Int, IntArray>()
        while (i < sorted.size) {
            val line = sorted[i++]
            // Example lines to parse (falls asleep is always followed by wakes up):
            // [1518-11-01 00:00] Guard #10 begins shift
            // [1518-11-01 00:05] falls asleep
            // [1518-11-01 00:25] wakes up
            if (line.endsWith("begins shift")) {
                currentGuard = line.substringAfter("#").substringBefore(" ").toInt()
                guards.putIfAbsent(currentGuard, IntArray(60))
                continue
            }
    
            val fallAsleep = line.substringAfter(":").take(2).toInt()
            val wakeup = sorted[i++].substringAfter(":").take(2).toInt()
            (fallAsleep until wakeup).forEach { guards.getValue(currentGuard)[it]++ }
        }
        return guards
    }
    
    private fun findGuardMinuteCombination(strategy: (Map.Entry<Int, IntArray>) -> Int): Int {
        val snooziestGuard = guards.maxBy { strategy(it) }!!
        val snooziestMinute = snooziestGuard.value.withIndex().maxBy { it.value }!!.index
        return snooziestGuard.key * snooziestMinute
    }
    
    fun solvePart1(): Int {
        // Find the guard with most total sleep time
        return findGuardMinuteCombination { guards -> guards.value.sum() }
    }
    
    fun solvePart2(): Int {
        // Find the guard with the maximum sleep time for a given minute
        return findGuardMinuteCombination { guards -> guards.value.max()!! }
    }
}

Special thanks to /u/bdjnk for the great variable name suggestion.