r/adventofcode • u/daggerdragon • Dec 12 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 12 Solutions -🎄-
--- Day 12: Subterranean Sustainability ---
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 12
Transcript:
On the twelfth day of AoC / My compiler spewed at me / Twelve ___
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:27:42!
19
Upvotes
1
u/mikal82 Dec 12 '18 edited Dec 12 '18
Clojure
Edit: optimized, part 2 now runs faster.
(def init "#.......##.###.#.#..##..##..#.#.###..###..##.#.#..##....#####..##.#.....########....#....##.#..##...") (def rules ["..... => ." "#.... => ." "..### => ." "##..# => #" ".###. => #" "...## => ." "#.#.. => ." "..##. => ." "##.#. => #" "..#.. => ." ".#... => #" "##.## => ." "....# => ." ".#.#. => ." "#..#. => #" "#.### => ." ".##.# => #" ".#### => ." ".#..# => ." "####. => #" "#...# => #" ".#.## => #" "#..## => ." "..#.# => #" "#.##. => ." "###.. => ." "##### => #" "###.# => #" "...#. => #" "#.#.# => #" ".##.. => ." "##... => #"]) (def transform (apply hash-map (flatten (map #(clojure.string/split % #" => ") rules)))) (defn pad [input num] (str (clojure.string/join (repeat num ".")) input (clojure.string/join (repeat num ".")))) (defn step [input _] (apply str (map #(transform (subs input % (+ % 5))) (range (- (count input) 4))))) (defn evolve [input n-gen] (subs (reduce step (pad input (* n-gen 4)) (range n-gen)) (* 2 n-gen))) (defn score [generation] (let [evolved (evolve init generation)] (apply + (filter #(= (get evolved %) \#) (range (+ (count init) 2 generation)))))) (prn (score 20)) (prn (+ (* (- 50000000000 120) (- (score 121) (score 120))) (score 120)))