Skip to content

Instantly share code, notes, and snippets.

@johannes-jansson
Created December 1, 2019 22:15
Show Gist options
  • Save johannes-jansson/2980d6236f91f0b5c0c13a5dd6f1b52b to your computer and use it in GitHub Desktop.
Save johannes-jansson/2980d6236f91f0b5c0c13a5dd6f1b52b to your computer and use it in GitHub Desktop.
(ns main
(:require [clojure.string :as str]))
(defn load_data
"Loads data from path, split by newline"
[path]
(str/split (slurp path) #"\n"))
(defn fuel
"Calculates the fuel consumption for weight"
[weight]
(- (int (Math/floor (/ weight 3))) 2))
(defn fuel_total
"Calculates the fuel consumption for weight, including weight of extra fuel"
[weight]
(def new_weight (fuel weight))
(if (< new_weight 8)
new_weight
(+ new_weight (fuel_total new_weight))))
(defn step_a [tot, weight]
(+ tot (fuel (Integer/parseInt weight))))
(defn step_b [tot, weight]
(+ tot (fuel_total (Integer/parseInt weight))))
(defn one_a [path]
(reduce step_a 0 (load_data path)))
(defn one_b [path]
(reduce step_b 0 (load_data path)))
(defn -main [& args]
(println (one_a "../01/input")) ; answer should be 3212842
(println (one_b "../01/input")) ; answer should be 4816402
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment