On 04/10, mukesh tiwari wrote:
Feel free to share your code. I don't write code in Lisp, but I use
All right!
# Problem 1
Iterate over the number digits, and:
- when 4, keep 2 on the first, and move 2 on the second
- othewise, keep the digit on the first, and put a 0 on the second
For example:
1234 => 1232 2
4321 => 2321 2000
4444 => 2222 2222
(defun solve (string)
(loop
:with a = 0 :and b = 0
:for c :across string
:for d = (- (char-code c) (char-code #\0))
:do (if (= d 4)
(setf a (+ (* a 10) 2)
b (+ (* b 10) 2))
(setf a (+ (* a 10) d)
b (+ (* b 10) 0)))
:finally (return (list a b))))
(defun solution ()
(loop
:for i :from 1 :to (parse-integer (read-line))
:for n = (read-line)
:for (a b) = (solve n)
:do (format t "Case #~d: ~d ~d~%" i a b)))
# Problem 2
Replace Ss with Es and vice-versa
(defun solve (lydia)
(map 'string (lambda (m) (if (char= m #\S) #\E #\S)) lydia))
(defun solution ()
(loop
:for i :from 1 :to (parse-integer (read-line))
:for ignored = (read-line)
:for lydia = (read-line)
:for mine = (solve lydia)
:do (format t "Case #~d: ~a~%" i mine)))
# Problem 3
That's a little more complicated than the others, but after you read the
analysis it should not be to difficult to follow along.
(defun read-ciphertext (length &aux (ciphertext (make-array length)))
(dotimes (i length ciphertext)
(setf (aref ciphertext i) (read))))
(defun split-sequence (string &optional (delimiter #\Space))
(loop
:for i = 0 :then (1+ j)
:for j = (position delimiter string :start i)
:collect (subseq string i j) :into parts
:while j
:finally (return (remove "" parts :test 'equal))))
(defun position-first-difference (ciphertext)
(loop
:for i = 0 :then (1+ i)
:for j = (1+ i)
:unless (= (aref ciphertext i) (aref ciphertext j)) :return i))
(defun divisors (ciphertext)
(let* ((divisors (make-array (1+ (length ciphertext))))
(pos (position-first-difference ciphertext)))
(setf (aref divisors (1+ pos)) (gcd (aref ciphertext pos) (aref ciphertext
(1+ pos))))
(loop
:for i :from pos :downto 0
:do (setf (aref divisors i) (/ (aref ciphertext i) (aref divisors (1+
i)))))
(loop
:for i :from (1+ pos) :below (length ciphertext)
:do (setf (aref divisors (1+ i)) (/ (aref ciphertext i) (aref divisors
i))))
divisors))
(defun decypher-table (divisors &aux (table (make-hash-table)))
(loop
:for p :across (sort (remove-duplicates divisors) #'<)
:for i = 0 :then (1+ i)
:do (setf (gethash p table) (code-char (+ (char-code #\A) i))))
table)
(defun solve (ciphertext)
(loop
:with divisors = (divisors ciphertext)
:with table = (decypher-table divisors)
:for d :across divisors
:collect (gethash d table) :into plaintext
:finally (return (coerce plaintext 'string))))
(defun solution ()
(loop
:for i :from 1 :to (parse-integer (read-line))
:for ignored = (read)
:for size = (read)
:for ciphertext = (read-ciphertext size)
:for plaintext = (solve (subseq ciphertext 0 size))
:do (format t "Case #~d: ~a~%" i plaintext)))
(solution)
Feedback is welcome!
Matteo
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/20190412195513.GD2837%40hairstyle.local.
For more options, visit https://groups.google.com/d/optout.