Harrygr

Harrygr

For part 2 I just define a lookup map which retains the first and last letter of the word version of the number which means any overlaps will be captured:

digits = %{
  "one" => "o1e",
  "two" => "t2o",
  "three" => "t3e",
  "four" => "f4r",
  "five" => "f5e",
  "six" => "s6x",
  "seven" => "s7n",
  "eight" => "e8t",
  "nine" => "n9e"
}

input
|> Kino.Input.read()
|> String.split("\n", trim: true)
|> Stream.map(fn line ->
  Enum.reduce(digits, line, fn {word, value}, acc -> String.replace(acc, word, value) end)
end)
|> Stream.map(fn line ->
  line
  |> String.split("", trim: true)
  |> Stream.filter(&Helper.is_numeric_string?/1)
  |> Enum.into([])
end)
|> Stream.map(&"#{List.first(&1)}#{List.last(&1)}")
|> Stream.map(&Integer.parse/1)
|> Stream.map(fn
  {i, _} -> i
  :error -> 0
end)
|> Enum.sum()
0
Post #41
Aetherus

Aetherus

Just tried solving Day 1 puzzles using the low-code platform developed by my company :rofl:

image
image1200×646 43.2 KB

There’s no function to split a string into lines, so I have to heavily rely on regular expressions.

image
image1200×556 41.9 KB

image
image1200×643 49.7 KB

By the way, the whole platform is powered by Elixir and Phoenix :grin:

6
Post #42
trnasistor

trnasistor

This gives me an idea to someday try to solve a puzzle by programming a FPGA :grin:

Very cool!

1
Post #43
Aetherus

Aetherus

And a bit of Erlang, and a bit of React.js (I’m not the frontend guy)

2
Post #44
GavinLindridge

GavinLindridge

First Elixir I’ve written and its definitely a new world but exciting to be learning something new

I’m not going to pretend like there wasn’t some chat gpt to help, but gotta start somewhere right

Any glaring improvements?

defmodule AdventOfCode.Day1 do
  @number_map %{
    "1" => 1,
    "2" => 2,
    "3" => 3,
    "4" => 4,
    "5" => 5,
    "6" => 6,
    "7" => 7,
    "8" => 8,
    "9" => 9,
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "four" => 4,
    "five" => 5,
    "six" => 6,
    "seven" => 7,
    "eight" => 8,
    "nine" => 9
  }

  def find_sum_calibration_values_from_file(file) do
    File.read!(file) |> find_sum_calibration_values()
  end

  def find_sum_calibration_values(text) do
    text
    |> String.split("\n")
    |> Enum.map(&extract_first_last_numbers/1)
    |> Enum.sum()
  end

  defp extract_first_last_numbers(line) do
    String.to_integer("#{find_first_number(line)}#{find_last_number(line)}")
  end

  defp find_first_number(str) do
    Enum.find_value(@number_map, fn {key, value} ->
      if String.starts_with?(str, key), do: value, else: nil
    end) || find_first_number(String.slice(str, 1..-1))
  end

  defp find_last_number(str) do
    Enum.find_value(@number_map, fn {key, value} ->
      if String.ends_with?(str, key), do: value, else: nil
    end) || find_last_number(String.slice(str, 0..-2))
  end
end
2
Post #45
Aetherus

Aetherus

Welcome to the Elixir world.

For me, Elixir is the most interesting language I’ve ever programmed with. It opened a whole new world of thinking to me. It certainly requires a lot of practice, but it’s fun, especially the pattern matching part.

3
Post #46
— All posts loaded —