Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis

cellularmitosis/README.md

Last active Dec 5, 2019
Embed
What would you like to do?
Advent of Code, Day 1

Blog 2019/12/1

<- previous | index | next ->

Advent of Code, Day 1

I thought I'd use Advent of Code as a way to get some more Clojure experience!

https://adventofcode.com/2019/day/1

Clojure solution

Part 1

Part 1 is straightforward. There is a file with a list of integers (one per line). We need to parse them in as a list of ints, map a formula across them, then sum to a single value.

Part 2

In part 2, we have to recursively calculate the fuel needed by the fuel, and the fuel needed by that fuel, and so on.

This recursion terminates because the floor / integer truncation eventually returns zero (or the forumla ends up returning a negative value).

This sounds similar to the square root / fixed point stuff from SICP: https://www.lvguowei.me/post/sicp-goodness-sqrt/

We rename mass-to-fuel to mass-to-fuel-naive and then implement a new, recursive mass-to-fuel.

; https://adventofcode.com/2019/day/1
(defn file-as-lines
"return a file as a list of lines."
[fpath]
(with-open [rdr (clojure.java.io/reader fpath)]
(doall (line-seq rdr))))
; thanks to https://stackoverflow.com/a/5622110
(defn line-to-int
"given a line from a file which is a list of ints, return the int."
[line]
(Integer. (re-find #"[0-9]+" line)))
(defn lines-to-ints
"given a list of lines from a file which is a list of ints, return a list of ints."
[lines]
(map line-to-int lines))
(defn mass-to-fuel
"calculate the fuel needed for the given mass."
[mass]
(let [a (/ mass 3)
b (Math/floor a)
c (int b)
d (- c 2)]
d))
(defn masses-to-fuels
"calculate the fuels needed for the list of masses."
[masses]
(map mass-to-fuel masses))
(defn sum
"sum the given list of ints"
[xs]
(apply + xs))
(defn total-fuel-for-mass-file
"given a file with a list of masses, sum up the needed fuel."
[fpath]
(let [lines (file-as-lines fpath)
masses (lines-to-ints lines)
fuels (masses-to-fuels masses)]
(sum fuels)))
; print the answer to day 1, part 1.
(println (total-fuel-for-mass-file "./input.txt"))
; https://adventofcode.com/2019/day/1
(defn file-as-lines
"return a file as a list of lines."
[fpath]
(with-open [rdr (clojure.java.io/reader fpath)]
(doall (line-seq rdr))))
; thanks to https://stackoverflow.com/a/5622110
(defn line-to-int
"given a line from a file which is a list of ints, return the int."
[line]
(Integer. (re-find #"[0-9]+" line)))
(defn lines-to-ints
"given a list of lines from a file which is a list of ints, return a list of ints."
[lines]
(map line-to-int lines))
(defn mass-to-fuel-naive
"calculate the (naive) fuel needed for the given mass.
does not account for the fuel needed by the fuel."
[mass]
(let [a (/ mass 3)
b (Math/floor a)
c (int b)
d (- c 2)]
(if (> d 0) d 0)))
(defn mass-to-fuel
"calculate the fuel needed for the given mass.
also accounts for the fuel needed by the fuel itself."
[mass]
(let [a (mass-to-fuel-naive mass)]
(if (> a 0)
(+ a (mass-to-fuel a))
0)))
(defn masses-to-fuels
"calculate the fuels needed for the list of masses."
[masses]
(map mass-to-fuel masses))
(defn sum
"sum the given list of ints"
[xs]
(apply + xs))
(defn total-fuel-for-mass-file
"given a file with a list of masses, sum up the needed fuel."
[fpath]
(let [lines (file-as-lines fpath)
masses (lines-to-ints lines)
fuels (masses-to-fuels masses)]
(sum fuels)))
; print the answer to day 1, part 2.
(println (total-fuel-for-mass-file "./input.txt"))
94164
100562
114499
134308
138764
114494
70457
113793
117753
77795
110371
113357
118839
99757
119918
145232
147113
142411
93053
81783
124022
98470
77368
75163
79175
131174
93196
121875
86016
148758
126577
109812
105696
66318
146939
113236
130014
135719
127114
69700
109416
64168
89215
69015
128511
59682
79067
58795
145447
129419
93058
63860
146148
58364
74149
81871
116469
131739
132852
148040
98865
56588
79326
114713
52397
134887
114809
113356
60505
142323
84792
117079
147796
50196
148897
100794
50508
71023
149350
66679
93680
116069
133042
117291
127439
81598
93163
83964
64226
63026
82625
59589
94831
66807
120375
112108
83484
109892
66136
126794
$ clojure day1part1.clj
3414791
$ clojure day1part2.clj
5119312
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.