Created
December 16, 2019 04:33
-
-
Save zck/7d563d3ee1b7baf5bf67558aece6c861 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns advent-2019.day-3 | |
(:require [clojure.set :as set])) | |
(def directions {"U" [0 -1] | |
"D" [0 1] | |
"L" [-1 0] | |
"R" [1 0]}) | |
(defn input-item-to-list [input-item] | |
(let [[input-again direction count] (re-matches #"(.)(\d*)" input-item) | |
count (Integer. count)] | |
(repeat count | |
(directions direction)))) | |
(defn input-items-to-list [input-items] | |
(mapcat input-item-to-list input-items)) | |
;;copy, put as strings, e.g. (def w1input (input-items-to-list ["R1003","U476","L876","U147"])) | |
;;This does require manipulation of the input data, but that a choice for speed. | |
(def w1input (input-items-to-list [])) | |
(def w2input (input-items-to-list [])) | |
(defn positions | |
"Given a set of STEPS (like [[0 1] [-1 0]]), return all positions taken by those steps. | |
The result from the example is [[0 0] [0 1] [-1 1]]." | |
[steps] | |
(into #{} | |
(reductions (partial map +) | |
[0 0] | |
steps))) | |
(def w1positions (positions w1input)) | |
(def w2positions (positions w2input)) | |
(def common-positions | |
(set/intersection w1positions w2positions)) | |
(defn manhattan-distance | |
([position] | |
(manhattan-distance [0 0] position)) | |
([[p1x p1y :as position1] | |
[p2x p2y :as position2]] | |
(+ (Math/abs (- p2x p1x)) | |
(Math/abs (- p2y p1y))))) | |
(def p1-solution | |
(apply min (map manhattan-distance (disj common-positions [0 0])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment