Created
December 5, 2017 12:45
-
-
Save borkdude/b37563939639b40013f483361a7c5d8f to your computer and use it in GitHub Desktop.
Advent of Code 2015 Day 6, Scala vs. Clojure
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
(ns adventofcode.day6stackoverflow) | |
(set! *unchecked-math* true) | |
(defn turn-off [^ints grid [r1 c1 r2 c2]] | |
(doseq [i (range r1 (inc r2)) | |
k (range (+ c1 (* i 1000)) (+ c2 (* i 1000) 1))] | |
(aset grid k 0))) | |
(defn turn-on [^ints grid [r1 c1 r2 c2]] | |
(doseq [i (range r1 (inc r2)) | |
k (range (+ c1 (* i 1000)) (+ c2 (* i 1000) 1))] | |
(aset grid k 1))) | |
(defn toggle [^ints grid [r1 c1 r2 c2]] | |
(doseq [i (range r1 (inc r2)) | |
k (range (+ c1 (* i 1000)) (+ c2 (* i 1000) 1))] | |
(aset grid k (- 1 (aget grid k))))) | |
(defn count-on [^ints grid] | |
(areduce grid i cnt 0 (+ cnt (aget grid i)))) | |
(defn day06a [] | |
(let [grid (int-array (* 1000 1000))] ; 0-initialized via Java | |
(with-open [rdr (clojure.java.io/reader | |
(clojure.java.io/resource "input-day6.txt"))] | |
(doseq [cmd (line-seq rdr)] | |
(let [v (map #(Integer/parseInt %) (re-seq #"[0-9]+" cmd))] | |
(cond (.startsWith cmd "turn off") (turn-off grid v) | |
(.startsWith cmd "turn on") (turn-on grid v) | |
(.startsWith cmd "toggle") (toggle grid v))))) | |
(count-on grid))) |
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 | |
object Day6Pt2 { | |
case class CommandLine(command: String, | |
leftTop: (Int, Int), | |
rightBottom: (Int, Int)) | |
case object CommandLine { | |
def fromString(s: String): CommandLine = { | |
val preProcessed = s.replace("turn off", "turn-off") | |
.replace("turn on", "turn-on") | |
.split(' ') | |
val command = preProcessed(0) | |
val leftTopString = preProcessed(1) | |
val rightBottomString = preProcessed(3) | |
def parseCoordinates(s: String): (Int, Int) = { | |
val pairsString = s.split(',') | |
(pairsString(0).toInt, pairsString(1).toInt) | |
} | |
CommandLine(command, parseCoordinates(leftTopString), parseCoordinates(rightBottomString)) | |
} | |
} | |
class Grid(grid: Array[Array[Int]]) { | |
def updateGrid(c: CommandLine): Unit = { | |
val (x1, y1) = c.leftTop | |
val (x2, y2) = c.rightBottom | |
for (x <- x1 to x2) { | |
val row = grid(x) | |
for (y <- y1 to y2) { | |
val oldValue = grid(x)(y) | |
c.command match { | |
case "turn-off" => grid(x)(y) = 0 max (oldValue - 1) | |
case "turn-on" => grid(x)(y) = oldValue + 1 | |
case "toggle" => grid(x)(y) = oldValue + 2 | |
} | |
} | |
} | |
} | |
def totalBrightness: Int = { | |
grid.flatten.sum | |
} | |
} | |
def main(args: Array[String]) = { | |
val lines = Source.fromFile("input-day6.txt").getLines() | |
val grid = new Grid(Array.tabulate(1000, 1000)((x, y) => 0)) | |
for (line <- lines) { | |
val command = CommandLine.fromString(line) | |
grid.updateGrid(command) | |
} | |
println(grid.totalBrightness) | |
val x = List("123","456","789") | |
x.mkString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment