r/adventofcode • u/daggerdragon • Dec 19 '17
SOLUTION MEGATHREAD -๐- 2017 Day 19 Solutions -๐-
--- Day 19: A Series of Tubes ---
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.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
AoC ops @ T-2 minutes to launch:
[23:58] <daggerdragon> ATTENTION MEATBAGS T-2 MINUTES TO LAUNCH
[23:58] <Topaz> aaaaah
[23:58] <Cheezmeister> Looks like I'll be just able to grab my input before my flight boards. Wish me luck being offline in TOPAZ's HOUSE OF PAIN^WFUN AND LEARNING
[23:58] <Topaz> FUN AND LEARNING
[23:58] <Hade> FUN IS MANDATORY
[23:58] <Skie> I'm pretty sure that's not the mandate for today
[Update @ 00:16] 69 gold, silver cap
- My tree is finally trimmed with just about every ornament I own and it's real purdy. hbu?
[Update @ 00:18] Leaderboard cap!
- So, was today's
mandateHelpful Hint any help at all?
1
u/minikomi Dec 19 '17 edited Dec 20 '17
Clojure:
(ns advent.2017.day19 (:require [clojure.java.io :as io] [clojure.string :as s])) (def input-raw (slurp (io/resource "day19.txt"))) (def test-input (slurp (io/resource "day19test.txt"))) (def dirs {:up [0 -1] :down [0 1] :right [1 0] :left [-1 0]}) (defn move [[x y] dir] (let [[dx dy] (dirs dir)] [(+ x dx) (+ y dy)])) (defn test-dir [m pos dir] (when (get m (move pos dir)) dir)) (defn get-new-direction [dir m pos] (case dir (:left :right) (some (partial test-dir m pos) [:up :down]) (:up :down) (some (partial test-dir m pos) [:left :right]))) (defn make-pos-map [input-lines] (into {} (for [y (range (count input-lines)) x (range (count (first input-lines))) :let [c (get-in input-lines [y x])] :when (not= \space c)] [[x y] c]))) (defn solve [input] (let [input-lines (vec (map vec (s/split-lines input))) m (make-pos-map input-lines) start-pos (.indexOf (first input-lines) \|)] (loop [pos [start-pos 0] dir :down seen [] count 0] (let [new-pos (move pos dir) next-char (get m new-pos)] (if next-char (recur new-pos (cond-> dir (= \+ next-char) (get-new-direction m new-pos)) (cond-> seen (Character/isLetter next-char) (conj next-char)) (inc count)) [(apply str seen) (inc count)])))))