bismark

bismark

Advent Of Code 2022 - Day 4

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…

Most Liked

deadbeef

deadbeef

Like others, using Range

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)
Aetherus

Aetherus

My simple solution:

defmodule Day04 do
  def part1(input_path) do
    input_path
    |> File.stream!()
    |> Stream.map(&String.trim/1)
    |> Stream.map(&String.split(&1, ~r/\D/, global: true))
    |> Stream.map(fn x -> Enum.map(x, &String.to_integer/1) end)
    |> Enum.count(fn [start1, end1, start2, end2] ->
      (start1 <= start2 and end1 >= end2) or
      (start1 >= start2 and end1 <= end2)
    end)
  end

  def part2(input_path) do
    input_path
    |> File.stream!()
    |> Stream.map(&String.trim/1)
    |> Stream.map(&String.split(&1, ~r/\D/, global: true))
    |> Stream.map(fn x -> Enum.map(x, &String.to_integer/1) end)
    |> Enum.count(fn [start1, end1, start2, end2] ->
      start1 <= end2 and start2 <= end1
    end)
  end
end
mruoss

mruoss

Advent of code always reminds me what a great “stdlib” Elixir comes with!

  parsed = 
    input
    |> String.split("\n", trim: true)
    |> Enum.flat_map(&String.split(&1, ~r/[-,]/))
    |> Enum.map(&String.to_integer/1)
    |> Enum.chunk_every(4)

part1 = Enum.count(parsed, fn [ax, ay, bx, by] -> (ax <= bx and ay >= by) or (ax >= bx and ay <= by) end)
part2 = Enum.count(parsed, fn [ax, ay, bx, by] -> (bx <= ay and by >= ax) end)

Edit: @Aetherus you beat me by a few seconds :wink:

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}"))

Where Next?

Popular in Challenges Top

sneako
Note by the Moderators: This topic is to talk about the first day of the Advent of Code. For general discussion about the Advent of Code...
New
bjorng
Note: This topic is to talk about Day 16 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
sukhmeetsd
All in all, from what I understand, it is better not to use GenServer.cast when we want some concurrent operations to happen for sure, be...
New
NobbZ
Note by the Moderators: This topic is to talk about the Day 2 of the Advent of Code. For general discussion about the Advent of Code 201...
New
stevensonmt
Trying to get more facility with dynamic programming concepts on Leetcode and having an issue I can’t find a way around. It’s a chutes an...
New
bjorng
This topic is about Day 15 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
New
adamu
Probably not the most efficient implementation, because part 1 took &gt;1 ms and part 2 &gt;4ms, but the code was simple enough. def p...
New
seeplusplus
Hello all, hopefully I post this before someone else does and I don’t dupe. IMO Day 4 was much easier than Day 3 (yay, I can sleep befor...
New
coen.bakker
Since I started using Elixir, I have benefited greatly from being able to study various open-source projects. The codebase of LiveBook, i...
New

Other popular topics Top

sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement