Day 1: The Tyranny of the Rocket Equation

Stavros Aronis 02.12.2019

I did the Advent of Code 2019 day 1 challenge in Erlang! 

 

#!/usr/bin/env escript
-mode(native).

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

main(Args) ->
  Nums = lists:append(read_list("~d")),
  Sol =
    case Args of
      ["2"] -> fuel_proper(Nums);
      _ -> fuel_mod(Nums)
    end,
  io:format("~w~n", [Sol]).

read_list(Pat) ->
  read_list(Pat, []).

read_list(Pat, Acc) ->
  case io:fread("", Pat) of
    {ok, Res} -> read_list(Pat, [Res|Acc]);
    eof -> lists:reverse(Acc)
  end.

fuel_mod(Nums) ->
  lists:sum([(Mod div 3) - 2 || Mod <- Nums]).

fuel_proper(Nums) ->
  lists:sum([mod_proper(Mod) || Mod <- Nums]).

mod_proper(Mod) ->
  mod_proper(Mod, 0).

mod_proper(Lift, Fuel) ->
  NF = (Lift div 3) - 2,
  case NF < 0 of
    true -> Fuel;
    false -> mod_proper(NF, Fuel + NF)
  end.

 

 


 
Stavros Aronis
Author

Stavros Aronis

Developer & Trainer at Erlang Solutions

ARTICLES: 6

Day 18: Settlers of The North Pole - Advent of Code 2018

Article by Stavros Aronis

I did the Advent of Code 2018 day 18 challenge in Erlang! Parts one and two are as follows:

READ MORE

Day 14: Chocolate Charts - Advent of Code 2018

Article by Stavros Aronis

I did the Advent of Code 2018 day 14 challenge in Erlang! Parts one and two are as follows:

READ MORE

Day 13: Mine Cart Madness - Advent of Code 2018

Article by Stavros Aronis

I did the Advent of Code 2018 day 13 challenge in Erlang! Parts one and two are as follows:

READ MORE

Day 12: Subterranean Sustainability - Advent of Code 2018

Article by Stavros Aronis

I did the Advent of Code 2018 day 12 challenge in Erlang! Parts one and two are as follows:

READ MORE

Day 9: Marble Mania - Advent of Code 2018

Article by Stavros Aronis

I did the Advent of Code 2018 day 9 challenge in Erlang! Parts one and two are as follows:

READ MORE

Day 8: Memory maneuver - Advent of Code

Article by Stavros Aronis

I did the Advent of Code 2018 day 8 challenge in Erlang! Parts one and two are as follows:

READ MORE