aboutsummaryrefslogtreecommitdiff
path: root/2022/Day04/part-two.cl
blob: a593167c2c4ff297eb5807ad8812c20910d7a116 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(defvar *prog-input*)

;;; After running file, if you enter a blank line, code will work on each
;;; line of input, until "end" is typed.
;;; Otherwise, the input is taken as a filename and code will be executed
;;; over each line in the file until EOF.
(let ((ui (read-line)))
  (if (equal ui "")
    (setq *prog-input* *standard-input*)
    (setq *prog-input* (open ui))))

(defun overlapping (a b)
  (or (and (<= (car a) (car b)) (<= (car b) (cdr a)))   ; The only change from part-one is "(<= (car b) ...)", instead of "(<= (cdr b) ...)"
      (and (<= (car b) (car a)) (<= (car a) (cdr b))))) ; The only change from part-one is "(<= (car a) ...)", instead of "(<= (cdr a) ...)"

(let
  ((inpt (read-line *prog-input* NIL)) (first-range '()) (second-range '()) (comma 0) (sum 0))

  (loop until (or (string= inpt "end") (not inpt)) do
        (setq comma (+ 1 (position #\, inpt))
              first-range  (cons (parse-integer inpt :junk-allowed T :start 0)
                                 (parse-integer inpt :junk-allowed T :start (+ 1 (position #\- inpt))))
              second-range (cons (parse-integer inpt :junk-allowed T :start comma)
                                 (parse-integer inpt :junk-allowed T :start (+ 1 (position #\- inpt :start comma)))))

        (if (overlapping first-range second-range)
          (setq sum (+ 1 sum)))

        (setq inpt (read-line *prog-input* NIL)))

  (print sum))

(if (not (eq *prog-input* *standard-input*))
  (close *prog-input*))