File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import calculateSingleLineDistance from "@lib/calculateSingleLineDistance.ts" ;
2+
3+ export function calculateDistances ( boxes : number [ ] [ ] ) {
4+ const distances = new Map < number , Set < ( typeof boxes ) [ number ] > > ( ) ;
5+ for ( let i = 0 ; i < boxes . length ; i ++ ) {
6+ const a = boxes [ i ] ;
7+ for ( let j = i + 1 ; j < boxes . length ; j ++ ) {
8+ const b = boxes [ j ] ;
9+ const distance = Math . abs ( calculateSingleLineDistance ( a , b ) ) ;
10+ distances . set ( distance , new Set ( [ a , b ] ) ) ;
11+ }
12+ }
13+ return distances ;
14+ }
Original file line number Diff line number Diff line change 1+ import solve from "./solve.ts" ;
2+
3+ import { assertEquals } from "@std/assert" ;
4+
5+ Deno . test ( "example" , ( ) => {
6+ const input = `\
7+ 162,817,812
8+ 57,618,57
9+ 906,360,560
10+ 592,479,940
11+ 352,342,300
12+ 466,668,158
13+ 542,29,236
14+ 431,825,988
15+ 739,650,466
16+ 52,470,668
17+ 216,146,977
18+ 819,987,18
19+ 117,168,530
20+ 805,96,715
21+ 346,949,466
22+ 970,615,88
23+ 941,993,340
24+ 862,61,35
25+ 984,92,344
26+ 425,690,689` ;
27+
28+ assertEquals ( solve ( input , { connect : 10 } ) , 40 ) ;
29+ } ) ;
Original file line number Diff line number Diff line change 1+ import { calculateDistances } from "../../distances.ts" ;
2+
3+ export default function solve ( input : string , { connect = 1000 } = { } ) {
4+ const boxes = input . split ( "\n" ) . map ( ( line ) => line . split ( "," ) . map ( Number ) ) ;
5+ const boxToCircuit = new Map ( boxes . map ( ( box ) => [ box , new Set ( [ box ] ) ] ) ) ;
6+ const stack = Array . from ( calculateDistances ( boxes ) ) . sort ( ( [ a ] , [ b ] ) => b - a ) ;
7+ for ( let n = 0 ; n < connect ; n ++ ) {
8+ const [ , [ boxA , boxB ] ] = stack . pop ( ) ! ;
9+ const circuitA = boxToCircuit . get ( boxA ) ! ;
10+ const circuitB = boxToCircuit . get ( boxB ) ! ;
11+ if ( circuitA === circuitB ) continue ;
12+ const circuit = circuitA . union ( circuitB ) ;
13+ for ( const box of circuit ) boxToCircuit . set ( box , circuit ) ;
14+ }
15+ const circuits = new Set ( boxToCircuit . values ( ) ) ;
16+ return Array . from ( circuits , ( { size } ) => size )
17+ . sort ( ( a , b ) => b - a )
18+ . slice ( 0 , 3 )
19+ . reduce ( ( product , size ) => product * size , 1 ) ;
20+ }
Original file line number Diff line number Diff line change 1+ import solve from "./solve.ts" ;
2+
3+ import { assertEquals } from "@std/assert" ;
4+
5+ Deno . test ( "example" , ( ) => {
6+ const input = `\
7+ 162,817,812
8+ 57,618,57
9+ 906,360,560
10+ 592,479,940
11+ 352,342,300
12+ 466,668,158
13+ 542,29,236
14+ 431,825,988
15+ 739,650,466
16+ 52,470,668
17+ 216,146,977
18+ 819,987,18
19+ 117,168,530
20+ 805,96,715
21+ 346,949,466
22+ 970,615,88
23+ 941,993,340
24+ 862,61,35
25+ 984,92,344
26+ 425,690,689` ;
27+
28+ assertEquals ( solve ( input ) , 25272 ) ;
29+ } ) ;
Original file line number Diff line number Diff line change 1+ import { calculateDistances } from "../../distances.ts" ;
2+
3+ export default function solve ( input : string ) {
4+ const boxes = input . split ( "\n" ) . map ( ( line ) => line . split ( "," ) . map ( Number ) ) ;
5+ const boxToCircuit = new Map ( boxes . map ( ( box ) => [ box , new Set ( [ box ] ) ] ) ) ;
6+ const circuits = new Set ( boxToCircuit . values ( ) ) ;
7+ const stack = Array . from ( calculateDistances ( boxes ) ) . sort ( ( [ a ] , [ b ] ) => b - a ) ;
8+ while ( circuits . size > 1 ) {
9+ const [ , [ boxA , boxB ] ] = stack . pop ( ) ! ;
10+ const circuitA = boxToCircuit . get ( boxA ) ! ;
11+ const circuitB = boxToCircuit . get ( boxB ) ! ;
12+ if ( circuitA === circuitB ) continue ;
13+ circuits . delete ( circuitA ) ;
14+ circuits . delete ( circuitB ) ;
15+ const circuit = circuitA . union ( circuitB ) ;
16+ for ( const box of circuit ) boxToCircuit . set ( box , circuit ) ;
17+ circuits . add ( circuit ) ;
18+ if ( circuits . size === 1 ) return boxA [ 0 ] * boxB [ 0 ] ;
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ export default function calculateSingleLineDistance (
2+ a : readonly number [ ] ,
3+ b : readonly number [ ] ,
4+ ) {
5+ return Math . hypot ( ...a . keys ( ) . map ( ( index ) => a [ index ] - b [ index ] ) ) ;
6+ }
You can’t perform that action at this time.
0 commit comments