Skip to content

Commit d444a51

Browse files
committed
day5
1 parent 9b42645 commit d444a51

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ erl_crash.dump
88
*.beam
99
/config/*.secret.exs
1010
.elixir_ls/
11+
input

05/sample_input

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
seeds: 79 14 55 13
2+
3+
seed-to-soil map:
4+
50 98 2
5+
52 50 48
6+
7+
soil-to-fertilizer map:
8+
0 15 37
9+
37 52 2
10+
39 0 15
11+
12+
fertilizer-to-water map:
13+
49 53 8
14+
0 11 42
15+
42 0 7
16+
57 7 4
17+
18+
water-to-light map:
19+
88 18 7
20+
18 25 70
21+
22+
light-to-temperature map:
23+
45 77 23
24+
81 45 19
25+
68 64 13
26+
27+
temperature-to-humidity map:
28+
0 69 1
29+
1 0 69
30+
31+
humidity-to-location map:
32+
60 56 37
33+
56 93 4

05/sol.exs

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)