//import { loadFile } from "std"; //var data = loadFile("5.txt").trim(); var data = await Deno.readTextFile("5.txt"); data = data.trim(); var getnums = function(w) { return w.match(/\d+/g).map(x => Number(x)); } var seeds = getnums(data.split('\n')[0]); var maps = data.split('\n\n').slice(1).map(g => g.split('\n').slice(1).map(x => getnums(x))); maps = maps.map(m => m.sort(function(a,b) { if (a[0] > b[0]) return 1; if (a[0] < b[0]) return -1; return 0; })); console.log(maps); var loc = 0; seeds.forEach(function(d) { maps.forEach(function(map) { var r = map.find(x => d >= x[1] && d <= x[1]+x[2]); if (r) d += (r[0]-r[1]); }); loc = Math.min(loc,d) || d; }); console.log(loc); var seedpairs = []; for (var i = 0; i < 10; i++) seedpairs.push([seeds[i*2], seeds[i*2+1]]); function in_seed(d) { return seedpairs.some(function(x) { return (d >= x[0] && d <= x[0]+x[1]); }); } maps.reverse(); for (var i = 0; i >= 0; i++) { var d = i; maps.forEach(function(m) { var r = m.find(r => d >= r[0] && d <= r[0] + r[2]); if (r) d += (r[1]-r[0]); }); if (in_seed(d)) { console.log(`location ${i}`); break; } }