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

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
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews