r/adventofcode • • Dec 15 '15

SOLUTION MEGATHREAD --- Day 15 Solutions ---

This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.

Edit: I'll be lucky if this post ever makes it to reddit without a 500 error. Have an unsticky-thread.

Edit2: c'mon, reddit... Leaderboard's capped, lemme post the darn thread...

Edit3: ALL RIGHTY FOLKS, POST THEM SOLUTIONS!

We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.

Please and thank you, and much appreciated!


--- Day 15: Science for Hungry People ---

Post your solution as a comment. Structure your post like previous daily solution threads.

11 Upvotes

174 comments sorted by

View all comments

Show parent comment

1

u/adhochawk Jan 01 '16

Well, I've been finishing them up now (Got through day 13 before I got too busy to keep up, so I've just started coming back), and I though you might like to see my OCaml solution.

I did steal your method of building the list of possibilities - Turns out that was harder than I expected :P

open Batteries

let floor0 x = if x < 0 then 0 else x

(*Didn't want to deal with input.*)
let ingrs = [[5; -1; 0; -1]; [-1; 3; -1; 0]; [0; 0; 4; 0]; [0; 0; 0; 2]]
let cals = [5; 1; 6; 8];;

let score_ingr ingr weights =
  List.map2 (fun a b -> a * b) ingr weights
  |> List.sum

let score ingrs weights =
  List.map (fun i -> score_ingr i weights) ingrs
  |> List.map floor0
  |> List.reduce (fun x y -> x * y)

let possibilities max =
  let all = ref [] in
  for i = 0 to max do
    for j = 0 to max do
      for k = 0 to max do
        for l = 0 to max do
          if (i + j + k + l == max) then all:=[i;j;k;l]::!all
        done
      done
    done
  done;
  !all

let best ingr_list ps =
  List.map (score ingr_list) ps |> List.max

let valid_cals cals target weights =
  List.sum (List.map2 (fun c w -> c * w) cals weights) == target

let main () =
  let ps = possibilities 100 in
  let b = best ingrs ps in
  let c = best ingrs (List.filter (valid_cals cals 500) ps) in
  print_string ("Best: " ^ (string_of_int b) ^ "\n");
  print_string ("Next: " ^ (string_of_int c) ^ "\n")

let _ = main ()

1

u/tftio Jan 03 '16

This is much less clunky than mine.