Advent of Code 2021 - Day 7

My first AOC problem with LFE.

(defmodule day7
    (export (part1 0) (part2 0)))

(defun part1 () 
    (lists:min
        (lists:map 
            (lambda (x) (fuel_consumption x (load_data))) 
            (min_max (load_data)))))

(defun part2 () 
    (lists:min
        (lists:map 
            (lambda (x) (fuel_consumption2 x (load_data))) 
            (min_max (load_data)))))

(defun min_max (list) 
    (lists:seq (lists:min list) (lists:max list)))

(defun fuel_consumption (pos list)
    (lists:sum
        (lists:map 
            (lambda(x) (abs (- pos x)))
            list)))

(defun fuel_consumption2 (pos list) 
    (lists:sum
        (lists:map 
            (lambda(x) (cost (abs (- pos x))))
            list)))

(defun cost (x) (* x (/ (+ x 1) 2)))

(defun load_data ()
    (lists:map
        (lambda (x) (element 1 (string:to_integer x)))
        (string:split
            (string:trim
                (element 2 (file:read_file "input.txt"))
            ) "," 'all)))

I feel like I have been assimilated by a functional cyborg :slight_smile:

5 Likes