parent
72c182f0a1
commit
6fcdf67ba6
@ -0,0 +1,35 @@
|
||||
-module(aoc01).
|
||||
|
||||
-export([calculate_module/1, fuel/2, run/0, run2/0]).
|
||||
|
||||
all_lines(Path) ->
|
||||
{ok, Fd} = file:open(Path, read),
|
||||
Lines = all_lines(Fd, []),
|
||||
file:close(Fd),
|
||||
lists:reverse(Lines).
|
||||
|
||||
all_lines(Fd, Lines) ->
|
||||
case file:read_line(Fd) of
|
||||
{ok, Line} ->
|
||||
all_lines(Fd, [string:chomp(Line) | Lines]);
|
||||
eof -> Lines
|
||||
end.
|
||||
|
||||
calculate_module(Mass) -> math:floor(Mass / 3) - 2.
|
||||
|
||||
fuel(Mass, Total) when Mass =< 6 -> Total;
|
||||
fuel(Mass, Total) ->
|
||||
Fuel = math:floor(Mass / 3) - 2,
|
||||
fuel(Fuel, Total + Fuel).
|
||||
|
||||
run() ->
|
||||
Lines = all_lines("aoc01.txt"),
|
||||
FuelQuantities = [calculate_module(list_to_integer(X))
|
||||
|| X <- Lines],
|
||||
lists:sum(FuelQuantities).
|
||||
|
||||
run2() ->
|
||||
Lines = all_lines("aoc01.txt"),
|
||||
FuelQuantities = [fuel(list_to_integer(X), 0)
|
||||
|| X <- Lines],
|
||||
lists:sum(FuelQuantities).
|
@ -0,0 +1,100 @@
|
||||
103842
|
||||
72629
|
||||
121232
|
||||
120959
|
||||
94285
|
||||
85852
|
||||
78876
|
||||
93545
|
||||
136775
|
||||
111893
|
||||
112863
|
||||
61947
|
||||
52671
|
||||
122769
|
||||
90995
|
||||
106037
|
||||
106618
|
||||
144212
|
||||
125766
|
||||
56163
|
||||
125865
|
||||
87828
|
||||
117596
|
||||
118778
|
||||
131537
|
||||
131498
|
||||
81583
|
||||
111443
|
||||
139184
|
||||
101980
|
||||
114117
|
||||
76003
|
||||
99157
|
||||
93721
|
||||
106494
|
||||
66654
|
||||
73954
|
||||
85815
|
||||
139358
|
||||
78163
|
||||
144753
|
||||
58928
|
||||
137799
|
||||
75580
|
||||
115861
|
||||
131718
|
||||
145985
|
||||
61232
|
||||
139664
|
||||
123931
|
||||
101512
|
||||
107532
|
||||
119323
|
||||
54937
|
||||
82412
|
||||
149218
|
||||
98531
|
||||
122318
|
||||
138890
|
||||
59125
|
||||
111176
|
||||
97205
|
||||
52214
|
||||
84531
|
||||
115983
|
||||
69976
|
||||
125186
|
||||
142852
|
||||
66808
|
||||
81689
|
||||
62885
|
||||
126094
|
||||
86092
|
||||
59981
|
||||
54868
|
||||
142381
|
||||
92384
|
||||
121232
|
||||
96994
|
||||
93489
|
||||
141201
|
||||
108497
|
||||
64092
|
||||
101991
|
||||
137907
|
||||
63230
|
||||
55724
|
||||
126888
|
||||
70665
|
||||
111235
|
||||
123493
|
||||
148071
|
||||
147590
|
||||
113936
|
||||
57270
|
||||
127204
|
||||
144599
|
||||
56041
|
||||
62105
|
||||
124342
|
Loading…
Reference in new issue