Skip to content

Commit

Permalink
2024 day 19 - use async streams to speed things up dramatically
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenseacat committed Dec 19, 2024
1 parent 045c123 commit d09cd32
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/y2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ day 16, part 2 13.07 76.51 ms ±5.00% 76.09 ms 85.
day 17, part 1 45.64 K 21.91 μs ±15.81% 21.17 μs 37.50 μs
day 18, part 1 22.53 44.38 ms ±5.11% 44.48 ms 48.54 ms
day 18, part 2 4.04 247.58 ms ±3.18% 247.68 ms 260.90 ms
day 19, part 1 4.30 232.82 ms ±1.78% 230.67 ms 240.83 ms
day 19, part 2 4.31 232.07 ms ±0.63% 231.50 ms 236.27 ms
day 19, part 1 29.38 34.03 ms ±0.86% 34.01 ms 35.47 ms
day 19, part 2 29.36 34.06 ms ±1.18% 34.03 ms 36.73 ms
```
9 changes: 5 additions & 4 deletions lib/y2024/day19.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ defmodule Y2024.Day19 do
use Advent.Day, no: 19

def part1(%{from: from_towels, to: to_towels}) do
Enum.count(to_towels, fn to ->
ways_to_make(to, from_towels) != 0
Task.async_stream(to_towels, fn to ->
ways_to_make(to, from_towels)
end)
|> Enum.count(fn {:ok, count} -> count != 0 end)
end

def part2(%{from: from_towels, to: to_towels}) do
Enum.map(to_towels, fn to ->
Task.async_stream(to_towels, fn to ->
ways_to_make(to, from_towels)
end)
|> Enum.sum()
|> Enum.reduce(0, fn {:ok, count}, acc -> acc + count end)
end

def ways_to_make(to, from_towels) do
Expand Down

0 comments on commit d09cd32

Please sign in to comment.