bismark
Advent Of Code 2022 - Day 4
Took me a minute to remember my binary math
…
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
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)
4
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
4
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 ![]()
4
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}"))
4
Popular in Challenges
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
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
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
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
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
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
This topic is about Day 2 of the Advent of Code 2020.
New
Probably not the most efficient implementation, because part 1 took >1 ms and part 2 >4ms, but the code was simple enough.
def p...
New
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
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
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
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
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
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
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
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
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
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
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
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







