Advent of code day 1

It’s that time of year again, and I’m a double gluten for punishment. Not only am I doing advent of code which is bad enough (toward the end), I’m also doing it in Clojure, because ¯\_(ツ)_/¯

(ns day-1.core
  (:require [clojure.string :as str]))

; calculate the fuel
(defn calc-fuel [f]
  (- (Math/floor (/ f 3)) 2)

; recursive figure out fuel
(defn calc-fuel2 [f]
  (loop [t 0
         fuel f]
        (def tmp (calc-fuel fuel))
        (if (> tmp 0) (recur (+ tmp t) tmp) t)

(defn -main []

  ;; load in the data to a list of numbers
  (def data (map #(Integer/parseInt %1) (str/split (slurp "./data/data.txt") #"\n")))

  ;; solution for part one.
  (def solution (map #(calc-fuel %1) data))
  (def part1 (reduce + solution))
  (println part1)

  ;; solution for part two
  (def solution (map #(calc-fuel2 %1) data))
  (def part2 (reduce + solution))
  (println part2)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s