adventofcode 2022-12-10

set the channel topic: Happy Advent 2022! Please put answers in the pinned threads or create one if it does not exist yet. | https://github.com/adventofcode-clojurians/adventofcode-clojurians | Join the private leaderboard with code 217019-4a55b8eb

(updated the year)

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

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

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

man once a software developer gets a little power ….

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

yeah the missing ones aren’t drawable in these constraints perhaps, too bad about the license though πŸ˜‰

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

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

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

πŸ‘ 4
πŸ‘πŸ» 1
🀯 4
❀️ 1
πŸ™Œ 1

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

(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

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

@tws 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

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

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

til abs is in Clojure 1.11

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

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

@nbardiuk ahhhh yeah, now it looks perfect

thanks!

πŸ‘ 1

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

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

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

yep, i took similar approach but theirs is less cryptic βž•

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

(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)))

I just did a bad thing, new elfscript library πŸͺΈ

(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

@xnooga oh yeah it is the same

@tws had a better idea about parsing the input though

So you just piped it to chatgpt eh?

for the OCR πŸ˜‰

(->> (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

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

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

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

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

I’m happy with my solution. The one trick I like from @tws 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

(cycle (range 40)) β€” makes it even more functionalish

Okay, I was happy with my solution, for, like, 3 seconds. But I pushed the β€œreplace instruction with 0” trick from @tws 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

That’s very readable

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

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.

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.

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

πŸ˜‚ 2

"β–“" "β–‘" looks nicer give it a try

πŸš€ 1

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

;; 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

6) 3360 **********  Nazarii Bardiuk
  7) 3360 **********  Aleksandr ZhuravlΡ‘v
😁

🀝 1

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

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

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

πŸ‘ 1