Do Advent of Code 2017 in Erlang

Tag: PROJECT
Edit
Created: 2019/10/24
Updated: 2019/10/24

Work on writing AoC exercises, with his code as a stopgap for understanding if I get well and truly stuck.

Day 1

-module(day1).
-export([main/0]).

main() ->
    {ok, Input} = file:read_file("input.txt"),
    % The lists to integers that I got from ferd.ca
    part2([I - $0 || I <- binary_to_list(Input)]).

part1(Input) ->
    L = lists:last(Input),
    pair_sum([L|Input], 0).

pair_sum([H,H], Acc) -> Acc + H;
pair_sum([H,H|Rest], Acc) -> pair_sum([H|Rest], Acc + H);
pair_sum([_|Rest], Acc) -> pair_sum(Rest, Acc);
pair_sum([], Acc) -> Acc.

%% So this is a different approach that requires
%% more memory than Fred Hilbert's solution, but is more compact codewise. 
part2(Input) -> 
    HalfLength = length(Input) div 2,
    HalfAdvanced = lists:sublist(Input, HalfLength + 1, HalfLength) ++ lists:sublist(Input, 1, HalfLength),
    Pairs = lists:zip(Input, HalfAdvanced),
    lists:foldl(fun({X,Y}, Acc) -> if X  =:= Y -> Acc + 1; true -> Acc end, Pairs).

Day 2

TODO