diff --git a/2023/README.md b/2023/README.md index 600f612..84b69c5 100644 --- a/2023/README.md +++ b/2023/README.md @@ -3,7 +3,7 @@ | S | M | T | W | T | F | S | | :-: | :-: | :-: | :-: | :-: | :-: | :-: | | | | | | | [1] | [2] | -| 3 | 4 | 5 | 6 | 7 | 8 | 9 | +| [3] | 4 | 5 | 6 | 7 | 8 | 9 | | 10 | 11 | 12 | 13 | 14 | 15 | 16 | | 17 | 18 | 19 | 20 | 21 | 22 | 23 | | 24 | 25 | | | | | | @@ -11,3 +11,4 @@ [1]: ./elixir/lib/2023/1.ex [2]: ./elixir/lib/2023/2.ex +[3]: ./elixir/lib/2023/3.ex diff --git a/2023/elixir/lib/2023/3.ex b/2023/elixir/lib/2023/3.ex new file mode 100644 index 0000000..71314a6 --- /dev/null +++ b/2023/elixir/lib/2023/3.ex @@ -0,0 +1,93 @@ +import AOC + +aoc 2023, 3 do + def p1(input) do + grid = + input + |> String.split("\n") + |> Enum.map(&String.to_charlist/1) + |> to_grid() + + {_, part_numbers} = + for {coord, symb} <- grid, + is_binary(symb), + neighbor_coord <- neighboring_coords(coord), + reduce: {MapSet.new(), []} do + {seen, part_numbers} -> + case Map.get(grid, neighbor_coord) do + {id, n} -> + if MapSet.member?(seen, id) do + {seen, part_numbers} + else + {MapSet.put(seen, id), [n | part_numbers]} + end + + _ -> + {seen, part_numbers} + end + end + + part_numbers + |> Enum.sum() + end + + def p2(_input) do + end + + def to_grid(lines) do + {grid, [], nil, _} = + for {line, i} <- Enum.with_index(lines), + {char, j} <- Enum.with_index(line), + reduce: {Map.new(), [], nil, 0} do + {acc, digits, l, id} -> + case {char, digits, l} do + {c, digits, curr} when c in ?0..?9 and curr in [i, nil] -> + {acc, [{{i, j}, c} | digits], i, id} + + {c, digits, _} when c in ?0..?9 -> + {insert_number_into_grid(acc, digits, id), [{{i, j}, c}], i, id + 1} + + {?., [], _} -> + {acc, [], nil, id} + + {?., digits, _} -> + {insert_number_into_grid(acc, digits, id), [], nil, id + 1} + + {c, [], _} -> + {Map.put(acc, {i, j}, List.to_string([c])), [], nil, id} + + {c, digits, _} -> + { + acc + |> Map.put({i, j}, List.to_string([c])) + |> insert_number_into_grid(digits, id), + [], + nil, + id + 1 + } + end + end + + grid + end + + def insert_number_into_grid(grid, coords_and_digits, id) do + {coords, rev_digits} = Enum.unzip(coords_and_digits) + + number = + rev_digits + |> Enum.reverse() + |> List.to_string() + |> String.to_integer() + + Stream.zip(coords, Stream.repeatedly(fn -> {id, number} end)) + |> Enum.into(%{}) + |> Map.merge(grid) + end + + def neighboring_coords({i, j}) do + for y <- [i - 1, i, i + 1], x <- [j - 1, j, j + 1], {y, x} != {i, j}, y >= 0, x >= 0 do + {y, x} + end + end +end diff --git a/README.md b/README.md index 7dd8c03..55cf7c1 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ 1. [2020] **17/50** 🌟 1. [2021] **43/50** 🌟 1. [2022] **14/50** 🌟 -1. [2023] **4/50** 🌟 +1. [2023] **5/50** 🌟 [2015]: ./2015 [2017]: ./2017