1
0
Fork 0

solve 2023 13.1

This commit is contained in:
sloane 2023-12-13 09:13:21 -05:00
parent 407453790f
commit 29f788f17b
No known key found for this signature in database
4 changed files with 69 additions and 9 deletions

View file

@ -1,12 +1,12 @@
# Advent of Code 2023 # Advent of Code 2023
| S | M | T | W | T | F | S | | S | M | T | W | T | F | S |
| :-: | :-: | :-: | :-: | :-: | :-: | :-------------------------: | | :-: | :-: | :-: | :-: | :-: | :-: | :-------------------------: |
| | | | | | [1] | [2] | | | | | | | [1] | [2] |
| [3] | [4] | [5] | 6 | [7] | [8] | [9]<sup>[haskell][9h]</sup> | | [3] | [4] | [5] | 6 | [7] | [8] | [9]<sup>[haskell][9h]</sup> |
| 10 | [11] | 12 | 13 | 14 | 15 | 16 | | 10 | [11] | 12 | [13] | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 | | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | | | | | | | 24 | 25 | | | | | |
[1]: ./lib/2023/1.ex [1]: ./lib/2023/1.ex
[2]: ./lib/2023/2.ex [2]: ./lib/2023/2.ex
@ -22,3 +22,5 @@
[9h]: ./9.hs [9h]: ./9.hs
[11]: ./lib/2023/11.ex [11]: ./lib/2023/11.ex
[13]: ./lib/2023/13.ex

44
2023/lib/2023/13.ex Normal file
View file

@ -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

View file

@ -7,10 +7,12 @@ defmodule AOCHelpers do
String.split(str) String.split(str)
end end
def letters(str) when is_binary(str) do def chars(str) when is_binary(str) do
String.split(str, "", trim: true) String.split(str, "", trim: true)
end end
def letters(str), do: chars(str)
def integers(str) when is_binary(str) do def integers(str) when is_binary(str) do
str str
|> words() |> words()
@ -27,6 +29,12 @@ defmodule AOCHelpers do
|> Enum.map(&integers/1) |> Enum.map(&integers/1)
end end
def lines_of_chars(input) do
input
|> lines()
|> Enum.map(&chars/1)
end
def to_grid(str) do def to_grid(str) do
lists = lists =
str str
@ -77,4 +85,10 @@ defmodule AOCHelpers do
def combinations([x | xs], n) do def combinations([x | xs], n) do
for(tail <- combinations(xs, n - 1), do: [x | tail]) ++ combinations(xs, n) for(tail <- combinations(xs, n - 1), do: [x | tail]) ++ combinations(xs, n)
end end
def transpose(list_of_lists) do
list_of_lists
|> Enum.zip()
|> Enum.map(&Tuple.to_list/1)
end
end end

View file

@ -14,7 +14,7 @@
1. [2020] **17/50** 🌟 1. [2020] **17/50** 🌟
1. [2021] **43/50** 🌟 1. [2021] **43/50** 🌟
1. [2022] **14/50** 🌟 1. [2022] **14/50** 🌟
1. [2023] **17/50** 🌟 1. [2023] **18/50** 🌟
[2015]: ./2015 [2015]: ./2015
[2017]: ./2017 [2017]: ./2017