bjorng

bjorng

Erlang Core Team

Advent of Code 2024 - Day 2

Here is my solution for day 2 of Advent of Code:

Most Liked

sevenseacat

sevenseacat

Author of Ash Framework
dimitarvp

dimitarvp

Life sadly kept getting in the way but ultimately:

defmodule Day02 do
  @moduledoc ~S"""
  A solution to https://adventofcode.com/2024/day/2.
  """

  @type level :: pos_integer()
  @type distance :: integer()
  @type report :: [level()]

  @spec all_variants_with_one_element_removed(report()) :: [report()]
  def all_variants_with_one_element_removed(list) do
    for i <- 0..(length(list) - 1), do: list |> List.delete_at(i)
  end

  @spec sign(integer()) :: :zero | :minus | :plus
  def sign(0), do: :zero
  def sign(i) when i > 0, do: :plus
  def sign(i) when i < 0, do: :minus

  @spec distances(report()) :: [distance()]
  def distances([first | rest]) do
    rest
    |> Enum.reduce({first, []}, fn current_level, {previous_level, distances} ->
      {current_level, [current_level - previous_level | distances]}
    end)
    |> then(fn {_last_level, distances} -> Enum.reverse(distances) end)
  end

  @spec same_signs?([distance()]) :: boolean()
  def same_signs?(list) do
    list
    |> Enum.map(&sign/1)
    |> Enum.uniq()
    |> length()
    |> Kernel.==(1)
  end

  @spec safe?(report()) :: boolean()
  def safe?(report) do
    distances = distances(report)
    monotonical? = same_signs?(distances)

    safely_advancing? =
      distances |> Enum.map(&abs/1) |> Enum.all?(fn distance -> distance <= 3 end)

    monotonical? and safely_advancing?
  end

  @spec safe_with_a_dampener?(report()) :: boolean()
  def safe_with_a_dampener?(report) do
    safe?(report) or
      report |> all_variants_with_one_element_removed() |> Enum.any?(&safe?/1)
  end

  @doc ~S"""
  iex> Day02.part_1("7 6 4 2 1\n1 2 7 8 9\n9 7 6 2 1\n1 3 2 4 5\n8 6 4 4 1\n1 3 6 7 9\n")
  nil
  """
  @spec part_1(String.t()) :: non_neg_integer()
  def part_1(input \\ Aoc.input(2)) do
    input
    |> Aoc.parse_lines_of_integers()
    |> Enum.count(&safe?/1)
  end

  @spec part_2(String.t()) :: non_neg_integer()
  def part_2(input \\ Aoc.input(2)) do
    input
    |> Aoc.parse_lines_of_integers()
    |> Enum.count(&safe_with_a_dampener?/1)
  end
end

Did my best to make it readable and intuitive, even benchmarked three competing implementation I had ideas about, and only posted the one that won.

lud

lud

No optimization here, just building all possible list before trying them one by one :smiley:

defmodule AdventOfCode.Solutions.Y24.Day02 do
  alias AoC.Input

  def parse(input, _part) do
    Enum.map(Input.stream!(input, trim: true), &parse_line/1)
  end

  defp parse_line(line) do
    Enum.map(String.split(line, " "), &String.to_integer/1)
  end

  def part_one(problem) do
    problem
    |> Enum.filter(&safe?/1)
    |> length()
  end

  defp safe?([a, b | _] = list) when a < b, do: safe?(:asc, list)
  defp safe?([a, b | _] = list) when a > b, do: safe?(:desc, list)
  defp safe?([a, a | _]), do: false

  defp safe?(:asc, [a, b | rest]) when abs(a - b) in 1..3 and a < b, do: safe?(:asc, [b | rest])
  defp safe?(:desc, [a, b | rest]) when abs(a - b) in 1..3 and a > b, do: safe?(:desc, [b | rest])
  defp safe?(_, [_last]), do: true
  defp safe?(_, _), do: false

  def part_two(problem) do
    problem
    |> Enum.filter(&safeish?/1)
    |> length()
  end

  defp safeish?(list) do
    candidates = [list | Enum.map(0..(length(list) - 1), &List.delete_at(list, &1))]
    Enum.any?(candidates, &safe?/1)
  end
end

Edit:

candidates = Stream.concat([list], Stream.map(0..(length(list) - 1), &List.delete_at(list, &1)))

This would save memory but given the input size it’s actually slower.

Aetherus

Aetherus

I was trying hard to find a smart-ass solution for part 2 without using List.delete_at/2, but in the end, I had to admit that I’m not that smart after all.

Part 1

puzzle_input
|> String.split("\n")
|> Enum.map(&String.split/1)
|> Enum.map(&Enum.map(&1, fn s -> String.to_integer(s) end))
|> Enum.count(fn
  [a, a | _] ->
    false
  
  [a, b | _] = line ->
    sign = div(a - b, abs(a - b))
  
    line
    |> Enum.chunk_every(2, 1, :discard)
    |> Enum.all?(fn [a, b] ->
      sign * (a - b) in 1..3
    end)
end)

Part 2

puzzle_input
|> String.split("\n")
|> Enum.map(&String.split/1)
|> Enum.map(&Enum.map(&1, fn s -> String.to_integer(s) end))
|> Enum.count(fn line ->
  0..length(line)
  |> Stream.map(&List.delete_at(line, &1))
  |> Enum.any?(fn
    [a, a | _] ->
      false
  
    [a, b | _] = line ->
      sign = div(a - b, abs(a - b))
  
      line
      |> Enum.chunk_every(2, 1, :discard)
      |> Enum.all?(fn [a, b] ->
        sign * (a - b) in 1..3
      end)
  end)
end)
lkuty

lkuty

#!/usr/bin/env elixir
# 2024. day 2.

defmodule A do
  @spec is_ok?([integer()]) :: boolean()
  def is_ok?(lst), do: is_ok?(lst, nil, nil)

  @spec is_ok?([integer()], nil | integer(), nil | :inc | :dec) :: boolean()
  defp is_ok?(lst, prev, dir)

  defp is_ok?([], _, _), do: true # empty list
  defp is_ok?([_fst], nil, _), do: true # single element list
  defp is_ok?([fst | rest], nil, nil), do: is_ok?(rest, fst, nil) # more than one element

  # same number!
  defp is_ok?([fst | _rest], fst, _dir), do: false

  # direction is unknown
  defp is_ok?([fst | rest], prev, nil) when fst < prev, do: (if prev-fst<=3, do: is_ok?(rest, fst, :dec), else: false)
  defp is_ok?([fst | rest], prev, nil) when fst > prev, do: (if fst-prev<=3, do: is_ok?(rest, fst, :inc), else: false)

  # direction is decreasing
  defp is_ok?([fst | rest], prev, :dec) when fst < prev, # we keep on decreasing
    do: (if prev-fst<=3, do: is_ok?(rest, fst, :dec), else: false)
  defp is_ok?([fst | _rest], prev, :dec) when fst > prev, do: false # we start to increase

  # direction is increasing
  defp is_ok?([fst | rest], prev, :inc) when fst > prev,
    do: (if fst-prev<=3, do: is_ok?(rest, fst, :inc), else: false) # we keep on increasing
  defp is_ok?([fst | _rest], prev, :inc) when fst < prev, do: false # we start to decrease

  # get all the sublists of lst with one element less than lst
  # A.sublists([1,2,3,4]) => [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
  def sublists(lst), do: sublists([], lst, [])
  def sublists(_pre, [], acc), do: acc
  def sublists(pre, [a | rest], acc), do: sublists([a | pre], rest, [(Enum.reverse(pre) ++ rest) | acc])
end

File.stream!("day02.txt")
|> Stream.map(fn line -> String.split(line) |> Enum.map(&String.to_integer/1) |> A.is_ok?() end)
|> Enum.count(&Function.identity/1)
|> IO.inspect(label: "part 1")

File.stream!("day02.txt")
|> Stream.map(fn line ->
  lst = String.split(line) |> Enum.map(&String.to_integer/1)
  Enum.any?([lst | A.sublists(lst)], fn lst -> A.is_ok?(lst) end)
end)
|> Enum.count(&Function.identity/1)
|> IO.inspect(label: "part 2")

Where Next?

Popular in Challenges Top

antoine-duchenet
Everything went smoothly today. Nothing to change to solve part 2 because I already used memoization for part 1 (it looked like an AoC e...
New
QuinnWilton
Note: This topic is to talk about Day 7 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can joi...
New
christhekeele
Continuation of Advent of Code 2022​:christmas_tree:, Day 1: Day 2! Leaderboard:
New
bjorng
Note: This topic is to talk about Day 25 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
NobbZ
Note by the Moderators: This topic is to talk about the Day 2 of the Advent of Code. For general discussion about the Advent of Code 201...
New
Aetherus
The second part of today’s puzzle is very misleading. FYI, each of the ghosts has only one possible position that ends with a "Z" on its...
New
stevensonmt
Anyone else think the prompt for this challenge is contradictory? The rules for comparing packets include If both values are lists, c...
New
Aetherus
Hello, guys. I’m back again, but only for the weekends, maybe. This topic is about Day 13 of the Advent of Code 2020 . Thanks to @egze,...
New
bjorng
This topic is about Day 18 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
mattbaker
I’m having so much fun working on the “Protohackers” challenges, I never got into Advent of Code much but this has been amazing. The chal...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement