Fork me on GitHub
#adventofcode
<
2022-12-10
>
norman05:12:49

Day 10 - Solutions

Apple05:12:22

;; 202210
(let [pos [20 60 100 140 180 220]
      d (->> (slurp "resources/202210") (re-seq #"(addx) (-?\d+)|(noop)")
             (reduce (fn [r [_ addx val noop]]
                       (let [v (peek r)]
                         (if addx
                           (conj r v (+ v (parse-long val)))
                           (conj r v))))
                     [1]))
      f (fn [i v] (if (<= -1 (- v (mod i 40)) 1) "▓" "░"))]
  (->> (map-indexed f d)
       (partition 40)
       (mapv (comp println str/join)))
  (->> (map (comp d dec) pos) (map * pos) (reduce +)))

;; ▓▓▓░░▓▓▓▓░░▓▓░░▓▓▓░░▓░░▓░▓▓▓░░▓▓▓▓░▓▓▓░░
;; ▓░░▓░░░░▓░▓░░▓░▓░░▓░▓░▓░░▓░░▓░▓░░░░▓░░▓░
;; ▓░░▓░░░▓░░▓░░░░▓░░▓░▓▓░░░▓░░▓░▓▓▓░░▓▓▓░░
;; ▓▓▓░░░▓░░░▓░▓▓░▓▓▓░░▓░▓░░▓▓▓░░▓░░░░▓░░▓░
;; ▓░░░░▓░░░░▓░░▓░▓░░░░▓░▓░░▓░░░░▓░░░░▓░░▓░
;; ▓░░░░▓▓▓▓░░▓▓▓░▓░░░░▓░░▓░▓░░░░▓▓▓▓░▓▓▓░░
;; 13680

👏 5
alekszelark07:12:12

6) 3360 **********  Nazarii Bardiuk
  7) 3360 **********  Aleksandr Zhuravlёv
😁

🤝 1
Joe Dumont07:12:05

I learned reduction from yesterday and got to use it, but unfortunately I used flatten so I'll probably have to rework it to avoid the curse. https://gist.github.com/iwrotesomecode/64da2ce8702d62d5557f030ce1752131

nbardiuk07:12:29

@U03KMPFU4TH you can replace map + flatten with mapcat which concatenates sequences produced by map into single sequence

Joe Dumont07:12:56

Ah yes! Thank you for saving me from dismay, @U076FM90B

👍 1
tylerw08:12:04

Probably would've done things differently had I known what part 2 was.

tschady08:12:39

Munge the string, eval, reductions: https://github.com/tschady/advent-of-code/blob/main/src/aoc/2022/d10.clj

🤯 4
👏 4
1
🙌 1
❤️ 1
Benjamin09:12:14

https://github.com/benjamin-asdf/advent-of-code/blob/master/src/Y2022/day10.clj thought I had a cool solution, after seeing @UP82LQR9N and @U1Z392WMQ I feel humbled and educated.

nooga12:12:37

(ns day10)

(def data (->> "input/day10.txt" slurp lines 
               (mapcat #(if (= % "noop") [0] [0 (-> % (split " ") second parse-long)]))
               (reductions + 1)))

(println "1:" (reduce + (map #(* % (nth data (dec %))) (range 20 260 40))))
(doseq [l (->> data (map-indexed #(if (-> %2 (- (mod %1 40)) abs (<= 1)) "#" ".")) (partition 40))]
  (println "2:" (apply str l)))

🙌 1
zamansky12:12:07

Missed the last two days - too much work but this one was fun. https://github.com/zamansky/advent2022/blob/main/src/day10.clj

alekszelark13:12:05

@U1Z392WMQ made your parsing function a little bit less dangerous 😅

(defn parse-input [input]
  (->> (str/replace input #"noop|addx" "0")
       (str "1 ") ; add init state
       (aoc/parse-longs)
       (reductions +)))

🙌 1
Vincent Olesen13:12:19

Used match which turned out nice. Tried out using reductions, but simply reducing over the instructions and adding two register States for addx seemed simpler, as my goal was to collect register states for every cycle. https://github.com/volesen/aoc2022/blob/main/day10/day10.clj

tschady13:12:26

fine, less dangerous, less fun, more readable:

(defn reg-series [input]
  (let [deltas (-> input (str/replace #"noop|addx" "0") s/ints)]
    (reductions + 1 deltas)))

(defn part-1 [input]
  (->> [20 60 100 140 180 220]
       (map #(* % (nth (reg-series input) (dec %))))
       (reduce +)))

(defn draw-pixel [cursor cycle]
  (let [dist (abs (- (mod cursor 40) cycle))]
    (if (<= dist 1) \# \.)))

(defn part-2 [input]
  (->> (reg-series input)
       (map-indexed draw-pixel)
       (partition 40)
       (map (partial apply str))))

👏 3
2
tschady13:12:39

til abs is in Clojure 1.11

Felipe14:12:50

I wonder if I have an off-by-one error somewhere or this just looks hard to read

nbardiuk14:12:22

try changing (<= (dec x) (mod cycle 40) (+ x 2)) to (<= (dec x) (mod cycle 40) (+ x 1)) the size of sprite is 3 and x is in the middle

Felipe14:12:15

@U076FM90B ahhhh yeah, now it looks perfect

Felipe14:12:18

thanks!

👍 1
mac15:12:23

@U1Z392WMQ (str/replace #"(noop|addx)" "0")) - that's clever.

leinfink16:12:44

I kinda assumed that part2 would involve different commands and command-durations...

bhauman16:12:24

@U1Z392WMQ has my favorite solution. So a big thumbs up there!

nooga16:12:03

yep, i took similar approach but theirs is less cryptic

bhauman16:12:50

well you could add some newlines… help a reader out!

nooga17:12:14

(ns day10)

(def data (->> "input/day10.txt" slurp lines
               (mapcat #(if (= % "noop") 
                          [0] 
                          [0 (-> % (split " ") second parse-long)]))
               (reductions + 1)))

(println "1:" (reduce + (map #(* % (nth data (dec %))) 
                             (range 20 260 40))))

(doseq [l (->> data 
               (map-indexed #(if (-> %2 (- (mod %1 40)) abs (<= 1)) 
                               "#" 
                               ".")) 
               (partition 40))] 
  (println "2:" (apply str l)))

tschady17:12:35

I just did a bad thing, new elfscript library :coral:

(defn part-2 [input]
  (->> (reg-series input)
       (map-indexed draw-pixel)
       (partition 40)
       (map (partial apply str))
       (elfscript/ocr)))

#_(part-2 input); "FCJAPJRE"

1
😂 2
bhauman17:12:53

@UJEK1RGJ0 oh yeah it is the same

nooga17:12:14

@U1Z392WMQ had a better idea about parsing the input though

bhauman17:12:32

So you just piped it to chatgpt eh?

bhauman17:12:58

for the OCR 😉

bhauman17:12:27

(->> (edn/read-string (str "[" (slurp "src/adv2022/day10/input.txt") "]"))
     (map #(if (symbol? %1) 0 %1))
     (reductions + 1))

;; this is a take on the solution by @tschady 

🙌 1
nooga17:12:29

well, I don't have read-string and anything like that so I need to parse "by hand"

bhauman17:12:11

oh I get it, I’m iterating on the idea

bhauman17:12:54

hmmmm seems like you already have a reader in there. oh but is it a compiled lexer or something?

nooga17:12:10

you mean in let-go? well I have a full clojure reader written in Go, but it's not callable from the language itself

👍 1
wevrem17:12:31

I’m happy with my solution. The one trick I like from @U1Z392WMQ is replacing 'addx and 'noop with 0. https://github.com/wevre/advent-of-code/blob/master/src/advent_of_code/2022/day_10_signals.clj

1
alekszelark18:12:45

(cycle (range 40)) — makes it even more functionalish

wevrem18:12:44

Okay, I was happy with my solution, for, like, 3 seconds. But I pushed the “replace instruction with 0” trick from @U1Z392WMQ a little further and did some refactoring for something even simpler. I remembered I had written a common/parse-longs method from prior years, which lets me drop edn/read-string and I also did less math, meaning I don’t rely as much on mod and abs and more on mapping indices (-1, 0, 1) to characters. That was a trick I saw you geniuses using for the rope problem from yesterday and I think it is a good fit for these problems that are simple integer x- and y-coordinates. https://github.com/wevre/advent-of-code/blob/master/src/advent_of_code/2022/day_10_signals.clj

bhauman18:12:45

That’s very readable

Mark Wardle18:12:39

Forgot to post. Like someone else in this thread, I thought part 2 might have lots of additional commands, so overengineered using multi methods. Using advice from prior days, reductions remains a superpower. https://github.com/wardle/aoc2022/blob/main/src/day10.clj

Mark Wardle18:12:04

Can’t imagine the mess I’d create if using Java to solve these, and the need to refactor entirely when part 2 isn’t what you expect.

Alex Alemi18:12:42

Not nearly as concise as the other ones here: https://github.clerk.garden/alexalemi/clerktudes/commit/85191e17481bf7a2dc7d441d14df5cfa4977d858/notebooks/advent-2022-10.html I built a lazy-seq of the cycles of the machine, simple transducer for part 1, some threading for part 2.

chucklehead19:12:12

might not be the most time I've spent on a captcha, but it has to be close

😂 2
Apple19:12:27

"▓" "░" looks nicer give it a try

🚀 1
bhauman01:12:16

It might be interesting to distill the lessons all of the years of AOC contests into a combinator style DSL for solving these problems. Oh wait we already have that …

👍 1
Felipe15:12:15

(updated the year)

tschady17:12:39

Note: this is not open source software. I want $5000 USD for a 1 year license. (elfscript/ocr) https://github.com/tschady/advent-of-code/blob/main/src/aoc/elfscript.clj

😆 7
🤑 2
bhauman17:12:45

well that’s pretty crappy deal, but if you had all the letters then we’re talking …

tschady17:12:38

OK, $10k for @bhauman, $5k for everybody else

bhauman17:12:07

man once a software developer gets a little power ….

tschady17:12:41

I actually think this is the full set, I pulled a few years input from a handful of people

bhauman17:12:27

yeah the missing ones aren’t drawable in these constraints perhaps, too bad about the license though 😉

bhauman17:12:19

The Elfscript license: This software is only free if used between the days of Dec 1 to 25th EST. Beyond that its $1000 per render. (There is an online API renderer, that’s only $5 a month)

😆 2