From d09cd32bd74e5eab540f1d7179d394e31ae69296 Mon Sep 17 00:00:00 2001 From: Rebecca Le Date: Thu, 19 Dec 2024 17:19:42 +0800 Subject: [PATCH] 2024 day 19 - use async streams to speed things up dramatically --- lib/y2024/README.md | 4 ++-- lib/y2024/day19.ex | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/y2024/README.md b/lib/y2024/README.md index b352a01..4defaa8 100644 --- a/lib/y2024/README.md +++ b/lib/y2024/README.md @@ -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 ``` diff --git a/lib/y2024/day19.ex b/lib/y2024/day19.ex index bd48eee..68b0923 100644 --- a/lib/y2024/day19.ex +++ b/lib/y2024/day19.ex @@ -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