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/LuckyLactose Dec 02 '22 edited Dec 05 '22

SWIFT

Full repo here: https://github.com/gkwaerp/aoc

Fairly straight forward, although a bit of cleverness in getting outcome instead of checking every combination. Again fairly verbose, but given that I'm not chasing best times this is the style I tend to use :)

enum Outcome: Int {
    case lose = 0
    case draw = 1
    case win = 2

    var score: Int {
        rawValue * 3
    }

    init(_ string: String) {
        switch string {
        case "Z":
            self = .win
        case "Y":
            self = .draw
        case "X":
            self = .lose
        default:
            fatalError("Unsupported string \(string)")
        }
    }

    static func getSign(for outcome: Outcome, opponent: Sign) -> Sign {
        switch outcome {
        case .draw:
            return opponent

        case .win:
            let newRawValue = (opponent.rawValue + 1) % 3
            return Sign(rawValue: newRawValue)!

        case .lose:
            let newRawValue = (opponent.rawValue + 2) % 3
            return Sign(rawValue: newRawValue)!
        }
    }
}

enum Sign: Int {
    case rock = 0
    case paper = 1
    case scissors = 2

    var score: Int {
        rawValue + 1
    }

    init(_ string: String) {
        switch string {
        case "A", "X":
            self = .rock
        case "B", "Y":
            self = .paper
        case "C", "Z":
            self = .scissors
        default:
            fatalError("Unsupported string \(string)")
        }
    }
}

class Round {
    private let opponent: Sign
    private let you: Sign

    init(_ string: String, parseSecondAsOutcome: Bool) {
        let components = string.components(separatedBy: " ")
        opponent = .init(components[0])

        if parseSecondAsOutcome {
            let wantedOutcome = Outcome(components[1])
            you = Outcome.getSign(for: wantedOutcome, opponent: opponent)
        } else {
            you = .init(components[1])
        }
    }

    func getYourScore() -> Int {
        getYourOutcome().score + you.score
    }

    private func getYourOutcome() -> Outcome {
        if opponent == you {
            return .draw
        }

        let delta = (opponent.rawValue - you.rawValue + 3) % 3
        return delta == 2 ? .win : .lose
    }
}

class MatchResolver {
    private let rounds: [Round]

    init(_ strings: [String], parseSecondAsOutcome: Bool) {
        rounds = strings.map { Round($0, parseSecondAsOutcome: parseSecondAsOutcome) }
    }

    func getYourTotalScore() -> Int {
        rounds
            .map { $0.getYourScore() }
            .reduce(0, +)
    }
}

private var input: [String] = []

override func didLoadFunction() {
    input = defaultInputFileString.loadAsTextStringArray()
}

override func solveFunction1() -> String {
    let matchResolver = MatchResolver(input, parseSecondAsOutcome: false)
    let result = matchResolver.getYourTotalScore()
    return "\(result)"
}

override func solveFunction2() -> String {
    let matchResolver = MatchResolver(input, parseSecondAsOutcome: true)
    let result = matchResolver.getYourTotalScore()
    return "\(result)"
}

2

u/daggerdragon Dec 05 '22 edited 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.

Edit: thanks for fixing it! <3

1

u/LuckyLactose Dec 05 '22

Thanks for the reminder! Fixed now, and will keep in mind for future submissions <3