import { InputType } from "./parser.ts";
import { parseInput, MapType } from "./parser.ts";

function mapping(m: MapType, input: number) {
    for (let i = 0; i < m.data.length; i++) {
        const d = m.data[i];
        // if input is in range
        if (input >= d.src && input < d.src + d.range) {
            return input - d.src + d.dst
        }
    }
    // else return input
    return input
}

const input = await Deno.readTextFile("input.txt");
const r = parseInput(input);

function composition(r: InputType, input: number) {
    let result = input;
    for (let i = 0; i < r.map.length; i++) {
        const m = r.map[i];
        result = mapping(m, result);
    }
    return result

    // ["soil", "fertilizer", "water", "light", "temperature", "humidity", "location"].forEach((key) => {
    //     const m = r.map.filter(m => m.to === key)[0];
    
    // });
}

const locations = r.seeds.map(seed => {
    const location = composition(r, seed) 
    console.log(location)
    return location;
});
const min = Math.min(...locations);
console.log("min ",min);