this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

part 2, attempt 1 -- brute force, no bueno

+106 -6
+66 -2
2023/day05/part2.ts
··· 1 + import { assert } from "https://deno.land/std@0.208.0/assert/mod.ts"; 2 + 1 3 if (import.meta.main) { 2 4 const input = ( 3 5 await Deno.readFile("input").then((bytes) => ··· 7 9 console.log(answer(input)); 8 10 } 9 11 12 + type Range = { 13 + fromStart: number; 14 + toStart: number; 15 + range: number; 16 + }; 17 + 18 + type MapRanges = { 19 + from: string; 20 + to: string; 21 + ranges: Range[]; 22 + }; 23 + 10 24 export function answer(input: string): number { 11 - console.log(input); 12 - return 42; 25 + const [seedLine, _, ...mapLines] = input.split("\n"); 26 + const seedRanges = seedLine 27 + .replace("seeds: ", "") 28 + .split(" ") 29 + .map((x) => parseInt(x)); 30 + 31 + const seeds = []; 32 + for (let i = 0; i < seedRanges.length; i += 2) { 33 + const from = seedRanges[i]; 34 + const to = from + seedRanges[i + 1]; 35 + for (let j = 0; j < to - from; j++) { 36 + seeds.push(seedRanges[i] + j); 37 + } 38 + } 39 + 40 + const mapRanges: MapRanges[] = mapLines 41 + .join("\n") 42 + .split("\n\n") 43 + .map((mapLine) => { 44 + const [fromToLine, ...rangeLines] = mapLine.split("\n"); 45 + const [_, from, to] = fromToLine.match(/([a-z]+)-to-([a-z]+) map/)!; 46 + const ranges = rangeLines.map((rangeLine) => { 47 + const [toStart, fromStart, range] = rangeLine 48 + .split(" ") 49 + .map((n) => parseInt(n)); 50 + return { fromStart, toStart, range }; 51 + }); 52 + 53 + return { from, to, ranges }; 54 + }); 55 + 56 + // start at `from: seed` to `to: location` 57 + const locationNumbers = seeds.map((s) => getLocationNumber(s, mapRanges)); 58 + 59 + return Math.min(...locationNumbers); 60 + } 61 + 62 + function getLocationNumber(seed: number, mapRanges: MapRanges[]): number { 63 + let current = "seed"; 64 + let currentNum = seed; 65 + while (current !== "location") { 66 + const mapRange = mapRanges.find(({ from }) => from === current); 67 + assert(mapRange, `mapRange not found for: ${current}`); 68 + 69 + const range = mapRange.ranges.find( 70 + (r) => currentNum >= r.fromStart && currentNum <= r.fromStart + r.range, 71 + ); 72 + if (range) currentNum = currentNum + (range.toStart - range.fromStart); 73 + 74 + current = mapRange.to; 75 + } 76 + return currentNum; 13 77 }
+40 -4
2023/day05/test.ts
··· 10 10 "50 98 2", 11 11 "52 50 48", 12 12 "", 13 + // 13 + (15-0) 13 14 "soil-to-fertilizer map:", 14 15 "0 15 37", 15 16 "37 52 2", ··· 41 42 assertEquals(p1.answer(examples), 35); 42 43 }); 43 44 44 - // Deno.test("part2", () => { 45 - // const examples = ["abc", "def"].join("\n"); 46 - // assertEquals(p2.answer(examples), 42); 47 - // }); 45 + Deno.test("part2", () => { 46 + const examples = [ 47 + "seeds: 79 14 55 13", 48 + "", 49 + "seed-to-soil map:", 50 + "50 98 2", 51 + "52 50 48", 52 + "", 53 + // 13 + (15-0) 54 + "soil-to-fertilizer map:", 55 + "0 15 37", 56 + "37 52 2", 57 + "39 0 15", 58 + "", 59 + "fertilizer-to-water map:", 60 + "49 53 8", 61 + "0 11 42", 62 + "42 0 7", 63 + "57 7 4", 64 + "", 65 + "water-to-light map:", 66 + "88 18 7", 67 + "18 25 70", 68 + "", 69 + "light-to-temperature map:", 70 + "45 77 23", 71 + "81 45 19", 72 + "68 64 13", 73 + "", 74 + "temperature-to-humidity map:", 75 + "0 69 1", 76 + "1 0 69", 77 + "", 78 + "humidity-to-location map:", 79 + "60 56 37", 80 + "56 93 4", 81 + ].join("\n"); 82 + assertEquals(p2.answer(examples), 46); 83 + });