Skip to content

Commit

Permalink
Day 16 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitpas committed Dec 16, 2023
1 parent 8335b8c commit 3d12b4e
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The way I write the code here is very different from how I would write code for

In a prod system the approach would be totally different, the code would handle as many variations of the input as possible but would report in the monitoring system unexpected inputs so that we can analyse it and maybe report to the customer upstream that it looks they is an issue with their requests (or we would fix our system if we misunderstood what the customer wants or the data we should expect).

Another difference is that appart from the function that reads the input file, I don't reuse code between problems. Altough, there are numerous functions (like converting an asci map to a grid), I don't put them in a library because I want each day's solution to be understandable on its own.

## Day1
Part 1 was straightforward and helped me setup my project
Part 2 has a nice twist,because of cases like 'eightwothree' we can use a simple replacement logic like the following:
Expand Down Expand Up @@ -115,4 +117,8 @@ Part 2: By changing the order the rounded rocks to also do a one pass processing

Part 1: Suprisingly simple !

Part 2: The main difficulty is understanding the instructions and then find out how to model the data.
Part 2: The main difficulty is understanding the instructions and then find out how to model the data.

## Day 16

Part 1: Another grid, another choice on how to store the grid. To avoid having to check if the coordinates are out of the grid, I'm going to store the points in a map. Also the light beams can get into a 'loop' so the logic needs to keep track of the location/directions which have already been explored.
110 changes: 110 additions & 0 deletions src/main/resources/day16_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
\.......................|..-.............../../.......|...............\.\.....|..../.............|............
........................../....../..............................-............-.-..--.........-...........\/...
....\............../...........-...-......|.............................\...............-........-....|.......
......\....|................./.\................/..................../.......................|..\......-......
.................../..................-./.................../.....-...................|......\.......|\...\./.
.....|........../....................\../..\.../.\.......-.............\...........\......|.\..-..........-...
..../............/.....\...................../..--\........|......\....-...|.-...../......-........../........
..............\.-............./........./....|\.......\..........|.................|.|.................-......
.....|..........-................-.../..........-........................\.......|.........................-..
.|.-......./....................\........-.............../..\.|.-\...........\./.................-............
................|/............................../..-.\..................-..../../...../.../..../..........|...
........................./.....................\..\....-....../-.....................\|.........././.-........
...\.....................-...../|............--......-..|...../.....|......|.../.\....|.............\|.......\
.........-...-.....|.........../...-/........|......./|............-.......-.....\.............\|....../......
.....|...|.................|...../.............|.................../..........\|..\......|..|....-............
...........................-.........\......\.............\...................-.....-.\.......-.....|.........
.................-.............-......./.............-..\./......|....-..-..........-....-............\.......
.\..\.......-......|........................\.............-.......................|.........|\.|..\......--/..
..........-.....||--........|........-....../...........|../...-.........../.....-.\..........|.......-.......
..........\..........|..................\.................-..../............/..........|.......-........../...
..--......../..........-..|........./.........-...../........-.................|..\............../.....-......
.....\.............|...../......../............./||..........................\............|...................
..-.................-|..-....................\.................|...\.....\............./......-.......\.......
............-../......\...../.-....-.....-....-./../......\..|..|............................|.../..|.........
-................./................................................|...............|....|.....-.....|....../..
...........-...................|...........\.......-..\.......-...........|..-....../........|...............|
.........|...|-........../...-..........|.........\.....||......./....|...............\......-......\...../...
....|.........|............\.-.......................................-.......................|.-..|.\...\.-...
.|....|....-/....................\\..........................|........\|...|..............\..|......-.\../....
..|.\......|..../.....|.......-....................|.\..\............/......................|.....\../../.....
.......|\\.\/......................./.............|................../.........|.....\-.......................
......................................../.../................\-..............-................./......\.......
............../............/\......./........................./......|............\/..........................
......./................|......../.........-............\.../.|....../....|...../..../......../...............
...................|............-\........../...\.......\....\.....-.........-./....|.........................
.....-..-\.................|\...........................|..........\........-...|.............................
..............................-.............-.......\......................../.......\|.......|...-....-......
\...................\..../.................\................|......-...-............./.-......................
........................./.|./...|..\.-............|.-.....-..........\....-..../.....|.......|...............
.................../......../..................|.|...............|.......|\.....|......|...............\/..|..
.........|....-......../........-./..|.../..|./...................-........|..................................
..|../............-......-.........................-..|....-................-..\....../...........|...........
....................-...............|.../.....|..../......../...|.......\.....-.....\..............-..\.-.....
...........\.........../............................................\......-................-.......|.....-...
..-............./.......-\...........|-..-../............../................................../...............
.................\.......\............../.|.......-...\............................................\.-........
.\....|....\......|........./.....--......................................|................\..|-..............
......../...............-..........................|...........................-.............-\.-..|..........
.......|......|/...\........./......../.....\.........\.......................-...........\..\................
.-..-../.............|................./../.................\........-......|.\.....||...................\|...
/......../....\.....|-..........\..............-.....|............/\......\...//....................\...-/....
................-...........-.-\-..........\.-./......../-................./../.\........................./...
-.........-.....|.....-..|..|.........|...................-..........\/.-.....................................
......./...../......\............-.|...............|..../.........|....\.................../..............|...
...........|.......|....|...............\........................\/..\......................................\.
......\........-.........................\..........\.............\......................\/.....\..|..........
......\...........|.........../..-............./........................../...-......../...............\..|...
..|............\../........../............\...|.......................-...............\-../.|...|\.....\......
.|....\.|....-|.........\......./.................../.|........|....../.--.\.....\..............-\\.........-.
......................./.......\...........|.\............-.....\.............\.........--......../.\..-.|../.
......../.............../......\.\....|.............../...........................-........-......../.........
.......|\.............|.../|/........../....\.....-....|.............-......./........\..\........\..|......-.
.............../...........-.....\..\....-...../...|......\...........................\...|...........|/......
.............|....../.................../..|.....................|.......|....//..............\...............
......./....\............................-.........................-/......../|......-....|.............../...
...........................|...................\...............................-.................|...........\
.............................-......./..../...................\.../...........-.|......................||.....
\....\....\...-./......../.........\................./................\...................|...........-.......
................-.../............/\..................\.\..-....../................./..........|...............
......../...\.......................\.......\..\......|.....|../..........\..........-.............|..........
../.........................................-\..........-.........|-./.....-...|..|.......\........-...../../.
.....|..........-......../|-..............|.................-...........|...\.....................\...........
.......|..............\....\.....-............../..\\..\.......-........./.........-..|...|................../
..........-.......-.-................../.........\\...../.....|..........\......./.|.............-...........\
..................................-..................|...........\../\...../..../...\................/........
...-.........................................................\.|..............\..................\............
-.|..|...................../../.|.......\.....|.....\.\...\.........-.................................../.....
...\./....../........\.......-.................\.........|............-.........../...........\.|.........|.\.
..........-...\.............-.-............-.-................/.....................|........|............-...
|......./............/..|..............\...............|.......|\................/....|.........|.........\..\
.....\../\./.....................-.............\..................|.|.........................................
............/......../.../.......\............/.........................\........................\............
....../..|.............../....................-.......\................................................|.....\
................................../.......././.......\.....|.\......-.........-..||......../........-.//......
........................\.......|............-....-...../..............................-............-....|...-
.....-.......|......\..|.........-....|......\......../..../.......\..........|....\.............||..\........
..........|...........-|.......\.......-....../...../..........||.......................\............-..|.....
.................../../..............................|....|................/........|-....|................/..
.....................\................/............................/-........./...|.............\...|..|\.....
.....\..\..........\......../......|.........-....-..-..|...........\....|./....\.\..\............../.........
.......-...............-....../...........-.......................|../...............|...|.........../..../...
.............|.../..................\..\..........-........||................\/...........-|..................
.............-..............-\.../.................|......\...../..........\...-.....-....../................\
.....-....................................-.........\/............../.............................|...........
./.|../......\\..........................................|..............\|/..-\..........................\....
...........-.........\............./....-........-....-....-.........\..\....-\...-...............--.........-
..\.............................\............................./\..........-.............-...-./........../....
.................../........\....|..-....|||./..............-......-./....................../..|...|..........
......\.........../............|\...|.-................../....................|\..|..\.............-./......|.
...........-...................-.|........................\...............|/..-...\...|/\...........|.........
............|..../..................................\.......................\........................-........
.\..........................|......|......../.|....................../.......\......\.............../.........
..........-.../............/..........................|....-......................|.-................|.-......
......../............-...|.....|......../-.....-|...|./.......-..................-\...........................
...............\.\/.-....././................-.../..../.............../-|.....-.......|......./..--...........
..........-\\.|-................./../..|.-.....--................../.................../.|.......-............
..\.................-..............-...............\.....|......................./..\.....--........./..../..\
.......-....../..............|..................\....|..|..........-.....|.|.-.........................\......
..../..\...\....................................||.|......................................|.......\/.........-
................\-.-.....||....\.........................-...................................................|
44 changes: 44 additions & 0 deletions src/main/scala/day16.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import zio._
import zio.Console._

object Day16 extends ZIOAppDefault {

def part1(grid: List[String]) =
val gMap = {
for
(y, l) <- (0 to grid.length - 1) zip grid
(x, c) <- (0 to l.length - 1) zip l
yield ((x, y), c)
}.toMap

val iP = (0, 0)
val iD = (1, 0)
val beams = LazyList.iterate((Set((iP, iD)), Set[((Int, Int), (Int, Int))]())) { (lpd, s) =>

val nextPoints = lpd.flatMap { (p, d) =>
val nP = (p._1 + d._1, p._2 + d._2)
gMap.get(p) match
case _ if s.contains((p, d)) => List()
case Some('.') => List(((p, d), (nP, d)))
case Some('\\') => List(((p, d), ((p._1 + d._2, p._2 + d._1), (d._2, d._1))))
case Some('/') => List(((p, d), ((p._1 - d._2, p._2 - d._1), (-d._2, -d._1))))
case Some('|') if d._1 == 0 => List(((p, d), (nP, d)))
case Some('|') =>
List(((p, d), ((p._1, p._2 + d._1), (0, d._1))), ((p, d), ((p._1, p._2 - d._1), (0, -d._1))))
case Some('-') if d._2 == 0 => List(((p, d), (nP, d)))
case Some('-') =>
List(((p, d), ((p._1 + d._2, p._2), (d._2, 0))), ((p, d), ((p._1 - d._2, p._2), (-d._2, 0))))
case None => List()
}
val nS = s ++ nextPoints.map(_._1).toSet
val nLpd = nextPoints.map(_._2)
(nLpd, nS)
}
beams.filter(_._1.size == 0).head._2.map(_._1).toSet.size

def run =
for {
v <- Day1.readFile("day16_input.txt")
_ <- printLine(s"part1=${part1(v)}")
} yield ()
}
21 changes: 21 additions & 0 deletions src/test/scala/day16_test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import org.junit.Test
import org.junit.Assert._

class Day16Test {

val grid = """.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....""".split('\n').toList

@Test
def testPart1() =
assertEquals(46, Day16.part1(grid))

}

0 comments on commit 3d12b4e

Please sign in to comment.