Advent of Code 2021

Advent of Code Day 1 completed. Took me 3 minutes to complete yet I did not get a position in the leaderboard.

I started with Elixir, then went ahead and did F# as well.

The Elixir one:

defmodule AdventOfCode.Y2021.Day01 do
  @moduledoc """
  --- Day 1: Sonar Sweep ---
  Problem Link: https://adventofcode.com/2021/day/1
  """
  use AdventOfCode.Helpers.InputReader, year: 2021, day: 1

  def run_1, do: input!() |> parse() |> depth_increase()
  def run_2, do: input!() |> parse() |> sliding_window() |> depth_increase()

  def parse(data) do
    data
    |> String.split("\n")
    |> Enum.map(&String.to_integer/1)
  end

  defp depth_increase(measurements) do
    measurements
    |> Enum.chunk_every(2, 1, :discard)
    |> Enum.count(fn [a, b] -> b - a > 0 end)
  end

  defp sliding_window(measurements) do
    measurements
    |> Enum.chunk_every(3, 1, :discard)
    |> Enum.map(&Enum.sum/1)
  end
end

The F# One:

/// Advent of Code 2021
/// Day 1: Sonar Sweep
/// Description: https://adventofcode.com/2021/day/1
module Year2021Day01

open AdventOfCode.FSharp.Utils

module Solution =
    let increase =
        Seq.pairwise
        >> Seq.filter (fun (a, b) -> b - a > 0)
        >> Seq.length

    let solvePart1 = ints >> increase >> output

    let solvePart2 =
        let slidingWindow =
            Seq.pairwise
            >> Seq.pairwise
            >> Seq.map (fun ((a, b), (_, d)) -> a + b + d)

        ints >> slidingWindow >> increase >> output

    let solve (input: string seq) = (solvePart1 input, solvePart2 input)
4 Likes