solve 2023 13.1
This commit is contained in:
parent
407453790f
commit
29f788f17b
4 changed files with 69 additions and 9 deletions
|
@ -1,12 +1,12 @@
|
|||
# Advent of Code 2023
|
||||
|
||||
| S | M | T | W | T | F | S |
|
||||
| :-: | :-: | :-: | :-: | :-: | :-: | :-------------------------: |
|
||||
| | | | | | [1] | [2] |
|
||||
| [3] | [4] | [5] | 6 | [7] | [8] | [9]<sup>[haskell][9h]</sup> |
|
||||
| 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]<sup>[haskell][9h]</sup> |
|
||||
| 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
|
||||
|
|
44
2023/lib/2023/13.ex
Normal file
44
2023/lib/2023/13.ex
Normal 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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue