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