Browse Source

Day 8

master
Shadowfacts 2 years ago
parent
commit
f10b2fdf99
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
  1. 71
      lib/day8/day8.ex
  2. 1
      lib/day8/input.txt

71
lib/day8/day8.ex

@ -0,0 +1,71 @@
defmodule Day8 do
def part1 do
File.read!("lib/day8/input.txt")
|> parse_input()
|> split_into_layers(25, 6)
|> min_zeros()
|> ones_times_twos()
# "123456789012"
# |> parse_input()
# |> split_into_layers(3, 2)
# |> min_zeros()
# |> ones_times_twos()
end
def part2 do
File.read!("lib/day8/input.txt")
|> parse_input()
|> split_into_layers(25, 6)
|> combine_layers()
|> Enum.map(fn
0 -> ?\s
1 -> ?X
end)
|> Enum.chunk_every(25)
|> Enum.each(&IO.puts/1)
# "0222112222120000"
# |> parse_input()
# |> split_into_layers(2, 2)
# |> combine_layers()
end
def parse_input(str) do
str
|> String.trim()
|> String.to_charlist()
|> Enum.map(fn digit -> digit - ?0 end)
end
def split_into_layers(input, layer_width, layer_height) do
Enum.chunk_every(input, layer_width * layer_height)
end
def min_zeros(layers) do
layers
|> Enum.min_by(fn layer ->
Enum.count(layer, &(&1 == 0))
end)
end
def ones_times_twos(layer) do
ones = Enum.count(layer, &(&1 == 1))
twos = Enum.count(layer, &(&1 == 2))
ones * twos
end
def combine_layers(layers) do
Enum.zip(layers)
|> Enum.map(&combine_pixel/1)
end
def combine_pixel(pixel) do
0..(tuple_size(pixel) - 1)
|> Enum.map(fn index -> elem(pixel, index) end)
|> Enum.reduce(2, fn
val, 2 -> val
_, it -> it
end)
end
end

1
lib/day8/input.txt

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save