-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday24.lisp
More file actions
53 lines (48 loc) · 1.77 KB
/
Copy pathday24.lisp
File metadata and controls
53 lines (48 loc) · 1.77 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
(defpackage :aoc/2017/24 #.cl-user::*aoc-use*)
(in-package :aoc/2017/24)
(defun parse-ports (x &aux (ports (make-hash-table)))
(flet ((parse-port (s &aux (splits (split-sequence:split-sequence #\/ s)))
(mapcar #'parse-integer splits)))
(loop
:for (a b) :in (mapcar #'parse-port x)
:do (push b (gethash a ports))
:unless (= a b) :do (push a (gethash b ports)))
ports))
(defun gen-bridges (ports)
(labels ((id (a b)
(if (> a b) (complex b a) (complex a b)))
(possible-outputs (a visited)
(loop
:for b :in (gethash a ports)
:for id = (id a b)
:unless (member id visited)
:collect b))
(recur (a visited)
(let ((outputs (possible-outputs a visited)))
(if (null outputs)
(list NIL)
(loop
:for b :in outputs
:for id = (id a b)
:for rest = (recur b (cons id visited))
:append (mapcar (partial-1 #'cons id) rest))))))
(recur 0 nil)))
(defun solve-part1 (bridges)
(labels ((strength (bridge)
(loop
:for p :in bridge
:sum (+ (realpart p) (imagpart p)))))
(loop
:for bridge :in bridges
:maximize (strength bridge))))
(define-solution (2017 24) (data parse-ports)
(let ((bridges (gen-bridges data)))
(values
(solve-part1 bridges)
(flet ((longest (bridges)
(loop
:for bridge :in bridges
:maximize (length bridge) :into longest
:finally (return (remove-if-not (partial-1 #'= longest) bridges :key #'length)))))
(solve-part1 (longest bridges))))))
(define-test (2017 24) (2006 1994))