Hello there,
Unfortunately I did not make it to the second round, but in case anyone
was interested, this is my Lisp solution for Robot Programming Strategy.
(defun not-losing-moves (opponent)
(case opponent
(#\R (list #\R #\P))
(#\P (list #\P #\S))
(#\S (list #\S #\R))))
(defun winning-move (opponent)
(case opponent
(#\R #\P)
(#\P #\S)
(#\S #\R)))
(defun solve (programs)
(labels ((opponent-move (i program)
(aref program (mod i (length program))))
(recur (programs i moves)
(if (not programs)
(coerce (reverse moves) 'string)
(loop
:with possible-moves = (list #\R #\P #\S)
:for program :in programs
:for opponent = (opponent-move i program)
:do (setf possible-moves (intersection possible-moves
(not-losing-moves
opponent)))
:unless possible-moves :return "IMPOSSIBLE"
:finally (let* ((move (if (= (length possible-moves) 1)
(first possible-moves)
(winning-move (opponent-move i (first
programs)))))
(remaining (remove move programs :key
#'(lambda (program)
(winning-move (opponent-move i program))))))
(return (recur remaining (1+ i) (cons move
moves))))))))
(recur programs 0 NIL)))
(defun solution ()
(loop
:for i :from 1 :to (read)
:for n = (read)
:for programs = (loop
:for ii :below n
:collect (read-line))
:for solution = (solve programs)
:do (format t "Case #~d: ~a~%" i solution)
:do (sb-ext:gc))) ;; without this, the hidden set fails
(solution)
I am quite happy with it, and hopefully it should not be too hard for
you brave readers to follow along (especially after you read the
analysis); however, putting that `(sb-ext:gc)` in there at the end of
each iteration really feels like cheating, but the grader would not
accept my solution for the hidden set without it, so ... feedback is
welcome!
Ciao,
Matteo
PS. I really miss https://www.go-hero.net/!
--
Matteo Landi
https://matteolandi.net
--
You received this message because you are subscribed to the Google Groups "Google
Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to google-code+unsubscr...@googlegroups.com.
To post to this group, send email to google-code@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/20190504170036.GB1912%40hairstyle.local.
For more options, visit https://groups.google.com/d/optout.