Created
December 25, 2020 11:48
-
-
Save sungkmi/bd75d95f4de473734f0693b64e6096fe to your computer and use it in GitHub Desktop.
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
package sungkmi.aoc2020.day3 | |
import scala.collection.JavaConverters._ | |
def count(dr: Int, dc: Int): Int = | |
@annotation.tailrec | |
def loop(r: Int, c: Int, acc: Int): Int = | |
//println((r, c)) | |
if (r >= rMax) acc else | |
val current = map(r)(c) match | |
case '.' => 0 | |
case '#' => 1 | |
loop(r + dr, (c + dc) % cMax, acc + current) | |
loop(0,0,0) | |
@main def part1: Unit = | |
val ans = count(2, 1) | |
println(ans) | |
@main def part2: Unit = | |
val subans = Seq((1, 1), (3, 1), (5, 1), (7, 1), (1, 2)).map{ | |
case (right, down) => BigInt(count(down, right)) | |
} | |
val ans = subans.product | |
println(ans) | |
lazy val map: IndexedSeq[IndexedSeq[Char]] = xs.map(_.trim.toIndexedSeq) | |
lazy val rMax = map.size | |
lazy val cMax = map(0).size | |
lazy val xs = input.trim.lines.iterator().asScala.toVector | |
// lazy val input = """.#....#..##.#..####....#.......""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment