Skip to content

Commit 24a897f

Browse files
author
Eric Bowman
committed
2024/19 reduce to single implementation
1 parent 3a2c04c commit 24a897f

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

src/main/scala/y2024/Day19.scala

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package y2024
22

33
// see https://adventofcode.com/2024/day/19
44
class Day19 extends util.Day(19):
5-
def solvePart1(input: IndexedSeq[String]): Any =
5+
6+
def solvePart1(input: IndexedSeq[String]): Int =
67
val (patterns, designs) = parseInput(input)
7-
designs.count(design => canConstruct(design, patterns))
8+
designs.count(design => countConstruct(design, patterns) > 0)
89

9-
def solvePart2(input: IndexedSeq[String]): Any =
10+
def solvePart2(input: IndexedSeq[String]): Long =
1011
val (patterns, designs) = parseInput(input)
1112
designs.map(design => countConstruct(design, patterns)).sum
1213

@@ -18,23 +19,6 @@ class Day19 extends util.Day(19):
1819
case _ =>
1920
throw new IllegalArgumentException("Invalid input format.")
2021

21-
private def canConstruct(design: String, patterns: Set[String]): Boolean =
22-
def recurse(index: Int, memo: Map[Int, Boolean]): (Boolean, Map[Int, Boolean]) =
23-
if index == design.length then (true, memo)
24-
else
25-
memo.get(index) match
26-
case Some(result) => (result, memo)
27-
case None =>
28-
val possible = patterns.exists: pattern =>
29-
val end = index + pattern.length
30-
end <= design.length &&
31-
design.startsWith(pattern, index) &&
32-
recurse(end, memo)._1
33-
(possible, memo + (index -> possible))
34-
35-
recurse(0, Map.empty)._1
36-
end canConstruct
37-
3822
private def countConstruct(design: String, patterns: Set[String]): Long =
3923
def recurse(index: Int, memo: Map[Int, Long]): (Long, Map[Int, Long]) =
4024
if index == design.length then (1L, memo)
@@ -44,14 +28,13 @@ class Day19 extends util.Day(19):
4428
case None =>
4529
val (total, updatedMemo) = patterns.foldLeft((0L, memo)):
4630
case ((acc, currentMemo), pattern) =>
47-
val end = index + pattern.length
48-
if end <= design.length && design.startsWith(pattern, index) then
49-
val (count, newMemo) = recurse(end, currentMemo)
50-
(acc + count, newMemo)
51-
else
52-
(acc, currentMemo)
31+
val end = index + pattern.length
32+
if end <= design.length && design.startsWith(pattern, index) then
33+
val (count, newMemo) = recurse(end, currentMemo)
34+
(acc + count, newMemo)
35+
else
36+
(acc, currentMemo)
5337
(total, updatedMemo + (index -> total))
5438

5539
recurse(0, Map.empty)._1
56-
end countConstruct
5740
end Day19

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy