Skip to content

Instantly share code, notes, and snippets.

View saidaspen's full-sized avatar

saidaspen

  • Stockholm, Sweden
View GitHub Profile
@saidaspen
saidaspen / AOC2022D23.kt
Last active December 23, 2022 06:57
Advent of Code 2022 Day 23
class World(val map: MutableMap<P<Int, Int>, Char>, var hasMoved: Boolean = false) {
private var considerDir = listOf(listOf(N, NE, NW), listOf(S, SE, SW), listOf(W, NW, SW), listOf(E, NE, SE))
private var plan = 0
val elvesCount : Int by lazy { map.entries.count { it.value == '#' }}
fun step() {
val elves = map.entries.filter { it.value == '#' }.map { it.key }.toList()
val planElves = elves.filter { neighbors(it).any { n -> map[n] == '#' } }
val plannedMoves = mutableMapOf<Point, Point>()
for (elf in planElves) {
@saidaspen
saidaspen / aoc22d22test.kt
Created December 22, 2022 11:00
Advent of Code 2022 Day 22 Test
package se.saidaspen.aoc.aoc2022
import junit.framework.TestCase.assertEquals
import org.junit.Test
import se.saidaspen.aoc.aoc2022.Day22.DIR.*
import se.saidaspen.aoc.util.P
internal class Day22Test {
@Test
@saidaspen
saidaspen / aoc2022D22.kt
Created December 22, 2022 10:53
Advent of Code 2022 Day 22
object Day22 : Day(2022, 22) {
enum class DIR(val p: Point) { RIGHT(P(1, 0)), DOWN(P(0, 1)), LEFT(P(-1, 0)), UP(P(0, -1));
fun right() = values()[(this.ordinal + 1).mod(values().size)]
fun left() = values()[(this.ordinal - 1).mod(values().size)]
}
enum class SIDE { A, B, C, D, E, F }
private val map = toMap(input.split("\n\n")[0]).entries.filter { it.value != ' ' }.associate { it.key to it.value }
import se.saidaspen.aoc.util.Day
import se.saidaspen.aoc.util.words
import kotlin.math.absoluteValue
import kotlin.math.max
fun main() = Dayxx.run()
object Dayxx : Day(2022, 21) {
var input2 = "root: pppw + sjmn\n" +
@saidaspen
saidaspen / aoc22_d20.kt
Last active December 20, 2022 18:02
Advent of Code 2022 Day 20 Kotlin
object Day20 : Day(2022, 20) {
override fun part1() = solve(1, 1)
override fun part2() = solve(811589153, 10)
private fun solve(key: Long, times: Int): Long {
// Changes 1, 2, 3 to (0, 1), (1, 2), (2, 3) to keep the original positions while moving things around
val wPos = longs(input).withIndex().map { P(it.index, it.value * key) }.toMutableList()
repeat(times) {
for (i in 0 until wPos.size) {
// Idx is the current index, elem.first is the original index, elem.second is the value
@saidaspen
saidaspen / 2022d19.kt
Last active December 20, 2022 18:04
Advent of Code 2022 Day 19
package se.saidaspen.aoc.aoc2022
import se.saidaspen.aoc.util.*
import java.lang.Integer.max
import kotlin.math.min
fun main() = Day19.run()
object Day19 : Day(2022, 19) {
@saidaspen
saidaspen / aoc2022d17.kt
Created December 17, 2022 07:37
Dag 17 ostädad :)
import se.saidaspen.aoc.util.*
import java.math.BigInteger
import kotlin.math.absoluteValue
fun main() = Dayxx.run()
object Dayxx : Day(2022, 17) {
var dash = toMap("####").map { it.key }.toSet()
var plus = toMap(".#.\n" +
@saidaspen
saidaspen / aoc2216.kt
Created December 16, 2022 11:01
AOC 2022 Day 16
import se.saidaspen.aoc.util.*
fun main() = Dayxx.run()
object Dayxx : Day(2022, 16) {
private val valves = input.lines().associate { it.split(" ")[1] to ints(it)[0] }
private val relevantValves = valves.entries.filter { it.value > 0 }.map { it.key to it.value }.toMap()
private val connections = input.lines().map { it.split(" ") }.associate {
it[1] to it.drop(9).map { c -> c.replace(",", "").trim() }.toList()
@saidaspen
saidaspen / aoc22-14.kt
Created December 14, 2022 06:00
Advent of Code 2022 14
import se.saidaspen.aoc.util.*
import kotlin.math.sign
fun main() = Dayxx.run()
object Dayxx : Day(2022, 14) {
override fun part1() : Any {
// input = "498,4 -> 498,6 -> 496,6\n" +
@saidaspen
saidaspen / AOC2213.kt
Created December 13, 2022 08:41
Advent of Code 2022 13
import se.saidaspen.aoc.util.Day
import se.saidaspen.aoc.util.P
fun main() = Day13.run()
object Day13 : Day(2022, 13) {
class Packet (private val fixed : Int? = null, val values : MutableList<Packet> = mutableListOf()) : Comparable<Packet> {
override fun toString() = fixed?.toString() ?: ("[" + values.joinToString(",") + "]")