Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- class Day13 {
- static let scorePoint = Point(x: -1, y: 0)
- let program: String
- var worldState: [Point: Int] = [:]
- var paddle: Point = Point(x: 0, y: 0)
- var ball: Point = Point(x: 0, y: 0)
- var score = 0
- public init(inputText: [String]) {
- // program = inputText[0]
- // free play
- program = "2" + inputText[0].dropFirst()
- }
- public func solve() {
- let computer = IntCode()
- computer.load(program: program)
- while computer.executeNext() != false {
- if ball.x > paddle.x {
- computer.inputQueue = [1]
- } else if ball.x < paddle.x {
- computer.inputQueue = [-1]
- } else {
- computer.inputQueue = [0]
- }
- // Figure out input
- // computer.inputQueue = [-1, 0, 1]
- // if computer.inputQueue.isEmpty {
- // computer.inputQueue.append(0)
- // }
- if computer.output.count == 3 {
- let out = computer.output
- computer.output = []
- let point = Point(x: out[0], y: out[1])
- if point == Day13.scorePoint {
- score = out[2]
- print("score: \(score)")
- } else {
- let type = out[2]
- worldState[point] = type
- }
- if out[2] == 4 {
- ball = point
- print("QQ ball: \(ball)")
- }
- if out[2] == 3 {
- paddle = point
- print("QQ paddle: \(paddle)")
- }
- }
- }
- let blocks = worldState.values.reduce(0) { ret, val in
- if val == 2 {
- return ret + 1
- }
- return ret
- }
- print(blocks)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement