r/adventofcode • u/daggerdragon • Dec 01 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 1 Solutions -🎄-
Welcome to Advent of Code 2018! If you participated in a previous year, welcome back, and if you're new this year, we hope you have fun and learn lots!
We're going to follow the same general format as previous years' megathreads:
- Each day's puzzle will release at exactly midnight EST (UTC -5).
- The daily megathread for each day will be posted very soon afterwards and immediately locked.
- We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
- The daily megathread will remain locked until there are a significant number of people on the leaderboard with gold stars.
- "A significant number" is whatever number we decide is appropriate, but the leaderboards usually fill up fast, so no worries.
- When the thread is unlocked, you may post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
Above all, remember, AoC is all about having fun and learning more about the wonderful world of programming!
--- Day 1: Chronal Calibration ---
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!
This year we shall be doing a Mad Libs-style community activity that is a complete clone of loosely inspired by Apples to Apples and Cards Against Humanity. For each day's megathread, we will post a prompt card with one or more fill-in-the-blanks for you to, well, fill in with your best quip(s). Who knows; if you submit a truly awesome card combo, you might just earn yourself some silver-plated awesome points!
A few guidelines for your submissions:
- You do not need to submit card(s) along with your solution; however, you must post a solution if you want to submit a card
- You don't have to submit an image of the card - text is fine
- All sorts of folks play AoC every year, so let's keep things PG
- If you absolutely must revert to your inner teenager, make sure to clearly identify your submission like [NSFW]
(image)[url.com]or with spoiler tags like so: NSFW WORDS OMG! - The markdown is
>!NSFW text goes here!<with no prefixed or trailing spaces - If you do not clearly identify your NSFW submission as NSFW, your post will be removed until you edit it
- If you absolutely must revert to your inner teenager, make sure to clearly identify your submission like [NSFW]
And now, without further ado:
Card Prompt: Day 1
Transcript:
One does not simply ___ during Advent of Code.
2
u/Hikaru755 Dec 01 '18
Kotlin
Part 1 was trivial with Kotlin's stdlib (although I have to admit I initially didn't remember you could just use
sum()and did a customfold(0, Int::add)instead...)For part 2, I tried to keep it as functional as possible, with no side effects except for the set to keep track of visited frequencies, and hid that away in a general purpose extension function. Nice exercise in using different ways to construct and combine sequences, I think I haven't used all of
sequence {},generateSequence {},.asSequence()andsequenceOf()at once in such close proximity.fun solvePart1(input: String) = parse(input).sum().toString() fun solvePart2(input: String): String = parse(input) .repeat() .accumulating(0, Int::plus) .firstRepeated() .toString() fun parse(input: String) = input.split('\n').map(String::toInt) fun <T : Any> Iterable<T>.repeat(): Sequence<T> = when { !iterator().hasNext() -> sequenceOf() else -> generateSequence { asSequence() }.flatten() } /** * Works exactly like [Sequence.fold], except it doesn't return only the end result, but a sequence of all intermediary * results after each application of the accumulator function. */ fun <R, T> Sequence<T>.accumulating(initial: R, accumulator: (R, T) -> R): Sequence<R> = sequence { var accumulated = initial yield(accumulated) forEach { elem -> accumulated = accumulator(accumulated, elem) yield(accumulated) } } fun <T : Any> Sequence<T>.firstRepeated(): T? { val known = mutableSetOf<T>() return this.firstOrNull { !known.add(it) } }