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 1 to 10

code-shoily

code-shoily

I am grateful for MapSet module. Not every language is blessed with a set API that has common set operations.

https://github.com/code-shoily/advent_of_code/blob/master/lib/2022/day_04.ex

srowley

srowley

Second half was actually a little simpler.

# Part 1
day_4_data
|> String.split("\n")
|> Enum.map(fn ranges -> 
   ranges
   |> String.split(",")
   |> Enum.map(fn range -> 
        [start, finish] = String.split(range, "-")
        (String.to_integer(start)..String.to_integer(finish)) |> MapSet.new()
      end)
   end)
|> Enum.count(fn [first, second] -> 
     MapSet.subset?(first, second) || MapSet.subset?(second, first)
   end)

# Part 2
day_4_data
|> String.split("\n")
|> Enum.map(fn ranges -> 
     ranges
     |> String.split(",")
     |> Enum.map(fn range -> 
          [start, finish] = String.split(range, "-")          
          (String.to_integer(start)..String.to_integer(finish))
        end)
   end)
|> Enum.count(fn [first, second] -> not Range.disjoint?(first, second) end)
stevensonmt

stevensonmt

Basically did the same. I wasn’t as clever with your use of the && and || operators to convert to numbers. I just filtered and counted.

adamu

adamu

Ooh, TIL, I started with Ranges, but then converted them to MapSets - I didn’t consider that we can do it, especially part 2, directly with the range. This is why AoC is great :slight_smile:

  def part1(input) do
    Enum.count(input, fn [a, b] -> MapSet.subset?(a, b) or MapSet.subset?(b, a) end)
  end

  def part2(input) do
    Enum.count(input, fn [a, b] -> not MapSet.disjoint?(a, b) end)
  end

  def input do
    with [input_filename] <- System.argv(),
         {:ok, input} <- File.read(input_filename) do
      input
      |> String.split(["-", ",", "\n"], trim: true)
      |> Enum.map(&String.to_integer/1)
      |> Enum.chunk_every(2)
      |> Enum.map(fn [a, b] -> MapSet.new(a..b) end)
      |> Enum.chunk_every(2)
    else
      _ -> :error
    end
  end

https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2022/day4.exs

lkuty

lkuty

part 1

start = System.monotonic_time(:microsecond)

File.stream!("input.txt")
|> Stream.map(fn line ->
   Regex.run(~r/^(\d+)-(\d+),(\d+)-(\d+)$/, line, capture: :all_but_first)
   |> Enum.map(&String.to_integer/1)
end)
|> Enum.reduce(0, fn [lo1, hi1, lo2, hi2], count ->
  cond do
    lo1 >= lo2 && hi1 <= hi2 -> count + 1
    lo2 >= lo1 && hi2 <= hi1 -> count + 1
    true -> count
  end
end)
|> tap(fn count -> IO.puts "Number of contained assignment pairs: #{count}" end)

elapsed = System.monotonic_time(:microsecond) - start
IO.puts "Job done in #{elapsed} µs"

part 2 is very similar to part 1.

start = System.monotonic_time(:microsecond)

File.stream!("input.txt")
|> Stream.map(fn line ->
   Regex.run(~r/^(\d+)-(\d+),(\d+)-(\d+)$/, line, capture: :all_but_first)
   |> Enum.map(&String.to_integer/1)
end)
|> Enum.reduce(0, fn [lo1, hi1, lo2, hi2], count ->
  cond do
    lo1 >= lo2 && lo1 <= hi2 -> count + 1
    lo2 >= lo1 && lo2 <= hi1 -> count + 1
    hi1 >= lo2 && hi1 <= hi2 -> count + 1
    hi2 >= lo1 && hi2 <= hi1 -> count + 1
    true -> count
  end
end)
|> tap(fn count -> IO.puts "Number of overlapping assignment pairs: #{count}" end)

elapsed = System.monotonic_time(:microsecond) - start
IO.puts "Job done in #{elapsed} µs"
mudasobwa

mudasobwa

Creator of Cure
...
|> Enum.map(& &1 |> String.replace("-", "..") |> Code.eval_string() |> elem(0))

and then

|> Enum.map(fn [r1, r2] -> not Range.disjoint?(r1, r2) end)
kwando

kwando

clever parsing :sweat_smile:

markholmes

markholmes

I’ve been working through AOC using Gleam this year! This is probably pretty terrible Gleam code, but it’s Elixir-adjacent so I thought I’d share :slight_smile:

https://github.com/markholmes/aoc/blob/main/src/y_22/d_04.gleam

christhekeele

christhekeele

I also went with parsing into Ranges, and was also surprised to learn about Range.disjoint?/2 solving part two that literally did all the work compared to part one.

What I really found impressive is that disjoint? handles steps with ranges, as well! That’s a cool technique.

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
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
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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