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/[deleted] Dec 13 '18

Common lisp:

Too verbose for my taste, might overhaul it with external libs.

(defun read-input ()
  (let ((sorted (with-open-file (in "04.input")
                  (loop for line = (read-line in nil)
                        while line collect line into lines
                        finally (return (sort lines #'string<))))))
    (loop for line in sorted
          for char = (char line 25)
          for id = (if (char= char #\#)
                       (parse-integer line :start 26 :end (position #\space line :start 26))
                       id)
          for prev-tp = curr-tp
          for curr-tp = (parse-integer line :start 15 :end 17)
          when (char= char #\u) collect (list id prev-tp curr-tp))))

(defun strategy-1 (asleep)
  (destructuring-bind (id minutes sum)
      (reduce (lambda (a b) (if (> (third a) (third b)) a b))
              (loop for k being the hash-keys of asleep using (hash-value v)
                    collect (list k v (reduce #'+ v))))
    (* id (position (reduce #'max minutes) minutes))))

(defun strategy-2 (asleep)
  (destructuring-bind (id minutes max)
      (reduce (lambda (a b) (if (> (third a) (third b)) a b))
              (loop for k being the hash-keys of asleep using (hash-value v)
                    collect (list k v (reduce #'max v))))
    (* id (position max minutes))))

(defun main ()
  (let ((intervals (read-input))
        (asleep (make-hash-table)))
    (loop for int in intervals
          for (arr exists) = (multiple-value-list (gethash (first int) asleep (make-array 60)))
          do (loop for i from (second int) below (third int)
                   do (incf (aref arr i)))
             (unless exists
               (setf (gethash (first int) asleep) arr)))
    (format t "Result 4a: ~d~%" (strategy-1 asleep))
    (format t "Result 4b: ~d~%" (strategy-2 asleep))))

;; CL-USER> (time (main))
;; Result 4a: 77941
;; Result 4b: 35289
;; Evaluation took:
;;   0.005 seconds of real time
;;   0.005114 seconds of total run time (0.005114 user, 0.000000 system)
;;   100.00% CPU
;;   11,035,934 processor cycles
;;   425,168 bytes consed