Skip to content

Instantly share code, notes, and snippets.

View dmckennell's full-sized avatar
🐢
.

David McKennell dmckennell

🐢
.
  • Vienna, Austria
View GitHub Profile
@dmckennell
dmckennell / day01.clj
Last active December 2, 2021 12:29
Advent of Code 2021 - Day 01
(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)
@dmckennell
dmckennell / Day09.scala
Last active December 9, 2020 08:36
202009 Advent
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
@dmckennell
dmckennell / Day08.scala
Last active December 8, 2020 17:37
2020208 Advent
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 {
@dmckennell
dmckennell / Day06.scala
Last active December 6, 2020 11:37
202006 Advent
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]) = {
@dmckennell
dmckennell / Day05.scala
Last active December 5, 2020 17:14
202005 Advent
package object year2020 {
type SeatId = Int
}
object Day05 extends SimpleCommonPuzzle[Seq[SeatId], SeatId, SeatId] {
private val binaryMap = Map(
'F' -> '0',
'B' -> '1',
'L' -> '0',
@dmckennell
dmckennell / Day04.scala
Last active December 4, 2020 10:29
202004 Advent
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)
@dmckennell
dmckennell / Day03.scala
Last active December 3, 2020 12:15
202003 Advent
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] {
@dmckennell
dmckennell / Day02.scala
Created December 2, 2020 09:21
202002 Advent
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) =>
@dmckennell
dmckennell / Day01.scala
Created December 1, 2020 12:54
202001 Advent
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))