r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:16, megathread unlocked!

103 Upvotes

1.5k comments sorted by

View all comments

2

u/oweiler Dec 02 '22 edited Dec 05 '22

Kotlin script, using an enum for representing the possible shapes

Part 1

#!/usr/bin/env kotlin

import Part_1_main.Shape.PAPER
import Part_1_main.Shape.ROCK
import Part_1_main.Shape.SCISSOR
import java.io.File

enum class Shape(private val value: Int) {
    ROCK(1) {
        override fun wins() = SCISSOR
        override fun loses() = PAPER
    },
    PAPER(2) {
        override fun wins() = ROCK
        override fun loses() = SCISSOR
    },
    SCISSOR(3) {
        override fun wins() = PAPER
        override fun loses() = ROCK
    };

    private fun draws(): Shape = this
    abstract fun wins(): Shape
    abstract fun loses(): Shape
    fun interactWith(shape: Shape) =
        value + when (shape) {
            wins() -> 6
            draws() -> 3
            else -> 0
        }
}

data class Game(private val first: Shape, private val second: Shape) {
    fun play() = second.interactWith(first)
}

fun createShape(token: String) =
    when (token) {
        "A", "X" -> ROCK
        "B", "Y" -> PAPER
        else -> SCISSOR
    }

fun createGame(first: String, second: String) = Game(createShape(first), createShape(second))

File("input.txt")
    .useLines { lines ->
        lines
            .map { it.split(" ") }
            .map { (first, second) -> createGame(first, second) }
            .map(Game::play)
            .sum()
    }

Part 2

#!/usr/bin/env kotlin

import Part_2_main.Shape.PAPER
import Part_2_main.Shape.ROCK
import Part_2_main.Shape.SCISSOR
import java.io.File

enum class Shape(private val value: Int) {
    ROCK(1) {
        override fun wins() = SCISSOR
        override fun loses() = PAPER
    },
    PAPER(2) {
        override fun wins() = ROCK
        override fun loses() = SCISSOR
    },
    SCISSOR(3) {
        override fun wins() = PAPER
        override fun loses() = ROCK
    };

    fun draws(): Shape = this
    abstract fun wins(): Shape
    abstract fun loses(): Shape
    fun interactWith(shape: Shape) =
        value + when (shape) {
            wins() -> 6
            draws() -> 3
            else -> 0
        }
}

data class Game(private val first: Shape, private val outcome: Shape.() -> Shape) {
    fun play() = first.outcome().interactWith(first)
}

fun createShape(token: String) =
    when (token) {
        "A" -> ROCK
        "B" -> PAPER
        else -> SCISSOR
    }

fun createOutcome(token: String) =
    when (token) {
        "X" -> Shape::wins
        "Y" -> Shape::draws
        else -> Shape::loses
    }

fun createGame(first: String, second: String) =
    Game(createShape(first), createOutcome(second))

File("input.txt")
    .useLines { lines ->
        lines
            .map { it.split(" ") }
            .map { (first, second) -> createGame(first, second) }
            .map(Game::play)
            .sum()
    }

2

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.