Finally, a problem made for FP again.
First, a small utility function to partition an encoded image into its layers:
; Returns a sequence of the layers in an encoded image starting from the bottom layer. Not lazy(defnlayers[imagedimensions](reverse(partition(apply*dimensions)image)))
Part 1:
; For an encoded image (sequence of chars or string) and dimensions in the format [x y],; finds the layer that has the least '0' pixels and calculates the value required to solve part 1.(defncorruption-test[imagedimensions](let[least-zeros-layer(applymin-key#(%\0)(mapfrequencies(layersimagedimensions)))](*(least-zeros-layer\1)(least-zeros-layer\2))))
Part 2:
; Layers a pixel onto a base pixel. If the new pixel is transparent, returns the base pixel, else the new pixel.(defnlayer[base-pixelnew-pixel](if(=\2new-pixel)base-pixelnew-pixel)); Returns a suited string representation for an encoded pixel.(defnpixel-to-str[pixel](if(=pixel\0)" ""█")); Decodes an encoded image (raw string or sequence of chars).; Splits the input into layers, reduces them by layering them on top of each other,; maps each pixel to an according string representation, partitions the result into rows; (depicted by the provided dimensions) and returns them as a string joined with new lines.(defndecode[imagedimensions](applystr(flatten(interpose"\n"(partition(dimensions0)(mappixel-to-str(reduce(partialmaplayer)(layersimagedimensions))))))))
Finally, a problem made for FP again.
First, a small utility function to partition an encoded image into its layers:
Part 1:
Part 2:
(Full code: github.com/jkoenig134/AdventOfCode...)