Skip to content

Commit

Permalink
Day 6 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitpas committed Dec 6, 2023
1 parent 6dce2df commit 7200082
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,15 @@ While implementing part 2, it was quite satisfying to be able to parametrise `ho

Part 1 is really simple, just a matter of converting the list of numbers to set and compute the intersection to find the numbers in common.

Part 2 looks like a good showcase of recursion.
Part 2 looks like a good showcase of recursion.

## Day 5

Part 1, no real difficulty, just need to use 64 bits signed ints to store the numbers.

For Part 2, we need to adapt the algorithm to work with ranges instead of numbers. Because there are too many numbers (seeds), we cannot just expand the ranges to number and use part 1.

## Day 6
Part 1, the distances follow a discrete [parabola](https://en.wikipedia.org/wiki/Parabola). To simply count the number of points it is easier to iterate but it ia probably quite possible to find an analytical solution.

Part 2, as the numbers are much bigger here we need the analytical solution ;-).
2 changes: 2 additions & 0 deletions src/main/resources/day6_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 46 80 78 66
Distance: 214 1177 1402 1024
31 changes: 31 additions & 0 deletions src/main/scala/day6.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import zio._
import zio.Console._

object Day6 extends ZIOAppDefault {

def raceDistances(time: Int) =
(1 to time - 1).map(s => (time - s) * s)

def part1(races: List[String]) =
def toIntList(s: String) = s
.split(':')(1)
.split(' ')
.flatMap {
case "" => None
case s => Some(s.toInt)
}
.toList
val times = toIntList(races(0))
val distances = toIntList(races(1))
(times zip distances)
.map { (t, d) =>
raceDistances(t).filter(_ > d).size
}
.reduce(_ * _)

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

class Day6Test {

val races = List("Time: 7 15 30", "Distance: 9 40 200")

@Test
def testRaceDistances() =
assertEquals(List(6, 10, 12, 12, 10, 6), Day6.raceDistances(7))

@Test
def testPart1() =
assertEquals(288, Day6.part1(races))
}

0 comments on commit 7200082

Please sign in to comment.