From 29f788f17b4ef6ce68ca9e56fd93ecb083fed18b Mon Sep 17 00:00:00 2001 From: sloane <1699281+sloanelybutsurely@users.noreply.github.com> Date: Wed, 13 Dec 2023 09:13:21 -0500 Subject: [PATCH] solve 2023 13.1 --- 2023/README.md | 16 ++++++++------- 2023/lib/2023/13.ex | 44 +++++++++++++++++++++++++++++++++++++++++ 2023/lib/aoc_helpers.ex | 16 ++++++++++++++- README.md | 2 +- 4 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 2023/lib/2023/13.ex diff --git a/2023/README.md b/2023/README.md index dca55fa..094313d 100644 --- a/2023/README.md +++ b/2023/README.md @@ -1,12 +1,12 @@ # Advent of Code 2023 -| S | M | T | W | T | F | S | -| :-: | :-: | :-: | :-: | :-: | :-: | :-------------------------: | -| | | | | | [1] | [2] | -| [3] | [4] | [5] | 6 | [7] | [8] | [9][haskell][9h] | -| 10 | [11] | 12 | 13 | 14 | 15 | 16 | -| 17 | 18 | 19 | 20 | 21 | 22 | 23 | -| 24 | 25 | | | | | | +| S | M | T | W | T | F | S | +| :-: | :-: | :-: | :-: | :-: | :-: | :-------------------------: | +| | | | | | [1] | [2] | +| [3] | [4] | [5] | 6 | [7] | [8] | [9][haskell][9h] | +| 10 | [11] | 12 | [13] | 14 | 15 | 16 | +| 17 | 18 | 19 | 20 | 21 | 22 | 23 | +| 24 | 25 | | | | | | [1]: ./lib/2023/1.ex [2]: ./lib/2023/2.ex @@ -22,3 +22,5 @@ [9h]: ./9.hs [11]: ./lib/2023/11.ex + +[13]: ./lib/2023/13.ex diff --git a/2023/lib/2023/13.ex b/2023/lib/2023/13.ex new file mode 100644 index 0000000..5fb2c3b --- /dev/null +++ b/2023/lib/2023/13.ex @@ -0,0 +1,44 @@ +import AOC +import AOCHelpers + +aoc 2023, 13 do + def p1(input) do + input + |> String.split("\n\n") + |> Enum.map(&lines_of_chars/1) + |> Enum.map(&summarize/1) + |> Enum.sum() + end + + def p2(_input) do + end + + def summarize(lines) do + vertical_line_of_reflection = find_reflection_index(lines) + horizontal_line_of_reflection = lines |> transpose() |> find_reflection_index() + + summary = vertical_line_of_reflection + 100 * horizontal_line_of_reflection + + summary + end + + def find_reflection_index(lines) do + len = lines |> List.first() |> length() + + idx = + for i <- 1..(len - 1) do + lines + |> Enum.all?(fn line -> + line + |> Enum.split(i) + |> Tuple.to_list() + |> map_list([&Enum.reverse/1, &id/1]) + |> Enum.zip_with(&apply(Kernel, :==, &1)) + |> Enum.all?(is?(true)) + end) + end + |> Enum.find_index(is?(true)) + + if idx, do: idx + 1, else: 0 + end +end diff --git a/2023/lib/aoc_helpers.ex b/2023/lib/aoc_helpers.ex index cdda4a3..fa90c04 100644 --- a/2023/lib/aoc_helpers.ex +++ b/2023/lib/aoc_helpers.ex @@ -7,10 +7,12 @@ defmodule AOCHelpers do String.split(str) end - def letters(str) when is_binary(str) do + def chars(str) when is_binary(str) do String.split(str, "", trim: true) end + def letters(str), do: chars(str) + def integers(str) when is_binary(str) do str |> words() @@ -27,6 +29,12 @@ defmodule AOCHelpers do |> Enum.map(&integers/1) end + def lines_of_chars(input) do + input + |> lines() + |> Enum.map(&chars/1) + end + def to_grid(str) do lists = str @@ -77,4 +85,10 @@ defmodule AOCHelpers do def combinations([x | xs], n) do for(tail <- combinations(xs, n - 1), do: [x | tail]) ++ combinations(xs, n) end + + def transpose(list_of_lists) do + list_of_lists + |> Enum.zip() + |> Enum.map(&Tuple.to_list/1) + end end diff --git a/README.md b/README.md index bbf2052..e2b4e09 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] **17/50** 🌟 +1. [2023] **18/50** 🌟 [2015]: ./2015 [2017]: ./2017