this post was submitted on 12 Dec 2023
7 points (100.0% liked)

Advent Of Code

15 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 12: Hot Springs

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


๐Ÿ”’ Thread is locked until there's at least 100 2 star entries on the global leaderboard

๐Ÿ”“ Unlocked after 25 mins

you are viewing a single comment's thread
view the rest of the comments
[โ€“] cvttsd2si@programming.dev 3 points 11 months ago* (last edited 10 months ago)

Scala3

def countDyn(a: List[Char], b: List[Int]): Long =
    // Simple dynamic programming approach
    // We fill a table T, where
    //  T[ ai, bi ] -> number of ways to place b[bi..] in a[ai..]
    //  T[ ai, bi ] = 0 if an-ai >= b[bi..].sum + bn-bi
    //  T[ ai, bi ] = 1 if bi == b.size - 1 && ai == a.size - b[bi] - 1
    //  T[ ai, bi ] = 
    //   (place) T [ ai + b[bi], bi + 1]   if ? or # 
    //   (skip)  T [ ai + 1, bi ]          if ? or .
    // 
    def t(ai: Int, bi: Int, tbl: Map[(Int, Int), Long]): Long =
        if ai >= a.size then
            if bi >= b.size then 1L else 0L 
        else
            val place = Option.when(
                bi < b.size && // need to have piece left
                ai + b(bi) <= a.size && // piece needs to fit
                a.slice(ai, ai + b(bi)).forall(_ != '.') && // must be able to put piece there
                (ai + b(bi) == a.size || a(ai + b(bi)) != '#') // piece needs to actually end
            )((ai + b(bi) + 1, bi + 1)).flatMap(tbl.get).getOrElse(0L)
            val skip = Option.when(a(ai) != '#')((ai + 1, bi)).flatMap(tbl.get).getOrElse(0L)
            place + skip

    @tailrec def go(ai: Int, tbl: Map[(Int, Int), Long]): Long =
        if ai == 0 then t(ai, 0, tbl) else go(ai - 1, tbl ++ b.indices.inclusive.map(bi => (ai, bi) -> t(ai, bi, tbl)).toMap)

    go(a.indices.inclusive.last + 1, Map())

def countLinePossibilities(repeat: Int)(a: String): Long =
    a match
        case s"$pattern $counts" => 
            val p2 = List.fill(repeat)(pattern).mkString("?")
            val c2 = List.fill(repeat)(counts).mkString(",")
            countDyn(p2.toList, c2.split(",").map(_.toInt).toList)
        case _ => 0L


def task1(a: List[String]): Long = a.map(countLinePossibilities(1)).sum
def task2(a: List[String]): Long = a.map(countLinePossibilities(5)).sum

(Edit: fixed mangling of &<)