Advent of Code 2022 - Day 01
Day 01
This problem boils down to being able to parse the input and being able to sum the total calories for each elf.
For the parsing I used regex to first get all the calories of one elf with regex \n *\n * which maches
anything between two newline characters. Then to convert paragraphs to lines I matched a newline character
with any number of whitespaces.
1(defn paragraph-lines
2 "Splits s intro paragraphs and paragraphs to lines"
3 [s]
4 (map (#(string/split % #"\n *")) (string/split s #"\n *\n *")))
Next I needed to sum calories of one elf which is just parsing and summing all the values from one row. And to get all the rows I need to just map this function over the whole data structure.
1(defn sum-row
2 "Parses and sums a row"
3 [row]
4 (->> (map parse-long row)
5 (apply +)))
6
7(defn total-elf
8 "Gets the total for each elf"
9 [s]
10 (map sum-row (paragraph-lines s)))
Part 1
For the first part I needed to sort by descending and take the first result.
1(defn part1
2 "Solution for the first part"
3 [s]
4 (->> (total-elf s)
5 (sort-by -)
6 (first)))
Part 2
For the second just take the top three results and sum them up
1(defn part2
2 "Solution for the second part"
3 [s]
4 (->> (total-elf s)
5 (sort-by -)
6 (take 3)
7 (apply +)))