Welcome! Please see the About page for a little more info on how this works.

0 votes
in Syntax and reader by

Hello! I'm sorry if this is the wrong forum to start a thread like this, but I am new to Clojure/lisps in general and thought I would take this year's Advent of Code (2022) as an opportunity to challenge myself to learn a bit.

So I've started Day 1 and solved the problem (in essence, Given a long list of double space separated numbers, find the largest sum amongst them) but with what looks like the most inelegant thrashing of how one might approach the problem in Clojure.

For example, the answer for the following would be 300

10
20

100
200

My solution is as follows, and I am wondering primarily:

  1. Is this a reasonable approach to the problem, as a beginner, what am I missing out on that would be more elegant.
  2. How does one even format a line like this?

(Spoiler below, in case anyone else is interested in the challenge)

(require '[clojure.string :as str])

(reduce max (map (fn [x] (reduce + (map #(Integer/parseInt %) (str/split x #"\n")))) (str/split (slurp "./input-1.txt") #"\n\n")))

I'm hoping, time permitting, to be able to continue this journey through the month so hopefully I'm able to as the challenges do become quite complicated even for languages I'm familiar with, but I can already say the REPL is really a wonderful thing.

3 Answers

0 votes
by

You might find the Clojure API cheatsheet helpful https://clojure.org/api/cheatsheet

Looks like split-lines and parse-long might be helpful functions here to know about.

0 votes
by

Hi, sorry this is not exactly an answer to your question but I'm also doing Advent of Code in clojure.

I'm an intermediate hobbyist clojure developer at best, so I can't promise that any of it is idiomatic or optimal but my solutions are here if you'd like to compare:

https://github.com/dfornika/advent-of-code/tree/master/2022/clojure

0 votes
by

On the subject of formatting and idioms, you may find the unofficial community https://guide.clojure.style/ helpful.

...