Skip to content

Instantly share code, notes, and snippets.

@rexim
Created March 25, 2016 10:34
Show Gist options
  • Save rexim/6bdbb912795c33838d5b to your computer and use it in GitHub Desktop.
Save rexim/6bdbb912795c33838d5b to your computer and use it in GitHub Desktop.
IO probe for GCJ
(require 'cl-lib)
(defun read-next-word ()
(let ((end (progn (forward-word)
(point)))
(start (progn (backward-word)
(point))))
(forward-word)
(buffer-substring start end)))
(defun read-next-number ()
(string-to-number (read-next-word)))
(defun find-two-purchases (c k ps)
(let ((result nil))
(cl-loop for i from 0 to (- k 2)
until result
do (cl-loop
for j from (1+ i) to (1- k)
until (= (+ (aref ps i) (aref ps j)) c)
finally do (setq result (cons i j))))
result))
(defun solve ()
(interactive)
(let ((input-buffer (find-file-noselect "input.txt"))
(output-buffer (find-file-noselect "output.txt")))
(with-current-buffer output-buffer
(erase-buffer))
(with-current-buffer input-buffer
(goto-char (point-min))
(let ((n (read-next-number)))
(dotimes (u n)
(let* ((c (read-next-number))
(k (read-next-number))
(ps (apply #'vector
(cl-loop
for i from 1 to k
collect (read-next-number))))
(result (find-two-purchases c k ps)))
(with-current-buffer output-buffer
(insert (format "Case #%d: %d %d\n"
(1+ u)
(1+ (car result))
(1+ (cdr result))))
(save-buffer))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment