This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-10
Channels
- # adventofcode (68)
- # babashka (6)
- # babashka-sci-dev (3)
- # beginners (11)
- # biff (8)
- # circleci (13)
- # clj-kondo (1)
- # clojure (17)
- # clojure-europe (9)
- # clojurescript (12)
- # core-async (17)
- # cursive (9)
- # data-science (1)
- # datomic (3)
- # emacs (10)
- # fulcro (4)
- # off-topic (3)
- # practicalli (1)
- # releases (3)
- # sql (1)
- # xtdb (30)
;; 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
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
@U03KMPFU4TH you can replace map
+ flatten
with mapcat
which concatenates sequences produced by map
into single sequence
Munge the string, eval, reductions: https://github.com/tschady/advent-of-code/blob/main/src/aoc/2022/d10.clj
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.
(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)))
Missed the last two days - too much work but this one was fun. https://github.com/zamansky/advent2022/blob/main/src/day10.clj
@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 +)))
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))))
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
@U076FM90B ahhhh yeah, now it looks perfect
@U1Z392WMQ (str/replace #"(noop|addx)" "0"))
- that's clever.
I kinda assumed that part2 would involve different commands and command-durations...
https://github.com/bhauman/advent-of-code-2022/blob/main/src/adv2022/day10/sol.clj
@U1Z392WMQ has my favorite solution. So a big thumbs up there!
(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 :coral:
(defn part-2 [input]
(->> (reg-series input)
(map-indexed draw-pixel)
(partition 40)
(map (partial apply str))
(elfscript/ocr)))
#_(part-2 input); "FCJAPJRE"
@UJEK1RGJ0 oh yeah it is the same
@U1Z392WMQ had a better idea about parsing the input though
(->> (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
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
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
(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 @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
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.
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 …
@bhauman yeah, this crazy one is 12k lines long! https://github.com/betaveros/noulith/blob/main/src/lib.rs
https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2022/day10.clj
My day 10 https://github.com/stuartstein777/clj-advent-of-code/blob/master/src/stuartstein777/2022/day10.clj
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
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 😉