Skip Navigation

🌟 - 2023 DAY 13 SOLUTIONS -🌟

Day 13: Point of Incidence

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

15

You're viewing a single thread.

15 comments
  • Scala3

    // i is like
    //  # # # # #
    //   1 2 3 4
    def smudgesAround(i: Int, s: List[List[Char]]): Long =
        val toEdge = math.min(i, s.size - i)
        (0 until toEdge).map(e => s(i - e - 1).lazyZip(s(i + e)).count(_ != _)).sum
    
    def symmetries(g: List[List[Char]], smudges: Int) =
        val rows = (1 until g.size).filter(smudgesAround(_, g) == smudges)
        val g2 = g.transpose
        val cols = (1 until g2.size).filter(smudgesAround(_, g2) == smudges)
        100*rows.sum + cols.sum
    
    def task1(a: List[String]): Long = a.chunk(_ == "").map(g => symmetries(g.map(_.toList), 0)).sum
    def task2(a: List[String]): Long = a.chunk(_ == "").map(g => symmetries(g.map(_.toList), 1)).sum
    
    
    • Wouldn't this code fail in a case like this? Because you count different lines, not different characters in the lines, right? Or am I missing something? Thanks (Let's ignore column symmetry for the sake of example).

      ##
      ..
      ..
      ..
      

      (I think you will get answer 100, but the answer is 300)

      But for sure after seeing this I have to learn Scala.

      • On your example, my code produces 301, which matches your 300 ignoring column symmetry (and 0 for task2, as there is no way to smudge a single cell to create symmetry).

        Let me explain the code real quick: What smudgesAround does is compute the number of tiles you would need to change to create a symmetry around index i. First, toEdge is the number of tiles to the nearest edge, everything after that will be reflected outside the grid and won't matter. Then, for each actually relevant distance, we compare the rows/columns this distance from i. By zipping them, we create an Iterator that first yields the pair of first entries, then the pair of second entries, and so on. On each pair we apply a comparison, to count the number of entries which did not match. This is the number of smudges we need to fix to get these rows/columns to match. Summing over all relevant pairs of rows/columns, we get the total number of smudges to make i a mirror line. In symmetries, we just check each i, for task1 we want a mirror line without smudges and for task2 we want a mirror line for exactly one

        You can also make this quite a bit shorter, if you want to sacrifice some more readability:

        def compute(a: List[String], sm: Int) =
            a.chunk(_ == "").map(_.map(_.toList)).map(g => Seq(g, g.transpose).map(s => (1 until s.size).filter(i => (0 until math.min(i, s.size - i)).map(e => s(i - e - 1).zip(s(i + e)).count(_ != _)).sum == sm).sum).zip(Seq(100, 1)).map(_ * _).sum).sum
        
        def task1(a: List[String]): Long = compute(a, 0)
        def task2(a: List[String]): Long = compute(a, 1)
        
        • Oh, I see, I for some reason switched zip operation with β€œcreate pair/tuple” operation in my head. Thanks for clarification.

15 comments