Skip to content

Commit

Permalink
Day 11 part 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitpas committed Dec 11, 2023
1 parent f542f82 commit b164186
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,12 @@ Part 2: Adapt the extrapolation algorithm to use the first element of the seqque

## Day 10

Part 1: From the start, find all the distances ['breadth' first](https://en.wikipedia.org/wiki/Breadth-first_search) and stop when all positions in the loop have been explored. Implemented using a LazyList.
Part 1: From the start, find all the distances ['breadth' first](https://en.wikipedia.org/wiki/Breadth-first_search) and stop when all positions in the loop have been explored. Implemented using a LazyList.

Part 2:To find the points which are inside or outside, we can count the number of intersection between an outside point and the inside point. if the number is odd, then the point is inside, if it is even then the point is outside.

## Day 11

Part 1: In the previous example, to store the map, I hesitated between using a two dimensional array or a map of the coordinates to the point at the location. Here as the galaxies are quite sparsed, it makes sense to only keep the coordinates. That will also help when 'expanding' the empty spaces.

Part 2: Here to store the larger coordinates I used Long instead of Int. The expansion coefficient was relatively easy to add
42 changes: 42 additions & 0 deletions src/main/scala/day11.scala
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 ()
}
39 changes: 39 additions & 0 deletions src/test/scala/day11_test.scala
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))

}

0 comments on commit b164186

Please sign in to comment.