r/adventofcode • • Dec 15 '16

SOLUTION MEGATHREAD --- 2016 Day 15 Solutions ---

--- Day 15: Timing is Everything ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".


ZAMENHOFA TAGO ESTAS DEVIGA [?]

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!

5 Upvotes

121 comments sorted by

View all comments

Show parent comment

2

u/Tarmen Dec 15 '16

Again a task where the parsing dwarfs the actual solution. Maybe I should just throw arrays into the solution as well.

import System.IO

main = print . firstValid . parseAll =<< readFile "in15.txt"

firstValid discs = head [time | time <- [0..], all (isValid time) discs]
  where isValid time (slots, offset) = (offset + time) `mod` slots == 0

parseAll = map parseLine . lines
parseLine ln = (slots, offset)
  where tokens = words ln
        position = read . init . last $ tokens
        slots = read $ tokens !! 3
        idx = read . tail $ tokens !! 1
        offset = idx + position

1

u/haoformayor Dec 15 '16

life's too short to parse competition input