this post was submitted on 13 Dec 2023
13 points (88.2% liked)

Advent Of Code

770 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
13
submitted 11 months ago* (last edited 11 months ago) by Ategon to c/advent_of_code
 

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

you are viewing a single comment's thread
view the rest of the comments
[–] cvttsd2si 4 points 11 months ago (1 children)

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

[–] [email protected] 2 points 11 months ago (1 children)

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.

[–] cvttsd2si 2 points 11 months ago* (last edited 11 months ago) (1 children)

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)
[–] [email protected] 2 points 11 months ago

Oh, I see, I for some reason switched zip operation with β€œcreate pair/tuple” operation in my head. Thanks for clarification.