Skip to content

Commit

Permalink
day18.clj
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Dec 18, 2023
1 parent 1da9c17 commit 1094a7c
Show file tree
Hide file tree
Showing 5 changed files with 666 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Day 00: Helper file | [aoc.clj](clojure/aoc.clj) |
[Day 15](http://adventofcode.com/2023/day/15) | [day15.clj](clojure/day15.clj) | Advent of Reading.
[Day 16](http://adventofcode.com/2023/day/16) | [day16.clj](clojure/day16.clj) | Is this a cousin of Day 10?
[Day 17](http://adventofcode.com/2023/day/17) | [day17.clj](clojure/day17.clj) | Consider only L/R directions, and then go straight as far as you can.
<!-- [Day 18](http://adventofcode.com/2023/day/18) | [day18.clj](clojure/day18.clj) | -->
[Day 18](http://adventofcode.com/2023/day/18) | [day18.clj](clojure/day18.clj) | TIL about Shoelace formula and Pick's theorem.
<!-- [Day 19](http://adventofcode.com/2023/day/19) | [day19.clj](clojure/day19.clj) | -->
<!-- [Day 20](http://adventofcode.com/2023/day/20) | [day20.clj](clojure/day20.clj) | -->
<!-- [Day 21](http://adventofcode.com/2023/day/21) | [day21.clj](clojure/day21.clj) | -->
Expand Down
43 changes: 43 additions & 0 deletions clojure/day18.clj
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)
3 changes: 2 additions & 1 deletion clojure/tests/solutions_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
day01 day02 day03 day04 day05
day06 day07 day08 day09 day10
day11 day12 day13 day14 day15
day16 day17 ; day18 day19 day20
day16 day17 day18 ; day19 day20
; day21 day22 day23 day24 day25
[clojure.test :refer [deftest is run-tests successful?]]))

Expand Down Expand Up @@ -41,6 +41,7 @@
(check-day 15 [1320 145] [498538 286278])
(check-day 16 [46 51] [6921 7594])
(check-day 17 [102 94] [758 892])
(check-day 18 [62 952408144115] [47045 147839570293376])

(let [summary (run-tests)]
(when-not (successful? summary)
Expand Down
Loading

0 comments on commit 1094a7c

Please sign in to comment.