day 4 part 1 in elisp
This commit is contained in:
parent
d059140b37
commit
a4145962b8
79
elisp/day-4/solution.el
Executable file
79
elisp/day-4/solution.el
Executable file
|
@ -0,0 +1,79 @@
|
|||
#!/usr/bin/env -S emacs --script
|
||||
|
||||
;; -*- lexical-binding: t; -*-
|
||||
|
||||
;; Copyright (C) 2022 notroot
|
||||
|
||||
;; This program is free software: you can redistribute it and/or modify
|
||||
;; it under the terms of the GNU General Public License as published by
|
||||
;; the Free Software Foundation, either version 3 of the License, or
|
||||
;; (at your option) any later version.
|
||||
|
||||
;; This program is distributed in the hope that it will be useful,
|
||||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
;; GNU General Public License for more details.
|
||||
|
||||
;; You should have received a copy of the GNU General Public License
|
||||
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
(add-to-list 'load-path "../")
|
||||
|
||||
(require 'ert)
|
||||
(require 'cl-lib)
|
||||
(require 'advent-of-code)
|
||||
|
||||
(defvar solution-input
|
||||
"../../input/day-4.txt")
|
||||
|
||||
(defvar solution-example-input
|
||||
"../../input/day-4-example.txt")
|
||||
|
||||
(defun solution-parse-pair (s)
|
||||
(let ((parts (string-split s "-")))
|
||||
(cons (string-to-number (car parts))
|
||||
(string-to-number (cadr parts)))))
|
||||
|
||||
(defun solution-parse-pairs (line)
|
||||
(let ((unparsed-pairs (string-split line "," t)))
|
||||
(list (solution-parse-pair (car unparsed-pairs))
|
||||
(solution-parse-pair (cadr unparsed-pairs)))))
|
||||
|
||||
(defun solution-pair-contains? (a b)
|
||||
(and (<= (car a) (car b))
|
||||
(>= (cdr a) (cdr b))))
|
||||
|
||||
(defun solution-pair-overlaps? (a b)
|
||||
(or (and (>= (car a) (car b))
|
||||
(<= (car a) (cdr b)))
|
||||
(and (>= (cdr a) (car b))
|
||||
(<= (cdr a) (cdr b)))))
|
||||
|
||||
(defun solution-part-1 (input)
|
||||
(let ((pairs (seq-map 'solution-parse-pairs input)))
|
||||
(seq-count (lambda (pair)
|
||||
(or (solution-pair-contains? (car pair) (cadr pair))
|
||||
(solution-pair-contains? (cadr pair) (car pair))))
|
||||
pairs)))
|
||||
|
||||
(defun solution-part-2 (input)
|
||||
(let ((pairs (seq-map 'solution-parse-pairs input)))
|
||||
(seq-count (lambda (pair)
|
||||
(or (solution-pair-overlaps? (car pair) (cadr pair))
|
||||
(solution-pair-overlaps? (cadr pair) (car pair))))
|
||||
pairs)))
|
||||
|
||||
(ert-deftest solution-part-1-example ()
|
||||
(should (equal (solution-part-1 (aoc-read-file-lines solution-example-input)) 2)))
|
||||
|
||||
(ert-deftest solution-part-1 ()
|
||||
(should (equal (solution-part-1 (aoc-read-file-lines solution-input)) 644)))
|
||||
|
||||
(when noninteractive
|
||||
(ert-run-tests-batch))
|
||||
|
||||
;; Code
|
||||
|
||||
;; Local Variables:
|
||||
;; read-symbol-shorthands: (("solution-" . "advent-of-code-day-4-") ("aoc-" . "advent-of-code-"))
|
||||
;; End:
|
6
input/day-4-example.txt
Normal file
6
input/day-4-example.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
1000
input/day-4.txt
Normal file
1000
input/day-4.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue