adventofcode

2021-12-02T14:27:05.109400Z

(Itโ€™s technically a solution, but I doubt most can read awk.)

๐Ÿ‘€ 1
roelof 2021-12-02T14:31:11.110Z

I cannot read and understand it

2021-12-02T14:32:01.110300Z

You want me to explain them?

2021-12-02T14:32:26.110700Z

day-2 is pretty straightforward

roelof 2021-12-02T14:34:25.111100Z

you mean the awk syntax. No need to

2021-12-02T14:35:01.111600Z

Too late

๐Ÿ˜‚ 1
2021-12-02T14:35:03.111800Z

# For lines that start with "u", subtract the second token (i.e. the number)
# from variable "d".
/^u/ {d-=$2}
# For lines that start with "d", add the second token (i.e. the number)
# to variable "d".
/^d/ {d+=$2}
# For lines that start with "f", add the second token (i.e. the number)
# to variable "h".
/^f/ {h+=$2} 
# At the end, print the product of "d" and "h"
END {print d*h}

๐Ÿ‘ 2
roelof 2021-12-02T14:36:00.112Z

๐Ÿ˜ข again too late

2021-12-02T14:45:51.113400Z

its like someone saw regex and thought, that should be an entire programming language in of itself.

2021-12-02T14:46:06.113600Z

I can't tell yet if I love it or hate it

2021-12-02T14:47:01.113800Z

Oh awk is actually awesome.

2021-12-02T14:47:34.114Z

I use it less than I used to, but you can really do a lot of work w/ it if you want.

2021-12-02T14:47:47.114300Z

Wellโ€ฆ circumstantially.

2021-12-02T14:48:26.114600Z

People at my work when they saw some CLojure were like "NOPE!, NOT HAPPENING"

2021-12-02T14:48:37.115Z

I can't imagine if I showed something I'd written in AWK or APL

2021-12-02T14:49:18.115800Z

Hell, they even said no chance to F#

roelof 2021-12-02T14:49:21.116100Z

also I also not. Can read clojure a little bit and some ruby

2021-12-02T14:50:00.117Z

I've basically realised they aren't willing to entertain trying to learn something unless the syntax basically looks like C

2021-12-02T14:50:35.117200Z

Wild to me how close-minded some people are.

2021-12-02T14:51:16.117900Z

Like, if I see something I donโ€™t like, I just say, โ€œYeah I donโ€™t really like that or want to spend my time on that.โ€ But to respond, โ€œNOOO. THATโ€™S TERRIBLE. ARE YOU KIDDING?โ€ is nuts.

roelof 2021-12-02T14:51:57.119800Z

im afraid a lot of people are closed minded. Even if I look at the dicussion take a vaccin or not. Looks like there are 2 sides and both said there are right and will not listen to the other side

2021-12-02T14:54:02.120400Z

Yeah just a general lacking of curiosity/willingness to be ignorant.

2021-12-02T14:54:18.120600Z

Willingness to be ignorant is actually a sort of superpower imo.

roelof 2021-12-02T14:54:29.120800Z

or believing a source on facebook more then a expert

2021-12-02T14:54:52.121Z

To be fair, they do seem to be happy to hack away on just completely awful code, mainly because I don't think they know any better.

2021-12-02T14:56:08.121400Z

See. The real problem is that usually when people are โ€œjust hacking away on completely awful codeโ€ very little (or nothing) is actually getting done.

2021-12-02T14:56:13.121600Z

like a program we had internally that downloaded a zip file, and extracted it to disc and ran an exse in the folder was 1000+ lines of code.

roelof 2021-12-02T14:56:33.122300Z

WAUW

2021-12-02T14:56:34.122500Z

1000 lines???

2021-12-02T14:56:46.122900Z

yes, thats not a typo, it was over 1000 lines of code

2021-12-02T14:56:53.123200Z

idk, Iโ€™ll give a benefit of doubt

2021-12-02T14:57:04.123400Z

things that actually run forever are often more complicated than one might assume

2021-12-02T14:57:21.123600Z

error checking/recovery can occupy a lot of space

roelof 2021-12-02T15:00:47.125200Z

that is right

2021-12-02T14:42:10.112800Z

Oh @twsโ€™s Day 2 is real nice.

๐Ÿ’ฏ 1
๐Ÿ‘ 1
โค๏ธ 3
2021-12-02T14:42:34.113100Z

I think you win. ๐Ÿ˜„

2021-12-02T14:49:41.116800Z

Youโ€™re supposed to sum the partition. Youโ€™ve clearly seen some math trick that I donโ€™t see.

2021-12-02T14:51:00.117400Z

if you have A B C D E F and you partition it by 3 and a step of 1 you get (A B C) (B C D) (C D E) etc

2021-12-02T14:51:15.117700Z

so only 1 is actually different

2021-12-02T14:51:29.118500Z

so you can jsut take diff of 1st item and 3rd item

2021-12-02T14:51:43.119100Z

i'm guessing thats what his offset of dropping 3 is doing ?

tschady 2021-12-02T14:51:44.119300Z

The picture in the problem showed me. The B and C lined up, don't need to add them.

๐Ÿ‘ 4
2021-12-02T14:51:54.119600Z

clever

2021-12-02T14:52:38.120Z

clever

2021-12-02T14:52:47.120200Z

I like your style.

borkdude 2021-12-02T15:00:04.124600Z

very clever :)

tschady 2021-12-02T15:17:02.128500Z

thanks2

Ben Sless 2021-12-02T15:59:39.130300Z

yeah, math!

Aleks 2021-12-02T18:07:25.138300Z

@potetm thanks for sharing @twsโ€™s solution, itโ€™s really neat and smart.

2
tschady 2021-12-02T18:08:13.138700Z

this is how I build my empire

Benjamin 2021-12-04T11:33:17.208500Z

sick

nooga 2021-12-02T17:32:24.135300Z

@borkdude never fails

Chase 2021-12-02T17:50:56.136900Z

parse-long is getting so much usage already. You can tell who saw the 1.11.0-alpha3 update post and who didn't. Haha

๐Ÿ‘ 2
๐Ÿ‘€ 1
๐Ÿ™‚ 1
nbardiuk 2021-12-02T18:06:28.138Z

I am waiting for usage of clojure.java.math. I've just grepped my last year solution and there are only 3 cases of Math

roelof 2021-12-02T18:11:25.139Z

I did not see that post. Is there a important change then ?

nbardiuk 2021-12-02T18:13:52.139200Z

Here is list of changes https://clojure.org/releases/devchangelog

nbardiuk 2021-12-02T18:14:36.139500Z

And here is a highlights of changes https://clojure.org/news/2021/11/24/deref

roelof 2021-12-02T18:16:26.139800Z

aha, that is why that post. parse-long is new ๐Ÿ™‚

tschady 2021-12-02T17:52:57.137600Z

i take my AOC way too seriously to use anything alpha.

๐Ÿ’ฏ 2
๐Ÿ˜† 7
borkdude 2021-12-02T20:11:27.142800Z

I added the new parse-long, parse-boolean, etc functions from clojure 1.11 in #babashka 0.6.8 in case anyone wants to play with that for AoC :)

3
๐Ÿ‘€ 1
โค๏ธ 2
ordnungswidrig 2021-12-03T10:39:29.163900Z

hehe

borkdude 2021-12-03T10:41:45.164100Z

For today's solution it would have been nice if parse-long supported the radix argument

borkdude 2021-12-03T10:41:57.164300Z

(cc @alexmiller)

ordnungswidrig 2021-12-02T21:24:18.149600Z

I just used -I and let bb to the parsing ๐Ÿ˜‰

borkdude 2021-12-02T21:51:25.149800Z

haha awesome!

2021-12-03T06:51:13.159300Z

Do you think we could ask a parse-binary in the next Clojure release?

2021-12-03T06:51:31.159500Z

.. kidding

R.A. Porter 2021-12-02T21:12:40.148300Z

Interesting observation that doesn't mean anything (though there's probably some small degree of correlation with something). A quick scan down the list of current members of the Clojurians private leaderboard (code in this channel's description if you've not joined) shows that there are a fair number of supporters of AoC, all in the top third.

tschady 2021-12-02T22:31:46.150500Z

thanks for the reminder!

R.A. Porter 2021-12-02T22:32:32.150700Z

Certainly not my intent, but ๐Ÿ‘

euccastro 2021-12-02T23:38:39.152500Z

this year I'll do these about 12 hours after the puzzles get unveiled, since I'm doing them with a friend in the EST timezone and there's less of a chance that the problems will bug me into work hours ๐Ÿ™‚

Aleks 2021-12-02T04:58:36.092800Z

๐Ÿงต Day 2 answers thread: post your answers here

Sam Adams 2021-12-04T03:52:08.200700Z

@tws thanks! Iโ€™m interspersing the code with markdown line-comments like ;; # Header, like one does with [Clerk](https://github.com/nextjournal/clerk), and then I have a little [script](https://github.com/samharad/samharad.github.io/blob/master/src/script/aoc.clj) for transforming the namespace into a markdown file. Then [Jekyll](https://jekyllrb.com/) takes it from there.

nbardiuk 2021-12-02T08:02:43.096500Z

My first implementation was similar to alekszelark, this one is already refactored https://github.com/nbardiuk/adventofcode/blob/master/2021/src/day02.clj

๐Ÿ‘ 2
Antonio Bibiano 2021-12-02T08:44:40.097200Z

I also went with a vector instead of a map because I think the argument destructuring makes it quite clear anyway

(defn answer [[distance depth]]
  (* distance depth))

(answer
 (reduce
  (fn [[distance depth] [direction step]]
    (case direction
      :forward [(+ distance step) depth]
      :down [distance (+ depth step)]
      :up [distance (- depth step)]))
  [0 0]
  input))


(answer
 (reduce
  (fn [[distance depth aim] [direction step]]
    (case direction
      :forward [(+ distance step) (+ depth (* aim step)) aim]
      :down [distance depth (+ aim step)]
      :up [distance depth (- aim step)]))
  [0 0 0]
  input))

๐Ÿ‘ 4
Callum Oakley 2021-12-02T08:47:21.097400Z

part-2 aim is identical to part-1 depth, so the same reduction works for both

(defn parse [s]
  (->> s (re-seq #"\w+") (map read-string) (partition 2)))

;; This covers parts 1 and 2: for part-1 treat aim as depth and ignore dep
(defn dive [commands]
  (reduce (fn [[hor aim dep] [op x]]
            (case op
              forward [(+ hor x) aim (+ (* aim x) dep)]
              down [hor (+ aim x) dep]
              up [hor (- aim x) dep]))
          [0 0 0]
          commands))

(defn part-1 []
  (let [[hor dep _] (->> "input/2021/02" slurp parse dive)]
    (* hor dep)))

(defn part-2 []
  (let [[hor _ dep] (->> "input/2021/02" slurp parse dive)]
    (* hor dep)))

๐Ÿ‘ 5
1
ajk 2021-12-02T08:51:35.098Z

(defn parse-input [input]
  (map (fn [line]
         (let [[direction amount] (string/split line #" ")]
           [(keyword direction)
            (Integer/parseInt amount)]))
       (string/split-lines input)))

(def input (parse-input (slurp "aoc-day-2.txt")))
(defn part-one [commands]
  (loop [commands commands
         horizontal-position 0
         vertical-position 0]
    (if-let [[direction amount] (first commands)]
      (case direction
        :forward (recur (rest commands)
                        (+ horizontal-position amount)
                        vertical-position)
        :up (recur (rest commands)
                   horizontal-position
                   (- vertical-position amount))
        :down (recur (rest commands)
                     horizontal-position
                     (+ vertical-position amount)))
      (* horizontal-position vertical-position))))
(defn part-two [commands]
  (loop [commands commands
         aim 0
         horizontal-position 0
         depth 0]
    (if-let [[direction amount] (first commands)]
      (case direction
        :forward (recur (rest commands)
                        aim
                        (+ horizontal-position amount)
                        (+ depth (* amount aim)))
        :up (recur (rest commands)
                   (- aim amount)
                   horizontal-position
                   depth)
        :down (recur (rest commands)
                     (+ aim amount)
                     horizontal-position
                     depth))
      (* horizontal-position depth))))

๐Ÿ‘ 1
2021-12-02T09:39:17.099100Z

(defn parser [l]
  (let [[dir v] (str/split l #" ")]
    {:dir dir
     :v (Integer/parseInt v)}))

; part 1
(->> (f/read-all-lines-and-parse puzzle-input parser)
     (reduce (fn [[x y] {:keys [dir v]}]
               (condp = dir
                 "forward" [(+ x v) y]
                 "up" [x (- y v)]
                 "down" [x (+ y v)])) [0 0])
     (reduce * 1))

; part 2
(->> (f/read-all-lines-and-parse puzzle-input parser)
     (reduce (fn [[x y aim] {:keys [dir v]}]
               (condp = dir
                 "forward" [(+ x v) (+ y (* aim v)) aim]
                 "up"      [x y (- aim v)]
                 "down"    [x y (+ aim v)])) [0 0 0])
     (take 2)
     (reduce * 1))

๐Ÿ‘ 1
Joe 2021-12-02T10:50:40.100400Z

https://github.com/RedPenguin101/aoc2021/blob/main/clojure/src/aoc2021/day02.clj

(defn parse [line]
  (let [x (Long/parseLong (re-find #"\d+" line))]
    (case (first line)
      \f [x 0]
      \u [0 (- x)]
      \d [0 x])))

(->> input
     (map parse)
     (apply map +)
     (apply *))
;; => 2073315

(defn proc [[x y aim] [p d-aim]]
  [(+ x p)
   (+ y (* p aim))
   (+ aim d-aim)])

(->> input
     (map parse)
     (reduce proc [0 0 0])
     (take 2)
     (apply *))
;; => 1840311528

๐Ÿ‘ 3
Antonio Bibiano 2021-12-02T10:57:53.100600Z

What do you think about parsing the file like this:

(->> "path"
     slurp
     (format "{%s}")
     read-string)

2021-12-02T10:59:29.100800Z

As I understand it, its probably fine for Advent of Code, but you wouldn't want to read untrusted input with (read-string) in real world stuff as it can execute code.

Antonio Bibiano 2021-12-02T11:02:30.101Z

ah right! it's right there in the docs ๐Ÿคฆโ€โ™‚๏ธ

nbardiuk 2021-12-02T11:09:37.101200Z

As I understand clojure.edn/read-string is safer alternative and works for AoC fine since input is just data, no function definitions, etc.

1
nooga 2021-12-02T11:23:43.101500Z

Iโ€™m just pasting the input data directly into my code i.e. (def input '(..data here..)) ๐Ÿ˜„

๐Ÿ˜‰ 1
tschady 2021-12-02T11:58:27.102400Z

https://github.com/tschady/advent-of-code/blob/main/src/aoc/2021/d02.clj same as @c.oakley108

๐Ÿ‘ 2
๐Ÿ‘Œ 5
genmeblog 2021-12-02T12:38:38.103700Z

btw. thanks @borkdude, I learned that case works on symbols without quoting...

2021-12-02T14:16:52.104700Z

Interesting to see everyoneโ€™s slight variations on the exact same thing ๐Ÿ˜„

2021-12-02T14:17:39.105600Z

@allaboutthatmace1789 Did one novel thing in that he resolved to a direction tuple (e.g. [-1 0]) in the first step, which eliminated later switches.

2021-12-02T14:18:06.105800Z

Also good to see how much mine looks like @borkdudeโ€™s. Apparently Iโ€™m not too far off the beaten path ๐Ÿ˜„

2021-12-02T14:58:33.123900Z

same as everyone else's https://github.com/FelipeCortez/advent-of-code/blob/master/2021/02.clj

2021-12-02T14:59:23.124200Z

How do people decide wether to use case , condor condp ?

2021-12-02T15:00:23.124800Z

case - closed value dispatch cond - conditional logic dispatch condp - never

2021-12-02T15:00:33.125Z

haven't profiled it, but I imagine case is always faster so I use it when I can, cond when case isn't enough, condp when cond isn't enough condp when it's cleaner than cond

borkdude 2021-12-02T15:00:56.125400Z

I considered making the most hacky version for 2 day by rewriting the input to s-expressions, (forward 2), etc and then making functions for forward, etc so I could just run the program. ;)

๐Ÿ’ฏ 3
2021-12-02T15:01:06.125700Z

I literally have no idea what condp is good for.

borkdude 2021-12-02T15:01:22.126Z

I never use condp, it's dead to me ;)

2021-12-02T15:01:26.126200Z

same

2021-12-02T15:01:36.126400Z

@borkdude same! (wrt the lispy rewriting)

2021-12-02T15:01:39.126600Z

Iโ€™ve seen people who make messes use it. But thatโ€™s it.

2021-12-02T15:02:14.127Z

@borkdude That sounds like the best kind of over-engineering tbh.

borkdude 2021-12-02T15:02:35.127200Z

;-)

2021-12-02T15:03:32.127500Z

I recently finished https://beautifulracket.com/ and it seems like Racket really encourages this

2021-12-02T15:04:06.127700Z

read strings, turn them into s-exps somehow, write your DSL logic

gabo 2021-12-02T15:09:10.128300Z

A little late to the party https://gist.github.com/galuque/b7d60d2f2ac4c9b4658b4d21f02b12df

kpav 2021-12-02T15:19:02.128800Z

Did it last night, but here it is (not at all clean or clojurey, but im still learning!) https://github.com/kwpav/advent-of-code-2021/blob/master/src/advent_of_code_2021/day02.clj

Ben Sless 2021-12-02T15:58:06.130Z

https://github.com/bsless/clj-aoc-2021/blob/master/src/bsless/clj_aoc_2021/day2.clj ๐Ÿ™ƒ

๐Ÿคฏ 1
R.A. Porter 2021-12-02T16:13:07.130500Z

I couldn't resist reimplementing part 1 after @borkdude mentioned it...

๐Ÿ‘Œ 2
borkdude 2021-12-02T16:15:55.130900Z

@coyotesqrl awesome ;)

nooga 2021-12-02T16:39:14.131500Z

Note that this wonโ€™t work in Clojure. โญ for the one who tells me why. I still donโ€™t have proper assoc/`update` and destructuring so it looks a bit weird ๐Ÿคท

borkdude 2021-12-02T16:42:05.132Z

@xnooga case doesn't expect you to quote the symbols

โž• 1
โญ 2
borkdude 2021-12-02T16:42:15.132200Z

they are literals, not evaluated

borkdude 2021-12-02T16:42:52.132400Z

so in clojure you're basically matching against (quote forward)

borkdude 2021-12-02T16:48:56.132600Z

(case 'foo
  foo 1) ;;=> 1

Chase 2021-12-02T17:08:21.133200Z

https://github.com/Chase-Lambert/aoc-clojure/blob/main/src/aoc/2021/day_02.clj Oh man, some of the answers here are great. I could not figure out how to use reduce for part-2 because I kept thinking I needed a 2 arity function but I have to track 3 things so I had to resort to my usual ugly loop/recur method

Chase 2021-12-02T17:09:41.133900Z

This Python solution for Day 2 seems so clean to me: https://www.reddit.com/r/adventofcode/comments/r6zd93/2021_day_2_solutions/hmwbtbe/

Chase 2021-12-02T17:09:59.134200Z

They made the connection that aim for part 2 could just be your depth for part 1.

Andrew Byala 2021-12-02T17:33:57.135400Z

I refactored mine to create a "mover" function which knows how to move forward, down, and up for both parts, and then plugged the mover into a common solver function that uses reduce. Curious about anyone's thoughts. https://github.com/abyala/advent-2021-clojure/blob/main/src/advent_2021_clojure/day02.clj

Ben Sless 2021-12-02T17:37:36.135700Z

Another sexpr oriented solution is to just compile the instructions:

(reduce comp (map (fn [[d n]] (fn [sub] (move sub d n))) moves))

Ben Sless 2021-12-02T17:39:14.135900Z

If you compile it, you just get function(s). Why does it matter when you do it?

Mario C. 2021-12-02T17:57:56.137700Z

My solution for day2. Maybe too much abstraction ๐Ÿค” ๐Ÿ˜…

(def puzzle-input
  (->> (str/split (input "day2.txt") #"\n")
       (mapv (fn [instruction]
               (let [[direction steps] (str/split instruction #" ")]
                 [(keyword direction) (read-string steps)])))))

(def instruction-map
  {:forward [(fn [{:keys [x] :as current-position} steps]
               (assoc current-position :x (+ x steps)))]
   :down    [(fn [{:keys [y] :as current-position} steps]
               (assoc current-position :y (+ y steps)))]
   :up      [(fn [{:keys [y] :as current-position} steps]
               (assoc current-position :y (- y steps)))]})

(def instruction-map-z
  {:forward [(fn [{:keys [x] :as current-position} steps]
               (assoc current-position :x (+ x steps)))
             (fn [{:keys [y z] :as current-position} steps]
               (assoc current-position :y (+ y (* z steps))))]
   :down    [(fn [{:keys [z] :as current-position} steps]
               (assoc current-position :z (+ z steps)))]
   :up      [(fn [{:keys [z] :as current-position} steps]
               (assoc current-position :z (- z steps)))]})

(defn move-sub [instruction-map input]
  (reduce (fn [current-position [direction steps]]
            (reduce (fn [cp f] (f cp steps))
                    current-position
                    (get instruction-map direction)))
          {:x 0 :y 0 :z 0}
          input))

(defn solution [instruction-map input]
  (let [{:keys [x y]} (move-sub instruction-map input)]
    (* x y)))

(comment
  ;; Part 1
  (solution instruction-map puzzle-input)
  ;; Part 2
  (solution instruction-map-z puzzle-input))

Sam Adams 2021-12-02T18:27:23.140800Z

My unoriginal take: https://samadams.dev/2021/12/02/advent-of-code-day-2.html

Antonio Bibiano 2021-12-02T19:40:31.141600Z

love the blogging approach ๐Ÿ™‚

tschady 2021-12-02T19:53:24.141900Z

@sam.h.adams which tool do you use to process your source to create that blog? i was looking at marginalia but it didnโ€™t quite fit.

borkdude 2021-12-02T19:54:06.142100Z

@tws If you're looking for a home-made solution: https://blog.michielborkent.nl/writing-clojure-highlighter.html

Ben Sless 2021-12-02T20:27:09.143200Z

Solved with awk for fun

BEGIN {
    x=0;
    y=0;
    a=0
    #
    t["forward"]=0;
    t["up"]=-1;
    t["down"]=1;
    #
    h["forward"]=1;
    h["up"]=0;
    h["down"]=0;
    #
    v["forward"]=1;
    v["up"]=0;
    v["down"]=0;
}
{
    a = a + t[$1]*$2
    x = x + h[$1]*$2;
    y = y + v[$1]*$2*a;
}
END {
    print x*y;
}

borkdude 2021-12-02T20:28:29.143400Z

awkful! :)

Ben Sless 2021-12-02T20:30:18.143600Z

minified!

BEGIN {x=0; y=0; a=0 t["f"]=0; t["u"]=-1; t["d"]=1; h["f"]=1; h["u"]=0; h["d"]=0; v["f"]=1; v["u"]=0; v["d"]=0;} {c=substr($1,1,1) a = a + t[c]*$2 x = x + h[c]*$2; y = y + v[c]*$2*a;} END {print x*y;}

๐Ÿ˜ 1
๐Ÿ˜ฑ 1
Ben Sless 2021-12-02T20:30:47.143800Z

Fits in a tweet

borkdude 2021-12-02T20:30:53.144Z

do it.

Ben Sless 2021-12-02T20:31:33.144200Z

https://twitter.com/_bsless/status/1466505316363821059

Ben Sless 2021-12-02T20:32:25.144500Z

Reminds me of the annoying questions from intro to C, "do it without conditionals"

borkdude 2021-12-02T20:33:49.144700Z

and now write an awk interpreter that fits in a tweet. so we can combine both tweets using tweet-def?

Ben Sless 2021-12-02T20:35:29.144900Z

I think I'll first need to bootstrap it with tweet-def itself as a dep, then just chain it, but that's cheating

Ben Sless 2021-12-02T20:36:31.145100Z

Which reminds me I wanted to solve it with core.logic

ordnungswidrig 2021-12-02T21:23:34.149400Z

Babashka and pointless (threading style)

# part one
pbpaste | bb -I -e '(->> *input* (partition 2) (reduce (fn [[x y] [c v]] (case (keyword (name c)) :forward [(+ x v) y] :down [x (+ y v)] :up [x (- y v)])) [0 0]) (apply *))'
# part two
pbpaste | bb -I -e '(->> *input* (partition 2) (reduce (fn [[x y a] [c v]] (case (keyword (name c)) :forward [(+ x v) (+ y (* a v)) a] :down [x y (+ a v)] :up [x y (- a v)])) [0 0 0]) butlast (apply *))'

borkdude 2021-12-02T21:52:25.150200Z

Awesome :)

euccastro 2021-12-02T23:57:22.153100Z

my solution to day2. I'll read your solutions now; I know there are some crazy people who avoided using reduce ๐Ÿ™‚ https://github.com/euccastro/advent-of-code-2021/blob/main/day2.clj

euccastro 2021-12-03T00:31:22.154200Z

yeah, I had forgotten about the joys of using re-seq or wrapping the whole input to read it as edn...

Cora (she/her) 2021-12-03T06:08:48.158200Z

I'm real boring https://gist.github.com/corasaurus-hex/389968ca616083ba0feb630330ba1739

๐Ÿ‘ 1
Tero Matinlassi 2021-12-06T21:37:41.355400Z

Little bit late to the game, but here goes, neverthelessโ€ฆ

(ns day2
  (:require [clojure.string]))

(comment
  ; part 1
  (->> "input"
       slurp
       clojure.string/split-lines
       (map #(clojure.string/split % #" "))
       (map (fn [[command value]] [command (Integer/parseInt value)]))
       (reduce (fn [[pos depth] [command value]]
                 (case command
                   "forward" [(+ pos value) depth]
                   "down" [pos (+ depth value)]
                   "up" [pos (- depth value)]))
               [0 0])
       (apply *))
  ;=> 1698735
  )

(comment
  ; part 2
  (->> "input"
       slurp
       clojure.string/split-lines
       (map #(clojure.string/split % #" "))
       (map (fn [[command value]] [command (Integer/parseInt value)]))
       (reduce (fn [[pos depth aim] [command value]]
                 (case command
                   "forward" [(+ pos value) (+ depth (* aim value)) aim]
                   "down" [pos depth (+ aim value)]
                   "up" [pos depth (- aim value)]))
               [0 0 0])
       (take 2)
       (apply *))
  ;=> 1594785890
  )

R.A. Porter 2021-12-02T05:36:08.093Z

A bit inelegant, but it got the job done.

๐Ÿ‘ 3
Aleks 2021-12-02T05:52:36.093600Z

Here is mine https://github.com/zelark/AoC-2021/blob/main/src/zelark/aoc_2021/day_02.clj

๐Ÿ‘ 3
โค๏ธ 2
mchampine 2021-12-02T06:35:58.094800Z

Some wasted effort using maps, and could be cleaner but it got the job done.

๐Ÿ‘ 3
2021-12-02T06:59:52.095300Z

Hereโ€™s mine:

(def input (->> (u/day-input-source 2)
                (into [] (map (comp edn/read-string #(str "{:" % "}"))))))

(defn t1
  [input]
  (let [combined (reduce (fn [acc x] (merge-with + acc x)) input)
        {:keys [forward up down]} combined]
    (* forward (- down up))))

(defn t2
  [input]
  (let [f (fn [acc action]
            (let [[direction x] (-> action seq first)]
              (case direction
                :down    (update acc :aim + x)
                :up      (update acc :aim - x)
                :forward (-> acc
                             (update :horiz + x)
                             (update :depth + (* (:aim acc) x))))))
        init (zipmap [:horiz :depth :aim] (repeat 0))
        {:keys [horiz depth]} (reduce f init input)]
    (* horiz depth)))

๐Ÿ‘ 3
raicotop 2021-12-02T07:01:07.095500Z

My solution using recursion: https://github.com/raicotop/advent-of-code-2021/blob/main/src/advent_of_code_2021/day-02.clj

๐Ÿ‘ 1