-
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
60 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,2 @@ | ||
Time: 46 80 78 66 | ||
Distance: 214 1177 1402 1024 |
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,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 () | ||
} |
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,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)) | ||
} |