diff options
| author | Syndamia <kamen@syndamia.com> | 2022-12-04 21:47:50 +0200 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2022-12-04 21:47:50 +0200 |
| commit | 94ba216600fb2e90a89828b6821b30ae60538be2 (patch) | |
| tree | 09327aa38ebaf7ef213aae6e64d0fe66ed730a42 /2022/Day04/part-two.cl | |
| parent | 3e79cce47623855ab2025c29154e828e2007adbf (diff) | |
| download | advent-of-code-94ba216600fb2e90a89828b6821b30ae60538be2.tar advent-of-code-94ba216600fb2e90a89828b6821b30ae60538be2.tar.gz advent-of-code-94ba216600fb2e90a89828b6821b30ae60538be2.zip | |
[2022/D04] Solved both tasks from today
Diffstat (limited to '2022/Day04/part-two.cl')
| -rw-r--r-- | 2022/Day04/part-two.cl | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/2022/Day04/part-two.cl b/2022/Day04/part-two.cl new file mode 100644 index 0000000..aef744e --- /dev/null +++ b/2022/Day04/part-two.cl @@ -0,0 +1,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 first-range (cons (parse-integer inpt :junk-allowed T :start 0) + (parse-integer inpt :junk-allowed T :start (+ 1 (position #\- inpt))))) + (setq comma (+ 1 (position #\, inpt))) + (setq 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*)) + |
