bismark

bismark

Took me a minute to remember my binary math :smile: :grimacing:..

import Bitwise

__DIR__
|> Path.join("puzzle.txt")
|> File.stream!()
|> Stream.filter(fn line ->
  line
  |> String.trim()
  |> String.split(",")
  |> Enum.map(fn range ->
    [s,e] = range |> String.split("-") |> Enum.map(& String.to_integer(&1))
    Integer.pow(2, e - s + 1) - 1 <<< (s - 1)
  end)
  |> Enum.reduce(fn int1, int2 ->
    intersection = int1 ||| int2
    intersection == int1 or intersection == int2
  end)
end)
|> Enum.count()
|> IO.puts()
import Bitwise

__DIR__
|> Path.join("puzzle.txt")
|> File.stream!()
|> Stream.filter(fn line ->
  line
  |> String.trim()
  |> String.split(",")
  |> Enum.map(fn range ->
    [s,e] = range |> String.split("-") |> Enum.map(& String.to_integer(&1))
    Integer.pow(2, e - s + 1) - 1 <<< (s - 1)
  end)
  |> Enum.reduce(fn int1, int2 ->
    (int1 &&& int2) != 0
  end)
end)
|> Enum.count()
|> IO.puts()

Edit: oops, I’m supposed to import not use Bitwise now..

Showing Posts 24 to 15

dmarcoux

dmarcoux

I decided to look into Regex, which I have never used before in Elixir. Here’s what I came up with, I don’t really like the part where I’m converting the strings to integers since this involves going through nested lists. I would have liked to do this somehow within Regex.scan/3, but I don’t know how. Maybe someone knows if it’s possible?

I’m happy with everything else. Especially since doing part 2 simply meant turning the and into or for the guard clauses in the Enum.reduce/3.

https://github.com/dmarcoux/advent_of_code_2022/blob/main/lib/advent_of_code2022/day4.ex

Tiger

Tiger

I went with a recursive solution for practice

defmodule Advent.Day4 do
  def getInput() do
    File.read!("inputs/day-4-input.txt")
    |> String.split("\n")
    |> Enum.map(fn line ->
      line
      |> String.split(",")
      |> Enum.map(fn orders ->
        orders
        |> String.split("-")
        |> Enum.map(&String.to_integer/1)
      end)
    end)
  end

  def score(x), do: if x, do: 1, else: 0

  def contains?([[a, b], [x, y]]) do
    (a >= x and b <= y) or (a <= x and b >= y)
  end

  def containing([]), do: 0
  def containing([pair | rest]) do
    score(contains? pair) + containing rest
  end

  def solution1() do
    getInput() |> containing
  end

  def overlaps?([[a, b], [x, y]]) do
    (b >= x and a <= y) or (y >= a and x <= b)
  end

  def overlapping([]), do: 0
  def overlapping([pair | rest]) do
    score(overlaps? pair) + overlapping rest
  end

  def solution2() do
    getInput() |> overlapping
  end
end

IO.puts Advent.Day4.solution1

IO.puts Advent.Day4.solution2

dams

dams

simple Elixir, using MapSet.subset? and MapSet.disjoint?, both questions at the same time

File.stream!("input")
|> Enum.reduce({0,0}, fn line, {acc_part1, acc_part2} ->
    [s1, e1, s2, e2] = line
      |> String.split(~r/\D/, trim: true)
      |> Enum.map(&String.to_integer/1)
    {m1, m2} = {MapSet.new(s1..e1), MapSet.new(s2..e2)}
    {
      acc_part1 + (if MapSet.subset?(m1,m2) || MapSet.subset?(m2,m1), do: 1, else: 0),
      acc_part2 + (if MapSet.disjoint?(m1,m2), do: 0, else: 1),
    }
end)
|> IO.inspect()
marcelfahle

marcelfahle

Yea, MapSet all the way on this one :metal:

defmodule ExAOC2022.Day4 do
  @input "./lib/day4_input.txt"

  def puzzle1() do
    @input
    |> file_by_line()
    |> Enum.map(&line_to_elfarea/1)
    |> Enum.map(&subset?/1)
    |> Enum.reject(& !&1)
    |> length()
  end

  def puzzle2() do
    @input
    |> file_by_line()
    |> Enum.map(&line_to_elfarea/1)
    |> Enum.map(&intersect?/1)
    |> Enum.reject(& !&1)
    |> length()
  end

  defp line_to_elfarea(line) do
    line
    |> String.split(",")
    |> IO.inspect()
    |> Enum.map(&to_map_set/1)
  end

  defp to_map_set(area_str) do
    [first, last] = String.split(area_str, "-")

    Range.new(String.to_integer(first), String.to_integer(last))
    |> Enum.to_list
    |> MapSet.new()
  end

  defp subset?([one, two]) do
    MapSet.subset?(one, two) || MapSet.subset?(two, one)
  end

  defp intersect?([one, two]) do
    MapSet.size(MapSet.intersection(one, two)) > 0
  end

  defp file_by_line(file) do
    file
    |> File.read!()
    |> String.split(~r/\R/, trim: true)
  end
end
tfwright

tfwright

All my answers look the same :sob:

I thought about using a regex to extract the digits as I see others did with success, but I have a deeply embedded habit of strictly enforcing formatting rules for input like this (although I note @egze’s use does appear to enforce them without too much pain). Since there are a known quantity of sections though in a real case I would probably insist the format be changed to use leading zeros :smile:

egze

egze

Part 1

input
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
  %{"a1" => a1, "a2" => a2, "b1" => b1, "b2" => b2} =
    Regex.named_captures(~r/(?<a1>\d+)-(?<a2>\d+),(?<b1>\d+)-(?<b2>\d+)/, line)
  {Range.new(String.to_integer(a1), String.to_integer(a2)), Range.new(String.to_integer(b1), String.to_integer(b2))}
end)
|> Enum.filter(fn {r1, r2} ->
  s1 = MapSet.new(r1)
  s2 = MapSet.new(r2)

  MapSet.subset?(s1, s2) || MapSet.subset?(s2, s1)
end)
|> length()

Part 2

input
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
  %{"a1" => a1, "a2" => a2, "b1" => b1, "b2" => b2} =
    Regex.named_captures(~r/(?<a1>\d+)-(?<a2>\d+),(?<b1>\d+)-(?<b2>\d+)/, line)
  {Range.new(String.to_integer(a1), String.to_integer(a2)), Range.new(String.to_integer(b1), String.to_integer(b2))}
end)
|> Enum.reject(fn {r1, r2} ->
  s1 = MapSet.new(r1)
  s2 = MapSet.new(r2)

  MapSet.disjoint?(s1, s2)
end)
|> length()
bossek

bossek

day04.exs:

solve = fn oper ->
  "data/04"
  |> File.read!()
  |> String.split(["\n", ",", "-"], trim: true)
  |> Enum.map(&String.to_integer/1)
  |> Enum.chunk_every(4)
  |> Enum.filter(fn [a, b, c, d] -> oper.(c in a..b, d in a..b) or oper.(a in c..d, b in c..d) end)
  |> Enum.count()
end

(&and/2)
|> then(solve)
|> tap(&IO.puts("Part 1: #{&1}"))

(&or/2)
|> then(solve)
|> tap(&IO.puts("Part 2: #{&1}"))
hst337

hst337

One more day for scanning

defmodule AOC do
  def traverse(string, number \\ 0, line \\ [], acc \\ 0)
  def traverse("", 0, [], acc), do: acc
  def traverse(string, 0, [r2, l2, r1, l1], acc) do
    cond do
      r1 < l2 ->
        traverse(string, 0, [], acc)

      r2 < l1 ->
        traverse(string, 0, [], acc)

      true ->
        traverse(string, 0, [], acc + 1)
    end
  end
  def traverse(string, number, line, acc) do
    case string do
      <<splitter, tail :: binary>> when splitter in [?-, ?,, ?\n] ->
        traverse(tail, 0, [number | line], acc)

      <<int, tail :: binary>> ->
        traverse(tail, number * 10 + int - ?0, line, acc)

      "" ->
        traverse("", 0, [number | line], acc)

      "\n" ->
        traverse("", 0, [number | line], acc)
    end
  end
end

IO.inspect AOC.traverse IO.read :eof
newton-peixoto

newton-peixoto

Probably not the most performant code but it does the job. Suggestions are appreciated

https://github.com/newton-peixoto/advent-of-code/blob/main/2022/livebooks/day-04.livemd

Where Next? Top

Trending in Challenges Top

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews