-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import zio._ | ||
import zio.Console._ | ||
import scala.io.BufferedSource | ||
|
||
object Day2 extends ZIOAppDefault { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import zio._ | ||
import zio.Console._ | ||
|
||
object Day3 extends ZIOAppDefault { | ||
|
||
def padSchematics(schematics: List[String]) = | ||
val s2 = schematics.map("." + _ + ".") | ||
val pad = "." * s2(0).length | ||
pad :: s2.appended(pad) | ||
|
||
def horizontalZip(pSchematics: List[String]): List[List[(Char, Char, Char)]] = | ||
val n = pSchematics.length - 2 | ||
val psr1 = pSchematics.take(n) | ||
val psr2 = pSchematics.tail.take(n) | ||
val psr3 = pSchematics.tail.tail | ||
((psr1 zip psr2) zip psr3) map ((a, b) => | ||
val bt = List(a._1, a._2, b) | ||
bt.transpose.map { case c1 :: c2 :: c3 :: _ => | ||
(c1, c2, c3) | ||
} | ||
) | ||
|
||
def isDigit(c: Char) = '0' <= c && c <= '9' | ||
|
||
def isPart(c: Char): Boolean = !(isDigit(c) || c == '.') | ||
|
||
def isPart(col: (Char, Char, Char)): Boolean = isPart(col._1) || isPart(col._2) || isPart(col._3) | ||
|
||
def findPartsNumber(zSchematics: List[(Char, Char, Char)]): List[Int] = | ||
zSchematics | ||
.foldLeft((List[Int](), "", false)) { | ||
case ((l, digits, isPartNumber), col) if isDigit(col._2) => | ||
(l, digits.appended(col._2), isPartNumber || isPart(col)) | ||
case ((l, digits, isPartNumber), col) => | ||
( | ||
if (isPartNumber || isPart(col)) && digits != "" then l.appended(digits.toInt) | ||
else l, | ||
"", | ||
isPart(col) | ||
) | ||
} | ||
._1 | ||
|
||
def part1(schematics: List[String]) = | ||
val zs = horizontalZip(padSchematics(schematics)) | ||
zs.flatMap(findPartsNumber).sum | ||
|
||
def run = | ||
for { | ||
v <- Day1.readFile("day3_input.txt") | ||
_ <- printLine(s"part1=${part1(v)}") | ||
} yield () | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import org.junit.Test | ||
import org.junit.Assert._ | ||
|
||
class Day3Test { | ||
|
||
val schematics = List( | ||
"467..114..", | ||
"...*......", | ||
"..35..633.", | ||
"......#...", | ||
"617*......", | ||
".....+.58.", | ||
"..592.....", | ||
"......755.", | ||
"...$.*....", | ||
".664.598.." | ||
) | ||
|
||
@Test def testPadSchematics(): Unit = | ||
assertEquals(List("...", ".#.", "..."), Day3.padSchematics(List("#"))) | ||
|
||
@Test def testHorizontalZip(): Unit = | ||
assertEquals( | ||
List(List(('1', '3', '5'), ('2', '4', '6')), List(('3', '5', '7'), ('4', '6', '8'))), | ||
Day3.horizontalZip(List("12", "34", "56", "78")) | ||
) | ||
|
||
@Test def testFindPartsNumber(): Unit = | ||
val hz = Day3.horizontalZip(List(schematics(5), schematics(6), schematics(7)))(0) | ||
assertEquals(List(592), Day3.findPartsNumber(hz)) | ||
|
||
@Test def testPart1(): Unit = | ||
assertEquals(4361, Day3.part1(schematics)); | ||
} |