parent
f211a93e3e
commit
df417ae668
2 changed files with 178 additions and 0 deletions
@ -0,0 +1,32 @@ |
||||
kh-tc |
||||
qp-kh |
||||
de-cg |
||||
ka-co |
||||
yn-aq |
||||
qp-ub |
||||
cg-tb |
||||
vc-aq |
||||
tb-ka |
||||
wh-tc |
||||
yn-cg |
||||
kh-ub |
||||
ta-co |
||||
de-co |
||||
tc-td |
||||
tb-wq |
||||
wh-td |
||||
ta-ka |
||||
td-qp |
||||
aq-cg |
||||
wq-ub |
||||
ub-vc |
||||
de-ta |
||||
wq-aq |
||||
wq-vc |
||||
wh-yn |
||||
ka-de |
||||
kh-ta |
||||
co-tc |
||||
wh-qp |
||||
tb-vc |
||||
td-yn |
||||
@ -0,0 +1,146 @@ |
||||
#+title: Solution to p23 |
||||
|
||||
* Script |
||||
** Part 1 |
||||
---- Day 23: LAN Party --- |
||||
As The Historians wander around a secure area at Easter Bunny HQ, you come across posters for a LAN party scheduled for today! Maybe you can find it; you connect to a nearby datalink port and download a map of the local network (your puzzle input). |
||||
|
||||
The network map provides a list of every connection between two computers. For example: |
||||
|
||||
kh-tc |
||||
qp-kh |
||||
de-cg |
||||
ka-co |
||||
yn-aq |
||||
qp-ub |
||||
cg-tb |
||||
vc-aq |
||||
tb-ka |
||||
wh-tc |
||||
yn-cg |
||||
kh-ub |
||||
ta-co |
||||
de-co |
||||
tc-td |
||||
tb-wq |
||||
wh-td |
||||
ta-ka |
||||
td-qp |
||||
aq-cg |
||||
wq-ub |
||||
ub-vc |
||||
de-ta |
||||
wq-aq |
||||
wq-vc |
||||
wh-yn |
||||
ka-de |
||||
kh-ta |
||||
co-tc |
||||
wh-qp |
||||
tb-vc |
||||
td-yn |
||||
|
||||
Each line of text in the network map represents a single connection; |
||||
the line kh-tc represents a connection between the computer named kh |
||||
and the computer named tc. Connections aren't directional; tc-kh would |
||||
mean exactly the same thing. |
||||
|
||||
LAN parties typically involve multiplayer games, so maybe you can |
||||
locate it by finding groups of connected computers. Start by looking |
||||
for sets of three computers where each computer in the set is |
||||
connected to the other two computers. |
||||
|
||||
In this example, there are 12 such sets of three inter-connected |
||||
computers: |
||||
|
||||
aq,cg,yn |
||||
aq,vc,wq |
||||
co,de,ka |
||||
co,de,ta |
||||
co,ka,ta |
||||
de,ka,ta |
||||
kh,qp,ub |
||||
qp,td,wh |
||||
tb,vc,wq |
||||
tc,td,wh |
||||
td,wh,yn |
||||
ub,vc,wq |
||||
|
||||
If the Chief Historian is here, and he's at the LAN party, it would be best to know that right away. You're pretty sure his computer's name starts with t, so consider only sets of three computers where at least one computer's name starts with t. That narrows the list down to 7 sets of three inter-connected computers: |
||||
|
||||
co,de,ta |
||||
co,ka,ta |
||||
de,ka,ta |
||||
qp,td,wh |
||||
tb,vc,wq |
||||
tc,td,wh |
||||
td,wh,yn |
||||
|
||||
Find all the sets of three inter-connected computers. How many contain at least one computer with a name that starts with t? |
||||
** Part 2 |
||||
|
||||
* Solution |
||||
#+begin_src emacs-lisp :results none |
||||
(require 'dash) |
||||
(with-temp-buffer |
||||
(insert-file-contents "input-test") |
||||
(goto-char (point-min)) |
||||
(advent/replace-multiple-regex-buffer |
||||
'(("^\\(.*\\)-\\(.*\\)$" . "(\"\\1\" . \"\\2\") (\"\\2\" . \"\\1\")"))) |
||||
(insert "(setq data '(") |
||||
(goto-char (point-max)) |
||||
(insert "))") |
||||
(eval-buffer)) |
||||
data |
||||
#+end_src |
||||
|
||||
Create adjacency matrix |
||||
#+begin_src emacs-lisp |
||||
(setq computer-list (-distinct (-map #'car data))) |
||||
|
||||
(setq adjacency-matrix |
||||
(-map (lambda (a) |
||||
(--map (if (-contains? data (cons a it)) 1 0) computer-list)) |
||||
computer-list)) |
||||
|
||||
(defun multiply-adjacency-matrices (a b) |
||||
"This multiplies two symmetric matrices; it assumes that dimensions are correct" |
||||
(-map (lambda (row) (--map (apply #'+ (-zip-with #'* row it)) b)) a)) |
||||
|
||||
(multiply-adjacency-matrices (multiply-adjacency-matrices adjacency-matrix adjacency-matrix) adjac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 10 | 10 | 2 | 2 | 1 | 1 | 9 | 2 | 3 | 1 | 2 | 7 | 3 | 6 | 2 | |
||||
| 2 | 1 | 2 | 3 | 3 | 1 | 1 | 3 | 10 | 9 | 10 | 7 | 2 | 2 | 2 | 6 | |
||||
|
||||
#+begin_src emacs-lisp |
||||
(defun cluster (computer) |
||||
"This function returns the smallest cluster containing the given computer" |
||||
; a cluster is a set so that every computer is connected to every |
||||
; other computer in the cluster |
||||
(-reduce #'-intersection (--map (cons it (neighbours it)) (neighbours computer)))) |
||||
|
||||
(defun neighbours (computer) |
||||
(-map #'cdr (--filter (string= (car it) computer) data))) |
||||
|
||||
|
||||
(cluster "co") |
||||
#+end_src |
||||
|
||||
#+RESULTS: |
||||
| kh | wh | td | co | |
||||
Loading…
Reference in new issue