bjorng

bjorng

Erlang Core Team

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

defmodule Day01 do
  def part1(input) do
    all = parse(input)
    {first, second} = Enum.unzip(all)
    Enum.zip([Enum.sort(first), Enum.sort(second)])
    |> Enum.map(fn {a, b} ->
      abs(a - b)
    end)
    |> Enum.sum
  end

  def part2(input) do
    all = parse(input)
    {first, second} = Enum.unzip(all)
    frequencies = Enum.frequencies(second)
    first
    |> Enum.map(fn n ->
      n * Map.get(frequencies, n, 0)
    end)
    |> Enum.sum
  end

  defp parse(input) do
    input
    |> Enum.map(fn line ->
      {first, line} = Integer.parse(line)
      line = String.trim(line)
      {second, ""} = Integer.parse(line)
      {first, second}
    end)
  end
end

Showing Posts 1 to 10

code-shoily

code-shoily

I did it in F# and now that I see your Elixir code, mine is exactly the same code (in a different language lol)

The three spaces was this years twone for me and cost me a minute.

cblavier

cblavier

Happy Advent Of Code everyone! :christmas_tree: :partying_face: :nerd_face:

Part1:

def run(puzzle) do
  puzzle
  |> parse()
  |> then(fn {l1, l2} -> Enum.zip(Enum.sort(l1), Enum.sort(l2)) end)
  |> Enum.reduce(0, fn {n1, n2}, acc -> acc + abs(n1 - n2) end)
end

def parse(puzzle) do
  puzzle
  |> String.split("\n")
  |> Enum.reduce({[], []}, fn line, {acc1, acc2} ->
    [n1, n2] = String.split(line, "   ")
    {[to_integer(n1) | acc1], [to_integer(n2) | acc2]}
  end)
end

Part2:

def run(puzzle) do
  {l1, l2} = Part1.parse(puzzle)
  l2_frequencies = Enum.frequencies(l2)

  Enum.reduce(l1, 0, fn n, acc ->
    acc + n * Map.get(l2_frequencies, n, 0)
  end)
end
woojiahao

woojiahao

Pretty fun despite it’s simplicity, but the prompts are getting looong:

defmodule AOC.Y2024.Day1 do
  @moduledoc false

  use AOC.Solution

  @impl true
  def load_data() do
    Data.load_day(2024, 1)
    |> Enum.map(&String.split(&1, ~r/\s+/, trim: true))
    |> Enum.map(fn [a, b] -> {String.to_integer(a), String.to_integer(b)} end)
    |> Array.transpose()
  end

  @impl true
  def part_one([l, r]) do
    l
    |> Enum.sort()
    |> Enum.zip(Enum.sort(r))
    |> General.map_sum(fn {a, b} -> abs(a - b) end)
  end

  @impl true
  def part_two([l, r]) do
    Enum.frequencies(r)
    |> then(fn freq ->
      General.map_sum(l, fn a -> a * Map.get(freq, a, 0) end)
    end)
  end
end

Github: aoc/lib/aoc/y2024/day_1.ex at main · woojiahao/aoc · GitHub (made a whole bunch of utility modules and functions from past years, to reduce code duplication)

pehbehbeh

pehbehbeh

lud

lud

Starting very simple, but Elixir shines :slight_smile:

It’s basically @bjorng 's solution.

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

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

  defp parse_line(line) do
    [a, b] = String.split(line, " ", trim: true)
    {a, ""} = Integer.parse(a)
    {b, ""} = Integer.parse(b)
    {a, b}
  end

  def part_one(problem) do
    {left, right} = problem
    left = Enum.sort(left)
    right = Enum.sort(right)
    Enum.zip_with(left, right, fn a, b -> abs(a - b) end) |> Enum.sum()
  end

  def part_two(problem) do
    {left, right} = problem
    freqs = Enum.frequencies(right)
    left |> Enum.map(fn a -> a * Map.get(freqs, a, 0) end) |> Enum.sum()
  end
end
sevenseacat

sevenseacat

Author of Ash Framework
egze

egze

Part 1:

[_ | rest] = all =
  puzzle_input
  |> String.split(["\n", "   "], trim: true)
  |> Enum.map(& String.to_integer/1)

left_sorted = Enum.take_every(all, 2) |> Enum.sort()
right_sorted = Enum.take_every(rest, 2) |> Enum.sort()

Enum.zip_reduce([left_sorted, right_sorted], 0, fn [a, b], acc ->
  acc + abs(a - b)
end)

Part 2:

[_ | rest] = all =
  puzzle_input
  |> String.split(["\n", "   "], trim: true)
  |> Enum.map(& String.to_integer/1)

left = Enum.take_every(all, 2)
right_frequencies = Enum.take_every(rest, 2) |> Enum.frequencies()

left
|> Enum.reduce(0, fn a, acc ->
  acc + (a * Map.get(right_frequencies, a, 0))
end)
dimitarvp

dimitarvp

Are we not supposed to also include code for opening and reading the input from a file? Or we can opt to just put it in a module attribute and parse that?

egze

egze

Well, I used KinoAOC — KinoAOC v0.1.7 for Livebook, and it binds the input to puzzle_input automatically :slight_smile:

woojiahao

woojiahao

I made my own utility modules and functions because I’ve been using Elixir for the past 3-4 AoCs so I’ve accumulated use cases. The goal of posts should be the actual solution, it’s less important where the input comes from!

Where Next? Top

Trending in Challenges Top

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews