Skip to content

Commit

Permalink
Prepare for 2022
Browse files Browse the repository at this point in the history
  • Loading branch information
mbali committed Dec 1, 2022
1 parent e5bac48 commit a3fd5d2
Show file tree
Hide file tree
Showing 30 changed files with 218 additions and 80 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
plugins {
kotlin("jvm") version "1.6.0"
kotlin("jvm") version "1.7.22"
}

repositories {
mavenCentral()
}

dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-RC3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
}
tasks {
sourceSets {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = "advent-of-code-2021"
rootProject.name = "advent-of-code"
2 changes: 1 addition & 1 deletion src/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlin.time.measureTime
/**
* Reads lines from the given input txt file.
*/
fun readInput(name: String) = File("src", "$name.txt").readLines()
fun readInput(year: Int, name: String) = File("src/year$year", "$name.txt").readLines()

/**
* Converts string to md5 hash.
Expand Down
9 changes: 7 additions & 2 deletions src/Day01.kt → src/year2021/Day01.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package year2021

import benchmark
import readInput

fun main() {

fun toInts(input: List<String>) = input.map { it.toInt() }
Expand All @@ -16,11 +21,11 @@ fun main() {
}

// test if implementation meets criteria from the description, like:
val testInput = readInput("Day01_test")
val testInput = readInput(2021, "Day01_test")
check(part1(testInput) == 7)
check(part2(testInput) == 5)

val input = readInput("Day01")
val input = readInput(2021, "Day01")
println(part1(input))
println(part2(input))

Expand Down
9 changes: 7 additions & 2 deletions src/Day02.kt → src/year2021/Day02.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package year2021

import benchmark
import readInput

fun main() {

fun part1(input: List<String>): Int {
Expand Down Expand Up @@ -31,11 +36,11 @@ fun main() {
return horizontal * vertical
}

val testInput = readInput("Day02_test")
val testInput = readInput(2021, "Day02_test")
check(part1(testInput) == 150)
check(part2(testInput) == 900)

val input = readInput("Day02")
val input = readInput(2021, "Day02")
println(part1(input))
println(part2(input))

Expand Down
9 changes: 7 additions & 2 deletions src/Day03.kt → src/year2021/Day03.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package year2021

import benchmark
import readInput

fun main() {

fun toMeasurements(input: List<String>) = input
Expand Down Expand Up @@ -60,11 +65,11 @@ fun main() {
return o2GeneratorRating * co2ScrubberRating
}

val testInput = readInput("Day03_test")
val testInput = readInput(2021, "Day03_test")
check(part1(testInput) == 198)
check(part2(testInput) == 230)

val input = readInput("Day03")
val input = readInput(2021, "Day03")
println(part1(input))
println(part2(input))

Expand Down
10 changes: 8 additions & 2 deletions src/Day04.kt → src/year2021/Day04.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
package year2021

import SHOULD_NOT_REACH
import benchmark
import readInput

private class BingoBoard(val numbers: Array<Int>) {
init {
check(numbers.size == 25)
Expand Down Expand Up @@ -59,10 +65,10 @@ fun main() {
SHOULD_NOT_REACH()
}

val testInput = readInput("Day04_test")
val testInput = readInput(2021, "Day04_test")
check(part1(testInput) == 4512)

val input = readInput("Day04")
val input = readInput(2021, "Day04")
println(part1(input))

check(part2(testInput) == 1924)
Expand Down
10 changes: 7 additions & 3 deletions src/Day05.kt → src/year2021/Day05.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package year2021

import benchmark
import readInput
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.sign
Expand Down Expand Up @@ -33,7 +37,7 @@ private class Line(val start: Position, val end: Position) {
}
}

private fun List<String>.solution(ordinalDirectionsOnly: Boolean): Int = map(Line::from)
private fun List<String>.solution(ordinalDirectionsOnly: Boolean): Int = map(Line.Companion::from)
.flatMap { it.positions(ordinalDirectionsOnly) }
.groupingBy { it }
.eachCount()
Expand All @@ -49,10 +53,10 @@ fun main() {
return input.solution(false)
}

val testInput = readInput("Day05_test")
val testInput = readInput(2021, "Day05_test")
check(part1(testInput) == 5)

val input = readInput("Day05")
val input = readInput(2021, "Day05")
println(part1(input))

check(part2(testInput) == 12)
Expand Down
9 changes: 7 additions & 2 deletions src/Day06.kt → src/year2021/Day06.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package year2021

import benchmark
import readInput

fun main() {
fun tick(input: LongArray): LongArray {
check(input.size == 9) { "Input size should be 9" }
Expand Down Expand Up @@ -27,10 +32,10 @@ fun main() {
return solution(input, 256)
}

val testInput = readInput("Day06_test")
val testInput = readInput(2021, "Day06_test")
check(part1(testInput) == 5_934L)

val input = readInput("Day06")
val input = readInput(2021, "Day06")
println(part1(input))

check(part2(testInput) == 26_984_457_539L)
Expand Down
9 changes: 7 additions & 2 deletions src/Day07.kt → src/year2021/Day07.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package year2021

import benchmark
import middle
import readInput
import kotlin.math.absoluteValue

fun main() {
Expand Down Expand Up @@ -36,10 +41,10 @@ fun main() {
return solution(positions, positions.average().toInt()) { it * (it + 1) / 2 }
}

val testInput = readInput("Day07_test")
val testInput = readInput(2021, "Day07_test")
check(part1(testInput) == 37)

val input = readInput("Day07")
val input = readInput(2021, "Day07")
println(part1(input))

check(part2(testInput) == 168)
Expand Down
9 changes: 7 additions & 2 deletions src/Day08.kt → src/year2021/Day08.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package year2021

import benchmark
import readInput

private data class PuzzleEntry(val patterns: List<String>, val output: List<String>)

private fun String.toBits() =
Expand Down Expand Up @@ -86,10 +91,10 @@ fun main() {
.map { it to Decoder(it.patterns) }
.sumOf { (entry, decoder) -> decoder.decode(entry.output) }

val testInput = readInput("Day08_test")
val testInput = readInput(2021, "Day08_test")
check(part1(testInput) == 26)

val input = readInput("Day08")
val input = readInput(2021, "Day08")
println(part1(input))

check(part2(testInput) == 61229)
Expand Down
9 changes: 7 additions & 2 deletions src/Day09.kt → src/year2021/Day09.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package year2021

import benchmark
import readInput

private typealias HeightMap = Array<Array<Int>>


Expand Down Expand Up @@ -67,10 +72,10 @@ fun main() {
.take(3).reduce { a, b -> a * b }
}

val testInput = readInput("Day09_test")
val testInput = readInput(2021, "Day09_test")
check(part1(testInput) == 15)

val input = readInput("Day09")
val input = readInput(2021, "Day09")
println(part1(input))

check(part2(testInput) == 1134)
Expand Down
9 changes: 7 additions & 2 deletions src/Day10.kt → src/year2021/Day10.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package year2021

import benchmark
import readInput

fun main() {

class Parser constructor(val input: String) {
Expand Down Expand Up @@ -66,10 +71,10 @@ fun main() {
return scores[scores.size / 2]
}

val testInput = readInput("Day10_test")
val testInput = readInput(2021, "Day10_test")
check(part1(testInput) == 26397)

val input = readInput("Day10")
val input = readInput(2021, "Day10")
println(part1(input))

check(part2(testInput) == 288957L)
Expand Down
9 changes: 7 additions & 2 deletions src/Day11.kt → src/year2021/Day11.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package year2021

import benchmark
import readInput

fun main() {

fun step(energyLevels: IntArray): IntArray {
Expand Down Expand Up @@ -51,10 +56,10 @@ fun main() {
.count() //with the initial state, the count is the expected value
}

val testInput = readInput("Day11_test")
val testInput = readInput(2021, "Day11_test")
check(part1(testInput) == 1656)

val input = readInput("Day11")
val input = readInput(2021, "Day11")
println(part1(input))

check(part2(testInput) == 195)
Expand Down
13 changes: 9 additions & 4 deletions src/Day12.kt → src/year2021/Day12.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package year2021

import benchmark
import readInput

fun main() {

fun isSmall(id: String) = id.all { it.isLowerCase() }
Expand Down Expand Up @@ -47,14 +52,14 @@ fun main() {
}.size
}

val testInput1 = readInput("Day12_test1")
val testInput2 = readInput("Day12_test2")
val testInput3 = readInput("Day12_test3")
val testInput1 = readInput(2021, "Day12_test1")
val testInput2 = readInput(2021, "Day12_test2")
val testInput3 = readInput(2021, "Day12_test3")
check(part1(testInput1) == 10)
check(part1(testInput2) == 19)
check(part1(testInput3) == 226)

val input = readInput("Day12")
val input = readInput(2021, "Day12")
println(part1(input))

check(part2(testInput1) == 36)
Expand Down
9 changes: 7 additions & 2 deletions src/Day13.kt → src/year2021/Day13.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package year2021

import benchmark
import readInput

fun main() {
class FoldInstruction(val x: Int? = null, val y: Int? = null) {
fun transform(positions: Set<Pair<Int, Int>>): Set<Pair<Int, Int>> =
Expand Down Expand Up @@ -74,10 +79,10 @@ fun main() {
return result.count()
}

val testInput = readInput("Day13_test")
val testInput = readInput(2021, "Day13_test")
check(part1(testInput) == 17)

val input = readInput("Day13")
val input = readInput(2021, "Day13")
println(part1(input))

check(part2("TEST", testInput) == 16)
Expand Down
9 changes: 7 additions & 2 deletions src/Day14.kt → src/year2021/Day14.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package year2021

import benchmark
import readInput

fun main() {

class Polymer(val start: Char, val end: Char, val pairCounts: Map<String, Long>) {
Expand Down Expand Up @@ -59,10 +64,10 @@ fun main() {
return solution(input, 40)
}

val testInput = readInput("Day14_test")
val testInput = readInput(2021, "Day14_test")
check(part1(testInput) == 1588L)

val input = readInput("Day14")
val input = readInput(2021, "Day14")
println(part1(input))
check(part2(testInput) == 2188189693529L)
println(part2(input))
Expand Down
10 changes: 8 additions & 2 deletions src/Day15.kt → src/year2021/Day15.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
package year2021

import SHOULD_NOT_REACH
import benchmark
import crossJoin
import readInput
import java.util.*

fun main() {
Expand Down Expand Up @@ -66,10 +72,10 @@ fun main() {
return solution(input, 5)
}

val testInput = readInput("Day15_test")
val testInput = readInput(2021, "Day15_test")
check(part1(testInput) == 40)

val input = readInput("Day15")
val input = readInput(2021, "Day15")
println(part1(input))

check(part2(testInput) == 315)
Expand Down
Loading

0 comments on commit a3fd5d2

Please sign in to comment.