|
| 1 | +defmodule Day05 do |
| 2 | + defp read_input(file_path) do |
| 3 | + case File.read(file_path) do |
| 4 | + {:ok, content} -> |
| 5 | + sections = content |> String.split("\n\n", trim: true) |
| 6 | + |
| 7 | + seeds = |
| 8 | + sections |
| 9 | + |> Enum.at(0) |
| 10 | + |> String.split(" ", trim: true) |
| 11 | + |> tl |
| 12 | + |> Enum.map(&String.to_integer/1) |
| 13 | + |
| 14 | + maps = 1..7 |> Enum.map(&read_section_n(&1, sections)) |
| 15 | + {seeds, maps} |
| 16 | + |
| 17 | + {:error, reason} -> |
| 18 | + raise "Oh no! #{reason}" |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + defp read_section_n(n, sections) do |
| 23 | + sections |
| 24 | + |> Enum.at(n) |
| 25 | + |> String.split("\n", trim: true) |
| 26 | + |> Enum.slice(1..-1) |
| 27 | + |> Enum.map(&String.split(&1, " ", trim: true)) |
| 28 | + |> Enum.map(fn x -> Enum.map(x, &String.to_integer/1) end) |
| 29 | + |> Enum.sort_by(&Enum.at(&1, 1)) |
| 30 | + end |
| 31 | + |
| 32 | + defp lookupRange([dst, src, sz], x) do |
| 33 | + if x >= src and x < src + sz do |
| 34 | + dst + (x - src) |
| 35 | + else |
| 36 | + nil |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + defp lookupSectionH(section, [start, sz], acc) do |
| 41 | + droppedSection = Enum.drop_while(section, fn [_, src, sz] -> src + sz <= start end) |
| 42 | + |
| 43 | + case droppedSection do |
| 44 | + [] -> |
| 45 | + [[start, sz] | acc] |
| 46 | + |
| 47 | + [[dst, src, sz2] | _] -> |
| 48 | + if src > start do |
| 49 | + if src - start > sz do |
| 50 | + [[start, sz] | acc] |
| 51 | + else |
| 52 | + lookupSectionH(section, [src, sz - src + start], [[start, src - start] | acc]) |
| 53 | + end |
| 54 | + else |
| 55 | + if src + sz2 > start + sz do |
| 56 | + [[dst + start - src, sz] | acc] |
| 57 | + else |
| 58 | + lookupSectionH(section, [src + sz2, sz - (sz2 - start + src)], [ |
| 59 | + [dst + start - src, sz2 - start + src] | acc |
| 60 | + ]) |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + defp lookupSection(section, x) when is_integer(x) do |
| 67 | + lu = |
| 68 | + section |
| 69 | + |> Enum.map(&lookupRange(&1, x)) |
| 70 | + |> Enum.filter(fn x -> x != nil end) |
| 71 | + |
| 72 | + case lu do |
| 73 | + [] -> x |
| 74 | + [a] -> a |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + defp lookupSection(section, x) do |
| 79 | + x |> Enum.map(&lookupSectionH(section, &1, [])) |> Enum.concat() |
| 80 | + end |
| 81 | + |
| 82 | + defp lookupMap(map, x) do |
| 83 | + map |> Enum.reduce(x, &lookupSection(&1, &2)) |
| 84 | + end |
| 85 | + |
| 86 | + def partA(file_path) do |
| 87 | + {seeds, maps} = read_input(file_path) |
| 88 | + seeds |> Enum.map(&lookupMap(maps, &1)) |> Enum.min() |
| 89 | + end |
| 90 | + |
| 91 | + def partB(file_path) do |
| 92 | + {seedRanges, maps} = read_input(file_path) |
| 93 | + lookupMap(maps, Enum.chunk_every(seedRanges, 2)) |> Enum.min_by(fn [x, _] -> x end) |> hd |
| 94 | + end |
| 95 | +end |
| 96 | + |
| 97 | +IO.puts(Day05.partA("./input")) |
| 98 | +IO.puts(Day05.partB("./input")) |
0 commit comments