object Main extends App {

  case class State[A, S](run: S => (S, A)) {
    def flatMap[B](f: A => State[B, S]): State[B, S] = State((s: S) => {
      val (s1, a) = run(s)
      f(a).run(s1)
    })
//    def map[B](f: A => B) = State((s: S) => {
//      val (s1, a) = run(s)
//      (s1, f(a))
//    })
    def map[B](f: A => B): State[B, S] = {
      //      State((s: S) => (f(self.run(s)._1, f(self.run(s)._2)))
      flatMap(a => State.unit(f(a)))
    }
  }
  object State {
    def unit[A, S](a: A) = State((s: S) => (s, a))
  }

  implicit class StreamOps[A, S](s: Stream[State[A, S]]) {
    def sequence: State[Stream[A], S] =
      s.foldRight(State.unit[Stream[A], S](Stream.empty[A]))((h, acc) => h.flatMap(a => acc.map(st => a #:: st)))
  }

  case class Position(x: Double, y: Double, angle: Int) {
    def toInt = (x.toInt, y.toInt)
  }

  def exec(distance: Int, angle: Int): State[Unit, Position] = State((p: Position) => {
    (Position(
      p.x + distance * math.sin(math.Pi / 180 * p.angle),
      p.y + distance * math.cos(math.Pi / 180 * p.angle),
      angle + p.angle), Unit)
  })

  val ans = Stream.continually(readLine)
  .takeWhile(_ != "0,0")
  .map(_.split(",").map(_.toInt))
  .map { case Array(d, a) => exec(d, a) }
    .sequence
    .run(Position(0, 0, 0))
    ._1
    .toInt
  println(ans._1)
  println(ans._2)
}

