-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
666 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
(ns day18 | ||
(:require aoc | ||
[clojure.string :as str])) | ||
|
||
|
||
(def deltas {"R" [1 0] "D" [0 1] "L" [-1 0] "U" [0 -1] | ||
"0" [1 0] "1" [0 1] "2" [-1 0] "3" [0 -1]}) | ||
|
||
|
||
(defn parse-line [line] | ||
(let [[d n c] (str/split line #" ") | ||
c (str/replace c #"[(#)]" "") | ||
[dist dir] (map #(apply str %) (split-at 5 c)) | ||
dist (Integer/parseInt dist 16)] | ||
{:p1 {:dir (deltas d) | ||
:dist (parse-long n)} | ||
:p2 {:dir (deltas dir) | ||
:dist dist}})) | ||
|
||
|
||
(defn dig-trench [input] | ||
; Shoelace formula + Pick's theorem | ||
(->> input | ||
(reduce (fn [{:keys [total x]} | ||
{:keys [dir dist]}] | ||
(let [[dx dy] dir | ||
nx (+ x (* dist dx)) | ||
area (* nx (* dist dy))] | ||
{:x nx | ||
:total (+ total area (/ dist 2))})) | ||
{:total 1 | ||
:x 0}) | ||
:total | ||
long)) | ||
|
||
|
||
(defn solve [input-file] | ||
(let [dig-plan (aoc/read-input input-file parse-line)] | ||
[(dig-trench (map :p1 dig-plan)) | ||
(dig-trench (map :p2 dig-plan))])) | ||
|
||
|
||
(solve 18) |
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
Oops, something went wrong.