Day 1 complete

This commit is contained in:
Daniel Flanagan 2019-12-01 14:49:58 -06:00
commit c103fe2f82
3 changed files with 137 additions and 0 deletions

21
2019/1.moon Normal file
View File

@ -0,0 +1,21 @@
-- https://adventofcode.com/2019/day/1
require "common"
input = input_for_day 1
-- part 1
nums = -> input\gmatch "%d+"
mass_to_fuel = => -2 + math.floor @ / 3
print "Part 1:", reduce nums!, 0, (n) => @ + mass_to_fuel n
-- part 2
fuel_for_fuel = =>
f = mass_to_fuel @
print "fff:", f
if f < 1 then return 0 else return f + fuel_for_fuel f
print "Part 2:", reduce nums!, 0, (n) => @ + fuel_for_fuel(n)

16
2019/common.moon Normal file
View File

@ -0,0 +1,16 @@
export input_for_day, reduce
read_whole_file = =>
f = assert io.open @, "rb"
content = f\read "*all"
f\close()
content
input_for_day = =>
read_whole_file "input/#{@}.txt"
reduce = (iterable, initial, f) ->
acc = initial
for e in iterable
acc = f acc, e
acc

100
2019/input/1.txt Normal file
View File

@ -0,0 +1,100 @@
130762
108691
131618
138163
59967
130453
117515
115776
134083
86966
128075
55569
112843
97878
92330
70917
143903
81171
148506
141379
131161
88719
69654
82141
55265
75623
97408
105269
147378
126054
133962
60304
130503
138350
93164
69661
69271
100054
138295
142865
64142
123466
80101
149696
102510
129988
87742
106785
133039
59192
86544
124950
64242
80128
109287
129634
140335
118220
106819
97296
111003
103222
54192
103548
63861
140571
50476
100570
114065
110279
64720
91941
62312
80834
132969
51973
115887
68662
138266
107234
75795
81409
78610
112587
92384
111804
138861
79393
81285
131307
68815
54976
127529
103359
138537
79663
128097
56085
96504
119501