DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 13: Care Package

Collapse
 
rizzu26 profile image
Rizwan

No change in OpCode computer and it runs smoothly now.

Swift solution here

enum Joystick: Int {
    case left   = -1
    case right  = 1
    case neutral = 0
}

enum TileId: Int, CustomStringConvertible {
    var description: String {
        get {
            desc()
        }
    }

    func desc() -> String {
        switch self {
        case .empty:
            return "Empty"
        case .wall:
            return "Wall"
        case .block:
            return "Block"
        case .hPaddle:
            return "H Paddle"
        case .ball:
            return "Ball"
        }
    }

    case empty = 0
    case wall = 1
    case block = 2
    case hPaddle = 3
    case ball = 4

}

struct Point: CustomStringConvertible, Equatable, Hashable {
    var x: Int
    var y: Int

    var description: String {
        get {
            return "X: \(self.x) Y: \(self.y)"
        }
    }
}

class Arcade {
    var grid: [Point: TileId] = [:]
    var memory: [Int]

    init(_ memory: [Int]) {
        self.memory = memory
    }

    func compute() {
        let computer = Opcode(memory,0)

        while !computer.done {
            let x = computer.run()
            let y = computer.run()
            let id = TileId(rawValue: computer.run())

            grid[Point.init(x: x, y: y)] = id
        }
    }

    func getNumberOfTiles(for tile: TileId) -> Int {
        return grid.values.filter { $0 == tile }.count
    }

    func setQuarters(_ value: Int) {
        memory[0] = value
    }

    func beat() -> Int {
        let computer = Opcode(memory,0)
        var score = 0
        var ball = Point.init(x: 0, y: 0)
        var paddle = Point.init(x: 0, y: 0)

        while !computer.done {
            let x = computer.run()
            let y = computer.run()
            let value = computer.run()

            if x == -1 && y == 0 {
                score = value
            }
            else {
                let pos = Point.init(x: x, y: y)
                if value == 4 {
                    ball = pos
                }
                if value == 3 {
                    paddle = pos
                }
            }

            if paddle.x < ball.x {
                computer.inputIds.append(Joystick.right.rawValue)
            }
            else if paddle.x > ball.x {
                computer.inputIds.append(Joystick.left.rawValue)
            }
            else {
                computer.inputIds.append(Joystick.neutral.rawValue)
            }
        }
        return score
    }
}

func partOne() {
    let arcade = Arcade(input)
    arcade.compute()
    print(arcade.grid)
    print("Part 1 answer is :\(arcade.getNumberOfTiles(for: .block))")
}

func partTwo() {
    let arcade = Arcade(input)
    arcade.setQuarters(2)
    print("Part 2 answer is :\(arcade.beat())")
}

partOne()
partTwo()

Opcode can be found here in GitHub -> github.com/rizwankce/AdventOfCode/...