-
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
3 changed files
with
90 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import zio._ | ||
import zio.Console._ | ||
import Day6.nbDistances | ||
|
||
object Day11 extends ZIOAppDefault { | ||
|
||
def findSpaces(coordinates: Set[Long]) = | ||
val min = coordinates.min | ||
val max = coordinates.max | ||
(min to max).toSet.diff(coordinates) | ||
|
||
def getDistance(g1: (Long, Long), g2: (Long, Long), emptyCols: Set[Long], emptyRows: Set[Long], expansion: Long) = | ||
val cols = | ||
(g1._1 - g2._1).abs + expansion * emptyCols.count(x => math.min(g1._1, g2._1) < x && x < math.max(g1._1, g2._1)) | ||
val rows = | ||
(g1._2 - g2._2).abs + expansion * emptyRows.count(y => math.min(g1._2, g2._2) < y && y < math.max(g1._2, g2._2)) | ||
cols + rows | ||
|
||
def part1(space: List[String], expansion: Int) = | ||
val galaxies = { | ||
for | ||
(y, l) <- (1L to space.length) zip space | ||
(x, c) <- (1L to l.length) zip l | ||
if c == '#' | ||
yield (x, y) | ||
}.toList | ||
|
||
val (xGalaxies, yGalaxies) = galaxies.unzip | ||
val emptyCols = findSpaces(xGalaxies.toSet) | ||
val emptyRows = findSpaces(yGalaxies.toSet) | ||
val distances = galaxies.toList.combinations(2).map { case g1 :: g2 :: _ => | ||
getDistance(g1, g2, emptyCols, emptyRows, expansion) | ||
} | ||
distances.sum | ||
|
||
def run = | ||
for { | ||
v <- Day1.readFile("day11_input.txt") | ||
_ <- printLine(s"part1=${part1(v, 1)}") | ||
_ <- printLine(s"part2=${part1(v, 999999)}") | ||
} 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,39 @@ | ||
import org.junit.Test | ||
import org.junit.Assert._ | ||
|
||
class Day11Test { | ||
|
||
val space = """...#...... | ||
.......#.. | ||
#......... | ||
.......... | ||
......#... | ||
.#........ | ||
.........# | ||
.......... | ||
.......#.. | ||
#...#.....""".split('\n').toList | ||
|
||
val space2 = List("#..#..#", ".......", ".......", "......#") | ||
|
||
@Test | ||
def testFindSpaces() = | ||
assertEquals(Set(3, 6, 9), Day11.findSpaces(Set(1, 2, 4, 5, 7, 8, 10))) | ||
|
||
@Test | ||
def testPart1() = | ||
assertEquals(374, Day11.part1(space, 1)) | ||
|
||
@Test | ||
def testPart1b() = | ||
assertEquals(50, Day11.part1(space2, 1)) | ||
|
||
@Test | ||
def testPart2a() = | ||
assertEquals(1030, Day11.part1(space, 9)) | ||
|
||
@Test | ||
def testPart2b() = | ||
assertEquals(8410, Day11.part1(space, 99)) | ||
|
||
} |