This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-08
Channels
- # adventofcode (49)
- # announcements (2)
- # architecture (4)
- # babashka (48)
- # babashka-sci-dev (4)
- # beginners (7)
- # biff (1)
- # calva (14)
- # cider (6)
- # clj-kondo (1)
- # clj-yaml (1)
- # cljsrn (3)
- # clojure (14)
- # clojure-art (12)
- # clojure-europe (62)
- # clojure-nl (1)
- # clojure-norway (35)
- # clojure-uk (5)
- # clojurescript (18)
- # clr (4)
- # community-development (9)
- # conjure (2)
- # core-async (3)
- # cursive (2)
- # datomic (2)
- # emacs (8)
- # events (3)
- # graalvm (1)
- # helix (6)
- # holy-lambda (3)
- # jobs (1)
- # off-topic (16)
- # polylith (30)
- # practicalli (11)
- # reitit (5)
- # shadow-cljs (14)
- # slack-help (10)
- # xtdb (6)
https://gitlab.com/maximoburrito/advent2022/-/blob/main/src/day08/main.clj Dumb mistake on part 1 where I tested if all trees in the direction were visible, not just if the one tree was visible. I had to go back to the sample input and write the map visual to catch the mistake. Lots of time lost. Wheee
https://github.com/callum-oakley/advent-of-code/blob/main/src/aoc/2022/08.clj breaking out the grid helper functions from previous years 😌
A little parsing, a couple of transducers... all normal stuff here. • https://github.com/abyala/advent-2022-clojure/blob/main/docs/day08.md • https://github.com/abyala/advent-2022-clojure/blob/main/src/advent_2022_clojure/day08.clj
A bit verbose as always. This felt like an ur-aoc-problem, the kind of thing an AI would generate on prompt (not in a bad way, mind you). https://github.com/motform/advent-of-clojure/blob/master/src/advent_of_clojure/2022/08.clj
Using clojure.core.matrix: https://github.com/tylerw/advent-of-code-2022/blob/master/src/aoc2022/day08.cljc
with split-at
and split-with
today. https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2022/day08.clj
I've over engineered again https://github.com/nbardiuk/adventofcode/blob/master/2022/src/day08.clj
https://github.com/jramosg/advent-of-code/blob/master/src/advent_of_code/year_2022/day_08/main.clj
eduction https://github.com/zelark/AoC-2022/blob/master/src/zelark/aoc_2022/day_08.clj
Ugly as sin, and I realize this is not the first year that I've used meta
to track information like this in a grid: https://coyotesqrl.github.io/advent-of-code/2022/src/coyotesqrl/2022/day08.html
Oooo using clerk is nice @U01GXCWSRMW very pretty. Here is my effort, which seems overlong compared to the problem at hand. https://github.com/wardle/aoc2022/blob/main/src/day08.clj
I used a map with [x y] as keys.. and then just a bunch of threading (like most of this month has been so far)
https://github.com/Ramblurr/advent-of-code-2022/blob/2bee95d7f45a4fb1572004882fb17d11b0aa82e7/src/aoc/day08.clj#L25-L59
got to pull out medley's take-upto
which I find I use surprisingly often.. I wonder why it's not in clojure.core
couldn't get yesterday's, so glad I was able to get today's. Starting to think that maybe it wasn't a good idea to pick AoC as my first introduction to clojure haha https://github.com/mducharm/advent_of_code_2022/blob/main/src/advent_of_code/day_08.clj
Day 8 https://github.com/bhauman/advent-of-code-2022/blob/main/src/adv2022/day8/sol.clj
definitely late to the game today... with a rather ugly entry: https://github.com/CarnunMP/Advent-of-Code/blob/master/src/y2022/d8.clj now to read your solutions and realise what trick I missed! 😁
Okay, for one: Every now and then I'm reminded how in problems like this it's much nicer and neater to move unit distances from some position, rather than keep track of long lists of positions to check. And I think, "That's really nice and neat!" Then some time later it just leaks out of my ears, apparently. 🙃
nice https://github.com/callum-oakley/advent-of-code/blob/main/src/aoc/grid.clj @U01HL2S0X71!
another TI(re-)L: range
can take a 'step'!
ty @UA2U3KW0L :))
@U018D6NKRA4 np! I still feel weird when I have to inc/dec the end
https://github.com/stuartstein777/clj-advent-of-code/blob/master/src/stuartstein777/2022/day8.clj Finally. So, last night around 9pm. I started on part 2. I was sure what I had was right and would work, and then was debugging it till just past 1am... I just couldn't find what I had wrong. I re-read the question and realised I had skipped the paragraph.
To measure the viewing distance from a given tree, look up, down, left, and right from that tree; stop if you reach an edge or at the first tree that is the same height or taller than the tree under consideration. (If a tree is right on the edge, at least one of its viewing distances will be zero.)
The Elves don't care about distant trees taller than those found by the rules above;
sigh
Anyway, I solved it by creating a grid like this of height and co-ordinate.
[[[3 [0 0]] [0 [0 1]] [3 [0 2]] [7 [0 3]] [3 [0 4]]]
[[2 [1 0]] [5 [1 1]] [5 [1 2]] [1 [1 3]] [2 [1 4]]]
[[6 [2 0]] [5 [2 1]] [3 [2 2]] [3 [2 3]] [2 [2 4]]]
[[3 [3 0]] [3 [3 1]] [5 [3 2]] [4 [3 3]] [9 [3 4]]]
[[3 [4 0]] [5 [4 1]] [3 [4 2]] [9 [4 3]] [0 [4 4]]]]
Then I could just deal with each row left -> right, because even after rotating the grid and reversing rows, I still had the original cell co-ordinates.(ns aoc.day-08
(:require [clojure.string :refer [split]]))
(let [nd (mapv #(mapv (comp parse-long str) %) (split (slurp "../day_08.data") #"\n"))
td (apply mapv vector nd)
[my mx] [(dec (count nd)) (dec (count td))]
views (fn [y x] [(reverse (take x (nd y))) (drop (inc x) (nd y))
(reverse (take y (td x))) (drop (inc y) (td x))])
yxvs (for [y (range 1 my) x (range 1 mx)] [y x ((nd y) x)])
fn1 (fn [a [y x v]]
(if (some #(every? (partial > v) %) (views y x))
(inc a) a))
fn2 (fn [a [y x v]]
(let [dist #(if (< %2 v) (inc %) (reduced (inc %)))]
(max a (->> (views y x)
(map #(reduce dist 0 %))
(reduce *)))))]
(prn {:one (reduce fn1 (+ (* 2 my) (* 2 mx)) yxvs)
:two (reduce fn2 0 yxvs)}))
;{:one 1785, :two 345168}
Late to the party; here's the meat of the solution:
(defn d8-part-2-distance-can-see-in-direction
[direction]
(let [[this-h & rst] (direction)]
(->> rst
(take-until (fn [h] (>= h this-h)))
(count))))
(defn d8-viewing-distances [tree-map x y]
(let [{:heights/keys [north
south
west
east]}
(d8-heights-fns tree-map x y)]
(* (d8-part-2-distance-can-see-in-direction north)
(d8-part-2-distance-can-see-in-direction south)
(d8-part-2-distance-can-see-in-direction west)
(d8-part-2-distance-can-see-in-direction east))))
(defn d8-part-2-solve
[input]
(let [tree-map
(d8-parse-input input)
{:keys [height width]}
(d8-dimensions tree-map)]
(->> (for [x (range 1 (dec width))
y (range 1 (dec height))]
(d8-viewing-distances tree-map x y))
(sort)
(reverse)
(first))))
Trying to catch up, here is mine: https://github.clerk.garden/formsandlines/aoc2022-clojure/commit/73f80fe4c2b12eed7e92ba5d3863f27a180c1fef/src/advent_of_clerk/day_08.html My approach was basically to loop over a grid, consisting of the input sequence in rows and a copy transposed to columns each mapped to coordinate-indices for easier lookups. The rows and columns are reversed to consume the grid from left, right, top and bottom at the same time in each iteration, while accumulating the visible trees (part 1) or the scenic scores for each tree (part 2) in a map. I’ll have to check what others have done, there are certainly much simpler and more performant solutions.
I just went with scanning each row and column both ways while obtaining the columns by transposition, then reducing the 2d result, it's dumb but it works 😄
(defn four-way [comb f]
(let [sides (fn [ts] (map comb (f ts) (reverse (f (reverse ts)))))]
(mapcat (partial map comb)
(map sides data)
(apply map list (map sides (apply mapv vector data))))))
(println "1:" (->> (four-way #(or % %2) seen) (filter true?) count))
(println "2:" (->> (four-way * scenic) (reduce max)))
save a few bytes.(let [d (->> (slurp "resources/202208")
(re-seq #"[^\n]+")
(map #(->> (re-seq #"\d" %) (map parse-long))))
seen (fn [row]
(loop [[h & t] row, top -1, r []]
(if h
(recur t (max top h) (conj r (> h top)))
r)))
scenic (fn [row]
(loop [[h & t] row, r []]
(if h
(recur t
(let [l (count (take-while #(< % h) t))]
(conj r (if (= l (count t)) l (inc l)))))
r)))
four-way (fn [comb f]
(let [rowfn (fn [row] (map comb (f row) (reverse (f (reverse row)))))]
(mapcat (partial map comb)
(map rowfn d)
(apply map list (map rowfn (apply map list d))))))]
[(->> (four-way #(or % %2) seen) (filter true?) count)
(->> (four-way * scenic) (reduce max))])
minor improvement. count forward in scenic.I went with reducing using coordinates, ugly but still manages to stay fairly concise (and below the 26 line limit : ):
(ns aoc.day-08
(:require [clojure.string :refer [split]]))
(let [nd (mapv #(mapv (comp parse-long str) %) (split (slurp "../day_08.data") #"\n"))
td (apply mapv vector nd)
[my mx] [(dec (count nd)) (dec (count td))]
views (fn [y x] [(reverse (take x (nd y))) (drop (inc x) (nd y))
(reverse (take y (td x))) (drop (inc y) (td x))])
yxvs (for [y (range 1 my) x (range 1 mx)] [y x ((nd y) x)])
fn1 (fn [a [y x v]]
(if (some #(every? (partial > v) %) (views y x))
(inc a) a))
fn2 (fn [a [y x v]]
(let [dist #(if (< %2 v) (inc %) (reduced (inc %)))]
(max a (->> (views y x)
(map #(reduce dist 0 %))
(reduce *)))))]
(prn {:one (reduce fn1 (+ (* 2 my) (* 2 mx)) yxvs)
:two (reduce fn2 0 yxvs)}))
;{:one 1785, :two 345168}
I feel like I get schooled every day by your solutions @UP82LQR9N
challenging myself and trying to find ways to cut a few bytes off your solutions @UP82LQR9N - a possible alternative to the reducing function for day 7 using fnil
and reductions
:
(defn parser [state [_ cmd path filesize]]
(if (= "cd" cmd)
(if (= path "..")
(update state :path pop)
(update state :path conj path))
(let [size (parse-long filesize)]
(reduce #(update-in %1 [:size %2] (fnil + 0) size)
state
(rest (reductions conj [] (:path state)))))))
hey hey, this time I schooled @UP82LQR9N, not the other way round, then they schooled me on my solution though ;D
ah that was your solution @UJEK1RGJ0 ? well pardonne moi - I stand corrected, I get schooled by both of you : )
@U4VDXB2TU I see you like concise solutions. After day 2 I decided not to go above 26 lines as a challenge to myself. Maybe you'll be interested in my AoC repo: https://github.com/nooga/aoc2022