Skip to content

Instantly share code, notes, and snippets.

@doeixd
Created December 5, 2023 21:16
Show Gist options
  • Save doeixd/ef7c938cdb1d1f1382f253e317b0ded0 to your computer and use it in GitHub Desktop.
Save doeixd/ef7c938cdb1d1f1382f253e317b0ded0 to your computer and use it in GitHub Desktop.
day5.mjs
let isTest = Deno.args.join('').match(/test\d?/)?.[0]
if (isTest && !/\d/.test(isTest.split('').at(-1))) isTest += '1'
let file = await Deno.readTextFile(`./${isTest || `input`}`)
file = file.trim('').split('\n\n')
let some_seeds = file[0].split(':')?.[1].trim().split(/\s+/).map((l) => +l)
file = file.slice(1)
let groups = []
for (let line of file) {
let [_map, values] = line.trim().split('map:')
values = values.trim().split(/\n/).map((l) => l.trim().split(/\s+/)).map((l) => l.map((x) => +x))
groups.push(values)
}
// console.log(get_smallest_location(some_seeds))
// let smallest = Infinity
// for (let seed_idx in some_seeds) {
// seed_idx = +seed_idx
// if ((seed_idx + 1) % 2 == 0) continue
// let seed_start = some_seeds[seed_idx]
// let qty = some_seeds[seed_idx + 1]
// let seed_end = seed_start + qty
// let seeds = []
// console.log(seeds.length)
// for (let i = 0; i < qty; i++) {
// seeds.push(seed_start + i)
// }
// // console.log(seeds)
// let sub_smallest = get_smallest_location(seeds)
// // console.log(sub_smallest)
// if (sub_smallest < smallest) smallest = sub_smallest
// }
// console.log(smallest)
// console.log(get_smallest_location(some_seeds, true))
console.log(get_smallest_location(some_seeds, true))
// console.log(get_smallest_location(some_seeds, {value: false}))
function does_first_range_contain_second_range(first_range, second_range, as_range) {
console.log('ran')
return first_range.start <= second_range.start && first_range.end <= second_range.end
}
function get_smallest_location(seeds, as_range = false) {
let smallest_location = Infinity
for (let seed_idx in seeds) {
seed_idx = +seed_idx
let seed = seeds[seed_idx]
let qty = as_range ? (seeds?.[seed_idx + 1] || 1) : 1
let seed_end = seed + qty
// console.log({qty})
if (as_range) {
if ((seed_idx + 1) %2 == 0) continue
}
let og_seed = seed
let items = [seed]
for (let i = 0; i < items.length; i++) {
// seed = og_seed + i
let source_id = seed
// console.log(source_id)
for (let group_idx in groups) {
group_idx = +group_idx
let group = groups[group_idx]
for (let [destination_start, source_start, range] of group) {
let source_end = source_start + range
let is_in_range = source_id <= source_end && source_id >= source_start
let remainder = source_id - source_start
let destination_id = destination_start + remainder
if (as_range && group_idx == 0) {
if (source_end <= seed_end && source_start >= destination_start) {
for (let )
items.push()
}
}
// if (seed == 26) console.log({seed, source_id, destination_id, is_in_range, qty, i, destination_start, source_start, source_end, remainder, range, og_seed})
if (is_in_range) {
source_id = destination_id
break
}
}
}
// console.log({seed, source_id})
if (source_id < smallest_location) smallest_location = source_id
// console.log({smallest_location})
}
}
return smallest_location
}
// console.log(almanac.seed)
// for (let line of file) {
// let [map, values] = line.trim().split('map:')
// values = values.trim().split(/\n/).map(l => l.trim().split(/\s+/)).map(l => l.map(x => +x))
// let { source_name, destination_name } = (map.match(/(?<source_name>\w+?)-to-(?<destination_name>\w+)/).groups)
// names.add(source_name)
// names.add(destination_name)
// // console.log(values)
// for (let [destination_start, source_start, range] of values) {
// let destination = destination_start
// let source = source_start
// for (let i = 0; i <= range; i++) {
// destination += 1
// source += 1
// // source_name == 'seed' && console.log({source, destination, source_name, destination_name, range, i})
// almanac[source_name] ||= {}
// almanac[source_name][source] ||= {}
// almanac[source_name][source][destination_name] ||= {}
// almanac[destination_name] ||= {}
// almanac[destination_name][destination] ||= {}
// almanac[source_name][source][destination_name][destination] = almanac[destination_name][destination]
// }
// }
// }
// // console.log(almanac['seed'])
// names = Array.from(names)
// // console.log(names)
// // console.log(almanac)
// let smallest_location = Infinity
// for (let seed of seeds) {
// let location = (get_location({id: seed}))
// // console.log(location)
// if (location < smallest_location) smallest_location = location
// // console.log(get_location({id: 79}))
// }
// console.log(smallest_location)
// function get_location({id, depth = 0}) {
// // let value = node?.[id]
// let name = names.at(depth)
// let next_name = names.at(depth + 1)
// let next_id = id;
// let value = almanac[name]?.[id]
// let is_populated = value ? !!Object?.keys?.(value)?.length : false
// if (value && is_populated) {
// next_id = Object.keys(value[next_name])[0]
// }
// if (depth == 7) {
// return id
// }
// return get_location({id:next_id, depth:depth + 1})
// }
// function get_location({id, depth = 0}) {
// // let value = node?.[id]
// let name = names.at(depth)
// let next_name = names.at(depth + 1)
// let next_id = id;
// let value = almanac[name]?.[id]
// let is_populated = value ? !!Object?.keys?.(value)?.length : false
// if (value && is_populated) {
// next_id = Object.keys(value[next_name])[0]
// }
// if (depth == 7) {
// return id
// }
// return get_location({id:next_id, depth:depth + 1})
// }
// function get_smallest_location(seeds, as_range = false) {
// let smallest_location = Infinity
// for (let seed_idx in seeds) {
// seed_idx = +seed_idx
// let seed = seeds[seed_idx]
// let qty = as_range ? (seeds?.[seed_idx + 1] || 1) : 1
// // console.log({qty})
// if (as_range) {
// if ((seed_idx + 1) %2 == 0) continue
// }
// let og_seed = seed
// for (let i = 0; i < qty; i++) {
// seed = og_seed + i
// let source_id = seed
// // console.log(source_id)
// for (let group of groups) {
// for (let [destination_start, source_start, range] of group) {
// let source_end = source_start + range
// let is_in_range = source_id <= source_end && source_id >= source_start
// let remainder = source_id - source_start
// let destination_id = destination_start + remainder
// // if (seed == 26) console.log({seed, source_id, destination_id, is_in_range, qty, i, destination_start, source_start, source_end, remainder, range, og_seed})
// if (is_in_range) {
// source_id = destination_id
// break
// }
// }
// }
// // console.log({seed, source_id})
// if (source_id < smallest_location) smallest_location = source_id
// // console.log({smallest_location})
// }
// }
// return smallest_location
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment