Skip to content

Commit

Permalink
2023 day 03
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramblurr committed Dec 3, 2023
1 parent 0e3592c commit 5147449
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
## 2023

* [Day 01](./src/aoc/2023/day01.clj)
* [Day 02](./src/aoc/2023/day02.clj)
* [Day 03](./src/aoc/2023/day03.clj)

## 2022

Expand Down
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
net.mikera/core.matrix {:mvn/version "0.63.0"}
aysylu/loom {:mvn/version "1.0.2"}
thi.ng/geom {:mvn/version "1.0.1"}
io.github.nextjournal/clerk {:mvn/version "0.15.957"}}
io.github.nextjournal/clerk {:mvn/version "0.15.957"}
djblue/portal {:mvn/version "0.49.1"}}
:paths ["resources/" "src/"]
:aliases {:dev {:jvm-opts ["-XX:-OmitStackTraceInFastThrow"]}
:test {:extra-paths ["test"]
Expand Down
2 changes: 1 addition & 1 deletion img/2020.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion img/2023.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions resources/2023/day03-sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
140 changes: 140 additions & 0 deletions resources/2023/day03.txt

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions src/aoc/2023/day03.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
(ns aoc.2023.day03
(:refer-clojure :exclude [parse-long symbol?])
(:require [aoc.core :refer [read-input-lines parse-long]]
[aoc.grid :as grid]))

(def digits #{"0" "1" "2" "3" "4" "5" "6" "7" "8" "9"})

(defn read-grid [fname] (grid/to-grid (read-input-lines fname)
(fn [v]
(cond
(= v ".") {}
(digits v) {:type :digit :digit (parse-long v)}
:else {:type :symbol :symbol v}))))

(defn digit? [v] (= :digit (:type v)))
(defn symbol? [v] (= :symbol (:type v)))

(defn walk-to-not-digit
"Start at coord [x y] in the grid g, walk horizontally in a direction, dir (- or +) until we get to a
grid cell that does not contain a digit, returns the x value of that cell. n is the upper/lower bound that we will walk"
[g [x y] dir n]
(or
(->> (drop 1 (range 100))
(map #(dir x %))
(drop-while #(digit? (g [% y])))
first)
n))

(defn digit->part
"Start at coord [x y] in grid g with n-cols columns. Walk horizontally in both directions accumulating digits.
Returns a vector of [coord part-number] where coord is the coordinate of the first digit in the part and part-number is the full part-number"
[g n-cols [x y]]
(let [start-x (walk-to-not-digit g [x y] - 0)
last-x (walk-to-not-digit g [x y] + n-cols)]
[[(inc start-x) y]
(->> (for [c (range start-x last-x)] (g [c y]))
(map :digit)
(map str)
(apply str)
(parse-long))]))

(defn adjacent-digits
"Using compass directions, returns a list of the neighboring cells of ref-coord, that have digits in them."
[g ref-coord]
(for [coord (grid/adjacent-compass ref-coord)
:when (digit? (g coord))] coord))

(defn grid->schematic
"Convert the grid into a schematic. A schematic is a list of maps. Each map
represents a symbol in the grid. The map contains the symbol and a a list of
part-numbers adjacent to the symbol"
[g]
(let [[_ n-cols] (grid/grid-size g)]
(->> g
(filter #(symbol? (val %)))
(map (fn [[coord v]]
{:symbol (:symbol v)
:part-numbers (->> (adjacent-digits g coord)
(map #(digit->part g n-cols %))
(into {}))}))

(into []))))

(defn part-1 [fname]
(->> (read-grid fname)
(grid->schematic)
(map :part-numbers)
(into {})
vals
(reduce +)))

(part-1 "2023/day03-sample.txt")
;; => 4361
(part-1 "2023/day03.txt")
;; => 512794

(defn part-2 [fname]
(->>
(read-grid fname)
(grid->schematic)
(filter #(= (:symbol %) "*"))
(filter #(= (count (:part-numbers %)) 2))
(map :part-numbers)
(map #(apply * (vals %)))
(reduce +)))

(part-2 "2023/day03-sample.txt")
;; => 467835

(part-2 "2023/day03.txt")
;; => 67779080
1 change: 1 addition & 0 deletions src/aoc/debug.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns aoc.debug)


(defn tap>> [v]
(tap> v)
v)
14 changes: 0 additions & 14 deletions src/aoc/dev.clj

This file was deleted.

9 changes: 9 additions & 0 deletions src/aoc/grid.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
(reduce (fn [g [x y v]]
(assoc g [x y] v)) g row)) {})))


(defn grid-size
"Returns the size of the grid as [width height]"
[g]
(reduce (fn [[width height] [x y]]
[(max width (inc x)) (max height (inc y))])
[0 0]
(keys g)))

(defn insert-with
"Given a sequence of cartesian coordinates and a value, inserts all positions into the grid with that value"
[g poss value]
Expand Down
16 changes: 13 additions & 3 deletions src/user.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
(ns user
(:require [nextjournal.clerk :as clerk]))
(:require
[portal.api :as p]
[nextjournal.clerk :as clerk]))

(defn go []

(p/open {:theme :portal.colors/gruvbox :port 5678})
(add-tap p/submit)
(tap> 1))

(defn clerk []
(clerk/serve! {:browse? true}))

(comment

;; start Clerk's built-in webserver on the default port 7777, opening the browser when done
(clerk/serve! {:browse? true})
;; start Clerk's built-in webserver on the default port 7777, opening the browser when done
(clerk/halt!)

(clerk/show! "src/aoc/2022/day01.clj")
Expand Down
11 changes: 11 additions & 0 deletions test/aoc/2023/day03_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns aoc.2023.day03-test
(:require [aoc.2023.day03 :as sut]
[clojure.test :refer :all]))

(def sample "2023/day03-sample.txt")
(def input "2023/day03.txt")
(deftest day02
(is (= 4361 (sut/part-1 sample)))
(is (= 512794 (sut/part-1 input)))
(is (= 467835 (sut/part-2 sample)))
(is (= 67779080 (sut/part-2 input))))

0 comments on commit 5147449

Please sign in to comment.