Skip to content

Commit

Permalink
Day 13 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitpas committed Dec 15, 2023
1 parent 2827dc7 commit 8335b8c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
29 changes: 16 additions & 13 deletions src/main/scala/day13.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,50 @@ object Day13 extends ZIOAppDefault {
}
._1

def findVerticalReflection(pattern: Array[Array[Char]]): Option[Int] =
def findVerticalReflection(pattern: Array[Array[Char]], nbDiff: Int) =
val nbCol = pattern(0).length
val nbRow = pattern.length
(1 to nbCol - 1)
.to(LazyList)
.map { i =>
val n = math.min(i, nbCol - i)
// compare colomns
val eq = (1 to n).foldLeft(true) { case (a, j) =>
(0 to nbRow - 1).foldLeft(a) { (a, k) => a && pattern(k)(i - j) == pattern(k)(i + j - 1) }
val eq = (1 to n).foldLeft(0) { case (a, j) =>
(0 to nbRow - 1).foldLeft(a) { (a, k) => a + (if pattern(k)(i - j) == pattern(k)(i + j - 1) then 0 else 1) }
}
(eq, i)
(eq == nbDiff, i)
}
.find(_._1)
.map(_._2)

def findHorizontalReflection(pattern: Array[Array[Char]]) =
def findHorizontalReflection(pattern: Array[Array[Char]], nbDiff: Int) =
val nbCol = pattern(0).length
val nbRow = pattern.length
(1 to nbRow - 1)
.to(LazyList)
.map { i =>
val n = math.min(i, nbRow - i)
// compare rows
val eq = (1 to n).foldLeft(true) { case (a, j) =>
a && (pattern(i - j) sameElements pattern(i + j - 1))
val eq = (1 to n).foldLeft(0) { case (a, j) =>
(0 to nbCol - 1).foldLeft(a) { (a, k) => a + (if pattern(i - j)(k) == pattern(i + j - 1)(k) then 0 else 1) }
// a + ( if (pattern(i - j) sameElements pattern(i + j - 1)) then 0 else 1)
}
(eq, i)
(eq == nbDiff, i)
}
.find(_._1)
.map(_._2)

def findReflection(pattern: Array[Array[Char]]) =
findHorizontalReflection(pattern).getOrElse(0) * 100 + findVerticalReflection(pattern).getOrElse(0)
def findReflection(nbDiff: Int)(pattern: Array[Array[Char]]) =
findHorizontalReflection(pattern, nbDiff).getOrElse(0) * 100 + findVerticalReflection(pattern, nbDiff).getOrElse(0)

def part1(strings: List[String]) =
def part12(strings: List[String], nbDiff: Int) =
val patterns = parseValley(strings)
patterns.map(findReflection).sum
patterns.map(findReflection(nbDiff)).sum

def run =
for {
v <- Day1.readFile("day13_input.txt")
_ <- printLine(s"part1=${part1(v)}")
_ <- printLine(s"part1=${part12(v, 0)}")
_ <- printLine(s"part2=${part12(v, 1)}")
} yield ()
}
14 changes: 9 additions & 5 deletions src/test/scala/day13_test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ class Day13Test {
val patterns = Day13.parseValley(valley)

@Test
def testFindHorizontalReflection() =
assertEquals(Some(4), Day13.findHorizontalReflection(patterns(1)))
def testFindHorizontalReflection1() =
assertEquals(Some(4), Day13.findHorizontalReflection(patterns(1), 0))

@Test
def testFindReflection() =
assertEquals(3, Day13.findReflection(pattern))
def testFindReflection1() =
assertEquals(3, Day13.findReflection(0)(pattern))

@Test
def testPart1() =
assertEquals(405, Day13.part1(valley))
assertEquals(405, Day13.part12(valley, 0))

@Test
def testPart2() =
assertEquals(400, Day13.part12(valley, 1))

}

0 comments on commit 8335b8c

Please sign in to comment.