Advent of Code 2021 - Day 1

First time doing the AoC will do it when I have time. But solved it with Excel and Erlang.

5 Likes

I am currently learning Erlang. I am not good at all with recursion, so I am hoping to use AOC to get some practice using recursion.

Here is my first try with the code that I usually would write in every other language (but with immutability):

partOne(Input, Counter) ->
	[_|Tail] = Input,
	if length(Tail) > 0 ->
		Current = lists:nth(1, Input),
		Next =  lists:nth(2, Input),
		if Current < Next -> partOne(Tail, Counter + 1);
			true -> partOne(Tail, Counter)
		end;
		true -> Counter
	end.

And here is my second try, in what I hope is a more kosher Erlang manner:

partOneRecursive([X, Y | Z]) when Y > X -> partOneRecursive([Y | Z]) + 1;
partOneRecursive([_, Y | Z]) -> partOneRecursive([Y | Z]);
partOneRecursive([_]) -> 0.```
7 Likes

Here’s a rather verbose solution in Sesterl, but got the job done, after I got it to type-check :slight_smile:

module Main = struct
  type file_descriptor =
    | FileDescriptorDummy

  val file_open : fun(binary) -> option<file_descriptor> = external 1 ```
    file_open(Path) ->
      case file:open(Path, [binary]) of
        {ok, Fd} -> {ok, Fd};
        _ -> error
      end.
  ```

  type file_read_error =
    | Eof
    | OtherFileReadError

  val file_read_line : fun(file_descriptor) -> result<binary, file_read_error> = external 1 ```
    file_read_line(FileDescriptor) ->
      case file:read_line(FileDescriptor) of
        {ok, Data} -> {ok, Data};
        eof -> {error, eof};
        _ -> {error, other_file_read_error}
      end.
  ```

  val binary_trim : fun(binary) -> binary = external 1 ```binary_trim(Binary) -> string:trim(Binary).```

  val binary_to_integer : fun(binary) -> int = external 1 ```binary_to_integer(Binary) -> erlang:binary_to_integer(Binary).```

  val rec count_measurement_increases(file, maybe_prev1, maybe_prev2, maybe_prev3, p1_count, p2_count) =
    case file_read_line(file) of
    | Error(_) -> {p1_count, p2_count}
    | Ok(line) ->
        let measurement = binary_to_integer(binary_trim(line)) in
        let new_p1_count =
          case maybe_prev3 of
          | None -> p1_count
          | Some(previous) ->
              if previous < measurement then
                p1_count + 1
              else
                p1_count
          end in
        let new_p2_count =
          case {maybe_prev1, maybe_prev2, maybe_prev3} of
          | {None, _, _} -> p2_count
          | {Some(previous1), Some(previous2), Some(previous3)} ->
              if (previous1 + previous2 + previous3) < (previous2 + previous3 + measurement) then
                p2_count + 1
              else
                p2_count
          end in
        count_measurement_increases(file, maybe_prev2, maybe_prev3, Some(measurement), new_p1_count, new_p2_count)
    end

  val main() =
    let Some(file) = file_open("../../1") in
    let {p1, p2} = count_measurement_increases(file, None, None, None, 0, 0) in
    let _ = print_debug(p1) in
    print_debug(p2)
end
3 Likes