This file contains hidden or 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 aoc-2021.day01 | |
(:require [clojure.java.io :as io])) | |
(defn parse-input [file] | |
(map #(Long/parseLong %) | |
(line-seq (io/reader (io/resource file))))) | |
(defn compare-and-increment [count window] | |
(let [[val1 val2 & _] window] | |
(if (> val2 val1) |
This file contains hidden or 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
object Day09 extends SimpleCommonPuzzle[Seq[Long], Long, Long] { | |
override def parse(resource: String): Seq[Long] = readResource(resource).map(_.toLong) | |
override def part1(input: Seq[Long]): Long = bugFinder(list = input) | |
override def part2(input: Seq[Long]): Long = { | |
val solutionPart1 = part1(input) | |
val contiguousSet = findContiguousSet(input, solutionPart1, 0).get | |
contiguousSet.min + contiguousSet.max |
This file contains hidden or 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.collection.{mutable => m} | |
case class Instruction(operation: String, argument: Int) | |
sealed trait TerminationCause | |
case object Completed extends TerminationCause | |
case object InfiniteLoop extends TerminationCause | |
case class LoopResult(accumulator: Int, terminationCause: TerminationCause) | |
object InstructionNames { |
This file contains hidden or 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
object Day06 extends SimpleCommonPuzzle[Array[String], Int, Int] { | |
override def parse(resource: String): Array[String] = | |
readResourceAsUtf8String(resource) | |
.split("""(\r\n|\r|\n){2,}""") | |
override def part1(answerGroups: Array[String]) = | |
answerGroups.map(_.replace("\n", "")).map(_.toCharArray.toSet).map(_.size).sum | |
override def part2(answerGroups: Array[String]) = { |
This file contains hidden or 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 object year2020 { | |
type SeatId = Int | |
} | |
object Day05 extends SimpleCommonPuzzle[Seq[SeatId], SeatId, SeatId] { | |
private val binaryMap = Map( | |
'F' -> '0', | |
'B' -> '1', | |
'L' -> '0', |
This file contains hidden or 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
object PassportValidation { | |
val birthYear = "byr" | |
val issueYear = "iyr" | |
val expirationYear = "eyr" | |
val height = "hgt" | |
val hairColour = "hcl" | |
val eyeColour = "ecl" | |
val passportId = "pid" | |
val requiredFields = Set(birthYear, issueYear, expirationYear, height, hairColour, eyeColour, passportId) |
This file contains hidden or 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
case class StepSizes(right: Int, down: Int) | |
object Trees { | |
implicit class CharTreeOp(self: Char) { | |
def isTree = self == '#' | |
} | |
} | |
object Day03 extends SimpleCommonPuzzle[Matrix[Char], Long, Long] { |
This file contains hidden or 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
case class PasswordWithNumberCriteria(password: Password, criteria: PasswordCriteria) | |
case class PasswordCriteria(firstNum: Int, secondNum: Int, letter: Char) | |
case class Password(value: String) | |
object Day02 extends SimpleCommonPuzzle[Seq[PasswordWithNumberCriteria], Int, Int] { | |
override def parse(resource: String): Seq[PasswordWithNumberCriteria] = { | |
val regex = """(\d+)-(\d+)\s(\w):\s(\w+)""".r | |
readResource(resource).map { | |
case regex(firstNum, secondNum, letter, pass) => |
This file contains hidden or 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
def timeTaken(part: Int, f: => Int): Int = { | |
val start = System.nanoTime | |
val result = f | |
val duration = (System.nanoTime - start) / 1e9d | |
println(s"Part $part took $duration seconds") | |
result | |
} | |
override def part1(values: Seq[Int]): Int = | |
timeTaken(1, solver(values, 2)) |