adventofcode

genmeblog 2021-12-13T09:28:42.093500Z

if you want to avoid if for folding (day13) you can use below formula (spoiler in thread)

👍 4
genmeblog 2021-12-13T09:29:00.093600Z

(- a (m/abs (- v a))) - v - value, a - axis (edited a little bit)

Aleks 2021-12-13T04:49:49.083400Z

🧵Day 13 answers thread: post your answers here

nbardiuk 2021-12-13T09:18:07.091100Z

I've enjoyed folding https://github.com/nbardiuk/adventofcode/blob/master/2021/src/day13.clj

👏 1
🤩 2
Antonio Bibiano 2021-12-13T09:22:51.091700Z

I'm happy about my folding 🙂 not as much about my rendering :D

genmeblog 2021-12-13T09:42:32.094200Z

🤩 2
Aleks 2021-12-13T09:55:33.094800Z

@nbardiuk I also enjoyed your solution, nice trick to avoid any if’s

nbardiuk 2021-12-13T10:11:46.095300Z

the fold-one trick is from @tsulej https://clojurians.slack.com/archives/C0GLTDB2T/p1639387722093500

3
2021-12-13T11:05:19.095800Z

parsing and drawing could be more concise but I'm fine with the folding in one line of specter https://github.com/IamDrowsy/aoc-2021/blob/main/src/aoc2021/d13.clj

👍 1
tschady 2021-12-13T13:32:47.096600Z

Nothing new to offer really, I originally did a map, but copied @drowsy’s specter since I’m a fan. https://github.com/tschady/advent-of-code/blob/main/src/aoc/2021/d13.clj

Joe 2021-12-13T14:15:16.097100Z

Bleh, not super happy with it. But it does work fine. On reflection, I made life difficult for myself by making the calculation for the post-fold coordinates based on the size of the paper, which meant I had to keep track of the size of the paper (since you can't determine it from the coordinates alone, though I didn't realize that at first) https://github.com/RedPenguin101/aoc2021/blob/main/clojure/src/aoc2021/day13.clj

Joe 2021-12-13T14:28:09.097600Z

In fact, I just refactored that out, now it's much nicer 🙂 thanks @tsulej

1
2021-12-13T14:56:20.097800Z

@tws you don't actually need the turn the transform result into a set. As long as the initial input is already a set. specter will keep types.

Sam Adams 2021-12-13T15:22:54.098Z

I liked this one too; printing the letters was very satisfying. https://samadams.dev/2021/12/13/advent-of-code-day-13.html

2021-12-13T19:53:49.100100Z

https://github.com/kfirmanty/advent-of-code-2021/blob/main/src/day13.clj day13 solution judging from if you want to avoid if... message identical to others 😄 Just printing the board for part2 and reading it manually felt like kind of cheating but I guess that is what most of the people did 😄

tschady 2021-12-13T20:16:47.100500Z

in years past i considered doing an “OCR” on the text by comparing to a fingerprint, but he changes the letter size and font often so it’s not worth it.

Antonio Bibiano 2021-12-13T20:52:49.101300Z

Maybe one can generate an image and send it off to an OCR api :D

2021-12-13T22:22:59.101500Z

Pretty easy one today

(ns stuartstein777.2021.day13
  (:require [stuartstein777.file :as f]
            [stuartstein777.utils :as u]
            [clojure.string :as str]
            [clojure.set :as set]))

(defn parse-coord [line]
  (->> (str/split line #",")
       (map #(Integer/parseInt %))))

(defn parse-folds [line]
  (let [[xy n] (u/frest (re-seq #"fold along (x|y)=(\d+)" line))]
    [xy (Integer/parseInt n)]))

(defn folder [xy n [x y]]
  (cond (and (= xy "x") (> x n))
        [(- n (Math/abs (- n x))) y]

        (and (= xy "y") (> y n))
        [x (- n (Math/abs (- n y)))]
        
        :else
        [x y]))

(defn row->str [max-x row]
  (let [xs (set (map first row))]
     (map (fn [n] (if (xs n) "⭐" "⬛")) (range (inc max-x)))))

(defn print [coords]
  (let [max-x (apply max (map first coords))]
    (->> coords
         (group-by #(second %))
         (sort-by key)
         (vals)
         (map (partial row->str max-x))
         (map (partial apply str))
         (str/join "\n")
         (println))))

(defn fold [coords [xy n]]
  (map (partial folder xy n) coords))

(let [input (->> (slurp "puzzle-inputs/2021/day13")
                 (str/split-lines))
      coords (->> input
                  (take-while #(not= "" %))
                  (map parse-coord))
      folds (->> input
                 (drop (inc (count coords)))
                 (map parse-folds)
                 #_(take 1))] ; uncomment to solve part 1
  (->> (reduce fold coords folds)
       distinct
       #_count ; uncomment to solve part 1
       print))

3
2021-12-13T23:50:52.102600Z

Oh, i see from @tsulej, I don't actually need the ((and (= xy "x") (> x n)) statements inside my cond in folder! Nice

1
2021-12-13T23:51:46.102900Z

I just need the (= xy "x") and (= xy "y")

Andrew Byala 2021-12-14T02:48:02.105300Z

Great solution, @mikelis.vindavs. I enjoyed both how you parsed your data with partition and especially with re-seq, without splitting the lines. For the fold function, your fold' made the map really clear. Thanks - I learned a lot from your tiny bit of code!

🙇🏻 1
kevinc 2021-12-14T02:49:10.105600Z

https://github.com/kconner/advent-of-code/blob/master/2021/13a.cljhttps://github.com/kconner/advent-of-code/blob/master/2021/13b.clj. I was inspired by one of yesterday's posts to compose all the folds into one, though I didn't go as far as using a transducer.

Miķelis Vindavs 2021-12-14T04:59:56.106600Z

Thanks @abyala, that’s nice to hear! I only picked the partition idea up myself a few days ago from another solution 🙂. Normally I also approach things top-down.

AC 2021-12-13T06:09:43.086600Z

this was a fun one..

(ns aoc21.day13
  (:require [ :as io]
            [clojure.string :as s]))

;; part 1

(defn parse-fold [s]
  (let [[_ var line] (re-matches #"fold along (.)=(\d+)" s)]
    [(keyword var) (Integer/parseInt line)]))

(defn make-grid [dots]
  (reduce (fn [g xy] (assoc g xy \X)) {} dots))

(defn input-data [filename]
  (let [[dots folds] (s/split (slurp (io/resource filename)) #"\n\n")]
    [(make-grid (partition 2 (map #(Integer/parseInt %) (re-seq #"\d+" dots))))
     (map parse-fold (s/split-lines folds))]))

(defn fold-y [grid offset]
  (into {} (map (fn [[[y x] _]] (cond (= x offset) nil
                                      (< x offset) [[y x] \X]
                                      (> x offset) [[y (- offset (- x offset))] \X]))
                grid)))

(defn fold-x [grid offset]
  (into {} (map (fn [[[y x] _]] (cond (= y offset) nil
                                      (< y offset) [[y x] \X]
                                      (> y offset) [[(- offset (- y offset)) x] \X]))
                grid)))

(defn fold [grid [axis offset]]
  (if (= axis :x)
    (fold-x grid offset)
    (fold-y grid offset)))

(defn soln-1 [filename]
  (let [[grid folds] (input-data filename)]
    (count (fold grid (first folds)))))

;; part 2

(defn dump-grid [grid]
  (let [[max-y max-x] (reduce (fn [[my mx] [[y x] _]]
                                [(max my y) (max mx x)])
                              [0 0]
                              grid)]
    (doseq [x (range (inc max-x))]
      (doseq [y (range (inc max-y))]
        (print (get grid [y x] \.)))
      (print "\n"))))

(defn soln-2 [filename]
  (let [[grid folds] (input-data filename)
        text (reduce (fn [g move] (fold g move)) grid folds)]
    (dump-grid text)
    (count text)))

peterc 2021-12-13T05:44:55.085700Z

Took me too long to render my result - spoiler in thread

peterc 2021-12-13T05:45:03.085800Z

peterc 2021-12-13T05:45:51.086200Z

This is what I started with

Miķelis Vindavs 2021-12-13T06:23:25.087300Z

The console is rotated 90deg? 😄

peterc 2021-12-13T06:37:10.087800Z

and mirrored 😄

Aleks 2021-12-13T06:49:00.089500Z

fold along day=13:christmas_tree:

⤴️ 2
↩️ 3
Aleks 2021-12-13T16:37:26.098400Z