function validateFifth(input) 473 { 474 let one = []; 475 let twos = []; 476 let array = input.trim().split("\n"); 477 let foundTwo = false; 478 let foundOne = false; 479 console.log("Parsing array"); 480 console.log(array); 481 for (let i = 0; i < array.length; ++i) 482 { 483 if (array[i].length !== array[0].length) 484 { 485 console.log("Invalid array"); 486 return "Arrays cannot have differing lengths."; 487 } 488 for (let j = 0; j < array[i].length; ++j) 489 { 490 if (array[i][j] === '2') 491 { 492 foundTwo = true; 493 console.log("Found 2 at " + [i,j]); 494 twos.push([i,j]); 495 } 496 if (array[i][j] === '1') 497 { 498 if (!foundOne) 499 { 500 console.log("Found 1 at " + [i, j]); 501 one = [i, j]; 502 foundOne = true; 503 } 504 else 505 { 506 console.log("Invalid array"); 507 return "Cannot have multiple ones in the array."; 508 } 509 } 510 } 511 } 512 if (!foundOne) 513 { 514 console.log("Invalid array"); 515 return "Missing '1'."; 516 } 517 518 let minDistance = null; 519 for (let i = 0; i < twos.length; ++i) 520 { 521 console.log("Finding distance for " + twos[i]); 522 let x = Math.min(array[0].length - (Math.abs(one[0] - twos[i][0])), (Math.abs(one[0] - twos[i][0]))); 523 console.log("x distance: " + x); 524 let y = Math.min(array[0].length - (Math.abs(one[1] - twos[i][1])), (Math.abs(one[1] - twos[i][1]))); 525 console.log("y distance: " + y); 526 if ((x + y < minDistance) || !minDistance) 527 { 528 console.log("New minDistance is " + (x + y)); 529 minDistance = x + y; 530 } 531 } 532 return minDistance; 533 }