Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
advent-2019/src/advent_2019/day01.clj
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
37 lines (32 sloc)
1.28 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns advent-2019.day01 | |
(:gen-class) | |
(:require [advent-2019.core :refer [get-input]])) | |
(defn calc-fuel | |
"Fuel required to launch a given module is based on its mass. | |
Specifically, to find the fuel required for a module, | |
take its mass, divide by three, round down, and subtract 2." | |
[mass] | |
(-> mass (/ 3) (Math/floor) (int) (- 2))) | |
(defn calc-total-fuel | |
"For each module mass, calculate its fuel and add it to the total. | |
Then, treat the fuel amount you just calculated as the input mass and repeat the process, | |
continuing until a fuel requirement is zero or negative." | |
[mass] | |
(loop [masses [] m mass] | |
(let [fuel (calc-fuel m)] | |
(if (< fuel 1) | |
(reduce + masses) | |
(recur (conj masses fuel) fuel))))) | |
(defn part1 | |
"What is the sum of the fuel requirements for all of the modules on your spacecraft?" | |
[input] | |
(->> input (map calc-fuel) (reduce +))) | |
(defn part2 | |
"What is the sum of the fuel requirements for all of the modules on your spacecraft | |
when also taking into account the mass of the added fuel?" | |
[input] | |
(->> input (map calc-total-fuel) (reduce +))) | |
(defn -main [] | |
(let [input (get-input "day01.txt" true)] ; Passing true converts input strings to ints | |
(println "Day 01, Part 1:" (part1 input)) | |
(println "Day 01, Part 2:" (part2 input)))) |