Skip to content

Instantly share code, notes, and snippets.

@jellea
Last active December 1, 2019 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jellea/4d62380f587fbc2c3de4ce23fc0f2909 to your computer and use it in GitHub Desktop.
Save jellea/4d62380f587fbc2c3de4ce23fc0f2909 to your computer and use it in GitHub Desktop.
Advent Of Code [Puzzle 1](https://adventofcode.com/2019/day/1) in [Fennel](https://fennel-lang.org/)
(fn mass->fuel [mass]
(-> (tonumber mass) (/ 3) (math.floor) (- 2)))
(let [file (io.open "1.data" "r")
file-contents (file:read "*a")
mods (string.gmatch file-contents "%d+")]
(var total-fuel 0)
(each [mass mods]
(let [fuel (mass->fuel mass)]
(set total-fuel (+ total-fuel fuel))
(var rest-fuel fuel)
(while (> rest-fuel 0)
(do
(set rest-fuel (mass->fuel rest-fuel))
(if (> rest-fuel 0)
(set total-fuel (+ total-fuel rest-fuel)))))))
(print total-fuel))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment