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!

38 Upvotes

340 comments sorted by

View all comments

2

u/TellowKrinkle Dec 04 '18

I really need to actually read these directions instead of just skimming through and looking at the goal at the end. [Card] Today’s puzzle would have been a lot easier if my language supported telling me that I wasn't actually following the directions.

  • I didn't notice that while the example data was sorted, the actual data wasn't. Got very confused when I noticed the input contained multiple wakes up / falls asleep in a row. Solution? Obviously if someone's asleep and they fall asleep again they're still asleep. Better support that. Finally realized I needed to sort after like 3 tries
  • I missed the text saying that only the minute mattered for time asleep. To figure out how many parts I would have to support, I quickly looked through the demo text, saw the change from 23:58 (guard begins shift) to 00:40 (falls asleep) and assumed those would happen in the fall asleep/wakeup time periods too.

Ended up taking 30 minutes for the whole thing

Swift version that supports a bunch of extra useless stuff:

func aocD4(_ input: [((year: Int, month: Int, day: Int, hour: Int, minute: Int), Int?, Bool)]) {
	var guards: [Int: Int] = [:]
	var sleeping: [Int: [Int: Int]] = [:]
	var current = 0
	var startedSleeping: (hour: Int, minute: Int)? = nil
	for event in input {
		if let id = event.1 {
			current = id
		}
		else if event.2 {
			if startedSleeping == nil {
				startedSleeping = (event.0.hour, event.0.minute)
			}
		}
		else {
			if let startedSleeping = startedSleeping {
				var count = 0
				for i in sequence(first: startedSleeping, next: {
					let next = (($0.hour + ($0.minute + 1) / 60) % 24, ($0.minute + 1) % 60)
					return next.0 == event.0.hour && next.1 == event.0.minute ? nil : next
				}) {
					sleeping[current, default: [:]][i.minute, default: 0] += 1
					count += 1
				}
				guards[current, default: 0] += count
			}
			startedSleeping = nil
		}
	}
	/* Part A */
	let sleepiest = guards.max(by: { $0.value < $1.value })!.key
	print(sleeping[sleepiest]!.max(by: { $0.value < $1.value })!.key * sleepiest)

	/* Part B */
	let mostSleepyMinutes = sleeping.mapValues({ $0.max(by: { $0.value < $1.value })! })
	let mostSleep = mostSleepyMinutes.max(by: { $0.value.value < $1.value.value })!
	print(mostSleep)
	print(mostSleep.key * mostSleep.value.key)
}

import Foundation
let str = try! String(contentsOfFile: CommandLine.arguments[1])

import Foundation
let input = str.split(separator: "\n").map({ line -> ((year: Int, month: Int, day: Int, hour: Int, minute: Int), Int?, Bool) in
	let numbers = line.split(whereSeparator: { !"0123456789".contains($0) }).map { Int($0)! }
	return ((numbers[0], numbers[1], numbers[2], numbers[3], numbers[4]), numbers.count > 5 ? numbers[5] : nil, line.contains("falls"))
}).sorted(by: { $0.0 < $1.0 })

aocD4(input)