Last active
December 3, 2019 10:51
-
-
Save emplv/01e66858a25c73d3b601b3b4d8052a31 to your computer and use it in GitHub Desktop.
AoC 2019 - day 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const INPUT = ` | |
| COPY & PASTE HERE INPUT LINES | |
| `; | |
| const [firstLine,secondLine]=INPUT.trim().split`\n`; | |
| const first = firstLine.split`,`; | |
| const second = secondLine.split`,`; | |
| const wires = {}; | |
| [first,second].forEach(input => { | |
| const currentWire = {}; | |
| input.forEach(coord => { | |
| const [length] = coord.match(/\d+/); | |
| const directionIndex = +/U|D/.test(coord[0]); | |
| const sign = /U|R/.test(coord[0]) || -1; | |
| [...Array(+length)].map((_,j)=>{ | |
| fc[directionIndex] += +sign; | |
| fc[2] += 1; | |
| const wiresIndex = `${fc[0]}_${fc[1]}`; | |
| wires[wiresIndex] = wires[wiresIndex] || []; | |
| if (!currentWire[wiresIndex]) { | |
| wires[wiresIndex].push(fc[2]); | |
| currentWire[wiresIndex] = fc[2]; | |
| } | |
| }); | |
| }, fc=[0,0,0]); | |
| }); | |
| const crossesKeys=Object.keys(wires).filter(key=>wires[key].length>1); | |
| // Part 1 | |
| crossesKeys.map(key=>(x=key.split`_`,Math.abs(x[0])+Math.abs(x[1]))).sort((x,y)=>x-y)[0]; | |
| // Part 2 | |
| crossesKeys.map(key=>wires[key][0]- -wires[key][1]).sort((x,y)=>x-y)[0]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment