|
1 | | -function addOneToNetwork(network, connections = network) { |
| 1 | +function addOneToNetworks(networks, connections = networks) { |
2 | 2 | let set = new Set(); |
3 | | - network.forEach(connection => { |
4 | | - let as = connection |
| 3 | + networks.forEach(computers => { |
| 4 | + let candidates = computers |
5 | 5 | .map(c => { |
6 | 6 | return connections |
7 | | - .filter(other => other.includes(c)) |
8 | | - .map(a => a.find(x => x !== c)); |
| 7 | + .filter(pair => pair.includes(c)) |
| 8 | + .map(pair => pair.find(x => x !== c)); |
9 | 9 | }) |
10 | | - .map(a => new Set(a)); |
11 | | - let result = as.reduce((a, b) => a.intersection(b)); |
12 | | - result.forEach(x => set.add([...connection, x].sort().join(","))); |
| 10 | + .map(x => new Set(x)); |
| 11 | + let result = candidates.reduce((a, b) => a.intersection(b)); |
| 12 | + result.forEach(x => set.add([...computers, x].sort().join(","))); |
13 | 13 | }); |
14 | 14 | return set; |
15 | 15 | } |
16 | 16 |
|
17 | 17 | export function part1(input) { |
18 | | - let connections = input.split("\n").map(line => { |
19 | | - let [a, b] = line.split("-").sort(); |
20 | | - return [a, b]; |
21 | | - }); |
22 | | - let set = addOneToNetwork(connections); |
| 18 | + let connections = input.split("\n").map(line => line.split("-")); |
| 19 | + let set = addOneToNetworks(connections); |
23 | 20 | return [...set].filter(x => x.match(/(^t|,t)/)).length; |
24 | 21 | } |
25 | 22 |
|
26 | 23 | export function part2(input) { |
27 | | - let connections = input.split("\n").map(line => { |
28 | | - let [a, b] = line.split("-").sort(); |
29 | | - return [a, b]; |
30 | | - }); |
31 | | - let network = connections; |
32 | | - while (network.length > 1) { |
33 | | - let next = addOneToNetwork(network, connections); |
34 | | - network = [...next].map(x => x.split(",")); |
| 24 | + let connections = input.split("\n").map(line => line.split("-")); |
| 25 | + let networks = connections; |
| 26 | + while (networks.length > 1) { |
| 27 | + let next = addOneToNetworks(networks, connections); |
| 28 | + networks = [...next].map(x => x.split(",")); |
35 | 29 | } |
36 | | - return network[0].sort().join(","); |
| 30 | + return networks[0].sort().join(","); |
37 | 31 | } |
0 commit comments