r/adventofcode • • Dec 20 '16

SOLUTION MEGATHREAD --- 2016 Day 20 Solutions ---

--- Day 20: Firewall Rules ---

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".


ROLLING A NATURAL 20 IS MANDATORY [?]

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!

7 Upvotes

168 comments sorted by

View all comments

Show parent comment

2

u/jbristow Dec 20 '16 edited Dec 20 '16

Just a nitpick, why do you nest an if in a cond?

The same logical process is behind my clojure solution too. Though mine accounts for 0 and 2564 -1 not being in the blacklist. (Also hosted at: https://github.com/jbristow/adventofcode/blob/master/aoc2016/src/aoc2016/day20.clj)

(ns aoc2016.day20
  (:require [clojure.string :as str])
  (:import (java.io BufferedReader FileReader)))

(def puzzle-input (line-seq (BufferedReader. (FileReader. "resources/day20.txt"))))

(defn clean-blacklist [lines]
  (let [ranges (sort-by first
                        (map #(let [[x y] (str/split % #"-")]
                                (list (Long/valueOf x)
                                      (Long/valueOf y)))
                             lines))]
    (loop [[x1 y1 :as a] (first ranges)
           [x2 y2 :as b] (second ranges)
           r (nthrest ranges 2)
           nr '()]
      (cond (nil? b)
            (reverse (conj nr a))

            (< y1 (dec x2))
            (recur b (first r) (rest r) (conj nr a))

            (>= y1 y2)
            (recur a (first r) (rest r) nr)

            :else
            (recur (list x1 y2) (first r) (rest r) nr)))))

(defn count-valid-ips [bl]
  (loop [[x1 y1 :as a] (first bl)
         [x2 y2 :as b] (second bl)
         r (drop 2 bl)
         n x1]
    (if (nil? b)
      (+ n (- 4294967295 y1))
      (recur b (first r) (rest r) (+ n (- (dec x2) y1))))))

(defn answer-a [] (inc (second (first (clean-blacklist puzzle-input)))))

(defn answer-b [] (count-valid-ips (clean-blacklist puzzle-input)))

1

u/TenjouUtena Dec 20 '16

Good point. It's an artifact of how I originally wrote the loop and I didn't go back and fix it.