-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Practice some Kotlin by doing 2018 problems
- Loading branch information
1 parent
2b714cd
commit b0ed1a7
Showing
34 changed files
with
4,368 additions
and
19 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package net.fish.collections | ||
|
||
fun IntRange.fullyContains(t: IntRange): Boolean = this.first <= t.first && this.last >= t.last | ||
|
||
fun IntRange.overlaps(t: IntRange): Boolean { | ||
return (this.first in t.first..t.last) || (this.last in t.first..t.last) || | ||
(t.first in this.first..this.last) || (t.last in this.first..this.last) | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@file:JvmName("AdventOfCode2023") | ||
package net.fish.y2018 | ||
|
||
import net.fish.Reflect | ||
import net.fish.Runner | ||
|
||
object AdventOfCode { | ||
val days = Reflect.getDays(2018) | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
val day = if(args.isEmpty()) 0 else args[0].toInt() | ||
Runner.run(2018, AdventOfCode.days, day) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package net.fish.y2018 | ||
|
||
import net.fish.Day | ||
import net.fish.resourceLines | ||
|
||
object Day01 : Day { | ||
private val data by lazy { resourceLines(2018, 1).map { it.toInt() } } | ||
private val previousTotals: MutableSet<Int> = mutableSetOf() | ||
|
||
override fun part1() = doPart1(data) | ||
override fun part2() = doPart2(data) | ||
|
||
fun doPart1(data: List<Int>): Int = data.sum() | ||
fun doPart2(data: List<Int>): Int { | ||
var currentIndex = 0 | ||
var foundDuplicate = false | ||
var currentTotal = 0 | ||
previousTotals += 0 | ||
// find when the total repeats itself by checking when we've seen it previously in the set, or adding to it and repeating | ||
while (!foundDuplicate) { | ||
currentTotal += data[currentIndex] | ||
if (previousTotals.contains(currentTotal)) { | ||
foundDuplicate = true | ||
} else { | ||
previousTotals += currentTotal | ||
currentIndex = (currentIndex + 1) % data.size | ||
} | ||
} | ||
return currentTotal | ||
} | ||
|
||
@JvmStatic | ||
fun main(args: Array<String>) { | ||
println(part1()) | ||
println(part2()) | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package net.fish.y2018 | ||
|
||
import net.fish.Day | ||
import net.fish.maths.combinations | ||
import net.fish.maths.permutations | ||
import net.fish.resourceLines | ||
|
||
object Day02 : Day { | ||
private val data by lazy { toBoxes(resourceLines(2018, 2)) } | ||
|
||
override fun part1() = doPart1(data) | ||
override fun part2() = doPart2(data) | ||
|
||
fun hasDuplicateOfCount(code: String, count: Int): Boolean { | ||
val byCharCount = code.toCharArray().toList().groupBy { it } | ||
// do any of the groups have the right size? | ||
return byCharCount.any { it.value.size == count } | ||
} | ||
|
||
data class Box(val code: String) { | ||
val size = code.length | ||
val hasPairs: Boolean = hasDuplicateOfCount(code, 2) | ||
val hasTriples: Boolean = hasDuplicateOfCount(code, 3) | ||
|
||
fun numDiffs(other: Box): Int = size - code.zip(other.code).count { it.first == it.second } | ||
fun removeDiffs(other: Box): String { | ||
return code.zip(other.code).filter { it.first == it.second }.map { it.first }.joinToString("") | ||
} | ||
} | ||
|
||
private fun toBoxes(data: List<String>): List<Box> { | ||
return data.map { Box(it) } | ||
} | ||
|
||
fun doPart1(data: List<Box>): Int = data.count { it.hasPairs } * data.count { it.hasTriples } | ||
fun doPart2(data: List<Box>): String { | ||
val x = data.combinations(2) | ||
val pairWith1Diff = x.first { it[0].numDiffs(it[1]) == 1 } | ||
return pairWith1Diff[0].removeDiffs(pairWith1Diff[1]) | ||
} | ||
|
||
@JvmStatic | ||
fun main(args: Array<String>) { | ||
println(part1()) | ||
println(part2()) | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package net.fish.y2018 | ||
|
||
import net.fish.Day | ||
import net.fish.geometry.Point | ||
import net.fish.resourceLines | ||
import java.lang.Exception | ||
|
||
object Day03 : Day { | ||
private val claimExtractor by lazy { Regex("""#(\d+) @ (\d+),(\d+): (\d+)x(\d+)""") } | ||
private val data by lazy { toClaims(resourceLines(2018, 3)) } | ||
|
||
override fun part1() = doPart1(data) | ||
override fun part2() = doPart2(data) | ||
|
||
data class Claim(val id:Int, val x: Int, val y: Int, val sx: Int, val sy: Int) { | ||
fun allPoints(): List<Point> { | ||
val combinations: MutableList<Point> = mutableListOf() | ||
for (a in (x until (x+sx))) { | ||
for (b in (y until (y+sy))) { | ||
combinations.add(Point(a, b)) | ||
} | ||
} | ||
return combinations.toList() | ||
} | ||
} | ||
|
||
fun toClaims(data: List<String>): List<Claim> = data.map { line -> | ||
claimExtractor.find(line)?.destructured!!.let { (id, x, y, sx, sy) -> | ||
// println("Adding claim for $id, $x, $y, $sx, $sy") | ||
Claim(id.toInt(), x.toInt(), y.toInt(), sx.toInt(), sy.toInt()) | ||
} | ||
} | ||
|
||
fun doPart1(claims: List<Claim>): Int = createPointToClaims(claims).values.count { it.size > 1 } | ||
|
||
private fun createPointToClaims(claims: List<Claim>): MutableMap<Point, MutableSet<Claim>> { | ||
val mapPointToClaims: MutableMap<Point, MutableSet<Claim>> = mutableMapOf() | ||
claims.forEach { claim -> | ||
claim.allPoints().forEach { p -> | ||
mapPointToClaims.getOrPut(p) { mutableSetOf() } += claim | ||
} | ||
} | ||
return mapPointToClaims | ||
} | ||
|
||
fun doPart2(claims: List<Claim>): Int { | ||
// find all claims that have a point in more than one claim, and remove them from all claims. should be left with 1 claim that | ||
// doesn't overlap anything. | ||
val p2c = createPointToClaims(claims) | ||
val claimsThatOverlap = p2c.values.filter { it.size > 1 }.flatten().toSet() | ||
val unique = claims - claimsThatOverlap | ||
if (unique.size != 1) { | ||
throw Exception("Found more than 1 that doesn't overlap: $unique") | ||
} | ||
return unique.first().id | ||
} | ||
|
||
@JvmStatic | ||
fun main(args: Array<String>) { | ||
println(part1()) | ||
println(part2()) | ||
} | ||
|
||
} |
Oops, something went wrong.