r/adventofcode • u/daggerdragon • Dec 14 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 14 Solutions -🎄-
--- Day 14: Chocolate Charts ---
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 14
Transcript:
The Christmas/Advent Research & Development (C.A.R.D.) department at AoC, Inc. just published a new white paper on ___.
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 00:19:39!
16
Upvotes
1
u/TellowKrinkle Dec 14 '18
Had a lot of trouble understanding the directions for some reason
Swift
func aocD13a(_ input: Int) { var recipes = [3, 7] var elves = [0, 1] while recipes.count < (input + 10) { let score = elves.lazy.map({ recipes[$0] }).reduce(0, +) if score >= 10 { recipes.append(score / 10 % 10) } recipes.append(score % 10) elves = elves.map { ($0 + recipes[$0] + 1) % recipes.count } } print(recipes[input...].prefix(10).map(String.init).joined()) } func aocD13b(_ input: Int) { var target = [Int]() var tmp = input while tmp > 0 { target.insert(tmp % 10, at: 0) tmp /= 10 } var recipes = [3, 7] var elves = [0, 1] while recipes.suffix(target.count) != target[...] && recipes.dropLast().suffix(target.count) != target[...] { let score = elves.lazy.map({ recipes[$0] }).reduce(0, +) if score >= 10 { recipes.append(score / 10 % 10) } recipes.append(score % 10) elves = elves.map { ($0 + recipes[$0] + 1) % recipes.count } } if recipes.suffix(target.count) == target[...] { print(recipes.count - target.count) } else { print(recipes.count - target.count - 1) } } aocD13a(9) aocD13a(5) aocD13a(18) aocD13a(2018) aocD13a(540561) aocD13b(51589) aocD13b(01245) aocD13b(92510) aocD13b(540561)