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
deadbeef
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
Popular in Challenges
Hello, guys. I’m back again, but only for the weekends, maybe.
This topic is about Day 13 of the Advent of Code 2020 .
Thanks to @egze,...
New
So… that’s it? Everyone is stuck on part 2? :slight_smile: I looked at Reddit hints and thought I probably wouldn’t have come up with the...
New
Here is my solution for day 2 of Advent of Code:
https://github.com/bjorng/advent-of-code/blob/main/2024/day02/lib/day02.ex
New
This topic is about Day 9 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 8 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
This topic is about Day 5 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
I spent 3 hours struggling in part 2, until I noticed a very basic mistake :joy:
Here’s my code:
https://github.com/Aetherus/advent-of-...
New
Nobody’s doing Advent of Code this year? :grinning_face_with_smiling_eyes:
I might do the first week or so.
For Day 1, first I solved i...
New
Setting this down for the night, as after a quick naive solve for quick part 1 I realize that part 2 is by design computationally expensi...
New
This topic is about Day 14 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Other popular topics
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









