Fork me on GitHub
#adventofcode
<
2022-12-14
>
wevrem05:12:18

Day 14 - Solutions

wevrem05:12:51

This is so sloppy and fast because I want to get to bed so I can go snowboarding tomorrow. Yay! https://github.com/wevre/advent-of-code/blob/master/src/advent_of_code/2022/day_14_sand.clj

1
1
🏂 2
Casey09:12:49

My solution is coming along. Got the parsing, ASCII visualization and sand falling.. trying to sort out how to detect the termination condition

Casey09:12:11

One could easily cheese it by hard coding based on your specific input.. but a general solution would be nice

Callum Oakley09:12:54

adapted my solution to 2018 day 17, so I don’t know if this is necessarily the most natural algorithm. doesn’t drop a unit of sand at a time, instead keeps track of areas of flowing sand and settled sand. in 2018 the “drop at a time” approach was too slow so I’d be curious to know how long it takes today. this one is ~200ms for part 2 (could certainly speed it up by drawing whole triangles at a time when there are no obstacles!) https://github.com/callum-oakley/advent-of-code/blob/main/src/aoc/2022/14.clj

Casey10:12:36

Got it! This one was very fun. https://github.com/Ramblurr/advent-of-code/blob/main/src/aoc/2022/day14.clj @U01HL2S0X71 For part 2 my input and algo takes ~4.5 seconds on my computer.

👌 1
alekszelark11:12:36

@U01HL2S0X71 “Elapsed time: 7734.680185 msecs”

👍 1
Felipe11:12:42

did a-step-at-a-time, got 3 seconds for both parts here (on a m1 mac, which is quite fast)

genmeblog12:12:54

what a cave!

👍 4
Felipe13:12:16

@U1EP3BZ3Q looks nice! I also remember seeing some very cool visualizations of the mountain climbing one on reddit

Mark Wardle13:12:19

I didn't use a grid, but just used sets representing the blocks and so looped adding sand along the way and naively faking an infinite floor. https://github.com/wardle/aoc2022/blob/main/src/day14.clj

zamansky13:12:07

Pretty straightforward but you can always make your life more difficult by writing this:

(apply max (first  (keys world))
to find the maximum x coord in the world instead of this
(apply max (map first  (keys world)
D'oh!!!! https://github.com/zamansky/advent2022/blob/main/src/day14.clj

wevrem14:12:12

I got up and thought I might feel compelled (shamed?) to do major overhaul, but upon review I think my original approach holds up well, and part 2 only takes about 3s. I got rid of the debug stuff scattered throughout and cleaned up some function signatures and I’m happy with it. https://github.com/wevre/advent-of-code/blob/master/src/advent_of_code/2022/day_14_sand.clj

wevrem15:12:57

That picture for part 2, @U02P133R2SZ, makes me wonder if it could be solved by subtracting triangles, instead of simulating grains of sand, like what @U01HL2S0X71 was saying. I haven’t done Year 2018 yet, but I seem to recall some puzzle that required adding/subtracting/combining rectangular regions. Hmmm.

alekszelark15:12:21

@UTFAPNRPT triangles are not a problem I guess, but what about these shapes?

wevrem15:12:45

That’s what makes it so interesting. Is there is a way to determine from the vertical and horizontal barriers which triangles (and maybe half triangles) to add and subtract? I’m not saying I’m going to spend the brain power to go figure it out, but it seems like there might be a solution there. (Of course, that is what I thought about normalizing distress signal packets from yesterday, and that turned out to be a fool’s errand. 🙃)

schadocalex17:12:11

You could remove the "white" triangle below horizontal lines, but then you have to manage some edge cases where sands can’t reach some areas due to vertical lines.

tschady22:12:38

Nothing special. Exact floor width needed (plus one for aesthetics). I did like this:

(defn simulate-add-sand [cave]
  (let [box (grid/bounds cave)]
    (loop [sand source]
      (cond
        (grid/ob? box sand)                   :infinite
        (nil? (get cave (m/add sand [0 1])))  (recur (m/add sand [0 1]))
        (nil? (get cave (m/add sand [-1 1]))) (recur (m/add sand [-1 1]))
        (nil? (get cave (m/add sand [1 1])))  (recur (m/add sand [1 1]))
        (= sand source)                       :blocked
        :else                                 (conj cave {sand \o})))))

(defn sim-p2-frames [input]
  (->> (parse-cave input)
       (add-floor)
       (iterate simulate-add-sand)
       (take-while #(not= :blocked %))))
Lots of optimizations to pursue, but I’d rather work on the Clojure2D viz. https://github.com/tschady/advent-of-code/blob/main/src/aoc/2022/d14.clj

genmeblog10:12:55

Finally... Used java.util.TreeSet for gathering blocking sand/rock from given column. https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2022/day14.clj Clojure2d viz part at the end.

Miķelis Vindavs13:12:35

It’s great. The core idea is very similar to threading (`->>`) in clojure, but with partial application as the default

Apple13:12:17

culi 8 hours ago | next [–]

I just checked the 2nd place[^0] person and they... also have their own programming language...[^1]
What's going on here? Are they just extra motivated to show off their languages? Is making your own programming language more common than I realized?

EDIT: same with the 4th place person[^2]

EDIT2: same with the 7th place[^3]. Btw this is the 4th person that's clickable so actually 3/4 leaders with their GHs linked have their own programming languages

EDIT3: jonahx pointed out I made a mistake and the 2nd place person didn't actually create Vyxal, just used (and contributed) to it

[^0]: 

[^1]: 

[^2]: 

[^3]: 

russmatney15:12:57

these langs (and code-golf, i.e. going for smallest program byte-size) are mind-blowing! i'd never understood it until this year (i'd always associated the term with vim-golf)