Skip to content

Instantly share code, notes, and snippets.

@dishbreak
Created April 11, 2024 22:37
Show Gist options
  • Save dishbreak/586791603e77433284ed1541ce7ebec9 to your computer and use it in GitHub Desktop.
Save dishbreak/586791603e77433284ed1541ce7ebec9 to your computer and use it in GitHub Desktop.
Advent of Code 2015 Day 15 in Haskell
import Data.List
-- Is this good haskell? I've got no idea.
-- Saving this solution because I didn't do it in go.
combos = [[a, b, c, d] | a <- [1..100], b <- [1..100], c <- [1..100], d <- [1..100], a+b+c+d == 100]
ingredients = [[2, 0, -2, 0],[0, 5, -3, 0],[0, 0, 5, -1],[0, -1, 0, 5]]
matches = [zip ingredients combo | combo <- combos ]
mults = [ [ map (\y -> y * snd x) (fst x) | x <- match ] | match <- matches ]
-- factors = [ zipWith (+) (mult !! 0) (mult !! 1) | mult <- mults ]
factors = [map sum (transpose mult) | mult <- mults]
all_pos :: [Integer] -> Bool
all_pos [] = True
all_pos (x:xs) | x <= 0 = False
| otherwise = all_pos xs
all_mult :: [Integer] -> Integer
all_mult [] = 0
all_mult [x] = x
all_mult (x:xs) = x * all_mult xs
scores = [all_mult x | x <- factors, all_pos x]
icals = [3,3,8,8]
candidates = [ c | c <- combos, sum (zipWith (*) icals c) <= 500]
matches2 = [ zip ingredients c | c <- candidates]
mults2 = [ [ map (\y -> y * snd x) (fst x) | x <- match] | match <- matches2]
factors2 = [map sum (transpose m) | m <- mults2]
score2 = [all_mult x | x <-factors2, all_pos x ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment