-
-
Save tong92/d7822451695b398e21f14f133f9f3a4c to your computer and use it in GitHub Desktop.
aoc-2022-10
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
import scala.io.Source | |
def run(xs: List[Int], x: Int = 1, cycle: Int, checkList: List[Int], res: List[Int] = List()): List[Int] = | |
checkList match | |
case Nil => res | |
case h :: t if h == cycle => run(xs, x, cycle, t, x * h :: res) | |
case _ => | |
xs match | |
case Nil => res | |
case h :: t => run(t, x + h, cycle + 1, checkList, res) | |
def change(xs: List[String], res: List[Int] = List()): List[Int] = | |
xs match | |
case Nil => res | |
case h :: t => | |
if h == "noop" then change(t, 0 :: res) | |
else change(t, h.split(" ")(1).toInt :: 0 :: res) | |
def ans1(xs: List[String]): Int = | |
val checkCycle = List(20, 60, 100, 140, 180, 220) | |
val t = run(xs = change(xs).reverse, cycle = 1, checkList = checkCycle) | |
t.sum | |
def run2(xs: List[Int], x: Int = 1, cycle: Int = 0, res: String = ""): String = | |
val next = if cycle >= x - 1 && cycle < x + 2 then "#" else "." | |
xs match | |
case Nil => res | |
case h :: t => run2(t, x + h, (cycle + 1) % 40, res + next) | |
def screen(xs: String): List[String] = | |
if xs.length > 40 then | |
val (h, t) = xs.splitAt(40) | |
h :: screen(t) | |
else | |
xs :: List() | |
def ans2(xs: List[String]): String = | |
val s = run2(xs = change(xs).reverse) | |
screen(s).mkString("\n") | |
def solve = | |
val input = Source.fromFile("day10.txt").getLines.toList | |
println(ans1(input)) | |
println(ans2(input)) | |
def test = | |
val input = Source.fromFile("ex10.txt").getLines.toList | |
val ans = """##..##..##..##..##..##..##..##..##..##.. | |
###...###...###...###...###...###...###. | |
####....####....####....####....####.... | |
#####.....#####.....#####.....#####..... | |
######......######......######......#### | |
#######.......#######.......#######.....""" | |
println(ans2(input)) | |
println() | |
println(ans) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment