r/adventofcode • • Dec 16 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 16 Solutions -🎄-

--- Day 16: Chronal Classification ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 16

Transcript:

The secret technique to beat today's puzzles is ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:39:03!

16 Upvotes

139 comments sorted by

View all comments

1

u/alcinos Dec 16 '18

Ocaml

Parsing was a bit annoying in Ocaml, but the rest is a breeze. Used Sets to keep track of possible values for each op-code, and set intersection/difference to remove the impossible values for each of them. Note that the input was crafted so that we didn't need any backtracking, so I didn't bother implementing one.

type instruction = {
  code : int;
  a : int;
  b : int;
  c : int;
};;
type sample = {
  before : int array;
  after : int array;
  instr : instruction;
};;

type op =
  | Mulr | Muli
  | Addr | Addi
  | Banr | Bani
  | Borr | Bori
  | Setr | Seti
  | Gtir | Gtri | Gtrr
  | Eqir | Eqri | Eqrr
  | UKN;;

let execr reg i f = reg.(i.c) <- (f reg.(i.a) reg.(i.b));;
let execi reg i f = reg.(i.c) <- (f reg.(i.a) i.b);;

let exec_comp reg i f a b = reg.(i.c) <- (if (f a b) then 1 else 0);;

let process reg i = function
  | Mulr -> execr reg i ( * );
  | Muli -> execi reg i ( * );
  | Addr -> execr reg i (+);
  | Addi -> execi reg i (+);
  | Banr -> execr reg i (land);
  | Bani -> execi reg i (land);
  | Borr -> execr reg i (lor);
  | Bori -> execi reg i (lor);
  | Setr -> reg.(i.c) <- reg.(i.a);
  | Seti -> reg.(i.c) <- i.a;
  | Gtir -> exec_comp reg i (>) i.a reg.(i.b);
  | Gtri -> exec_comp reg i (>) reg.(i.a) i.b;
  | Gtrr -> exec_comp reg i (>) reg.(i.a) reg.(i.b);
  | Eqir -> exec_comp reg i (==) i.a reg.(i.b);
  | Eqri -> exec_comp reg i (==) reg.(i.a) i.b ;
  | Eqrr -> exec_comp reg i (==) reg.(i.a) reg.(i.b);
  | UKN -> failwith "unknown op code";;

let all_ops =  Mulr :: Muli :: Addr :: Addi :: Banr :: Bani :: Borr :: Bori :: Setr :: Seti :: Gtir :: Gtri :: Gtrr :: Eqir :: Eqri :: Eqrr :: [];;

let make_instr co a b c = {code = co; a = a; b = b; c = c};;

let make_sample i1 i2 i3 i4 co a b c j1 j2 j3 j4 =
  let before = [|i1; i2; i3; i4|] and after = [|j1; j2; j3; j4|] in
  {before = before; after = after; instr = make_instr co a b c};;


let rec parse =  function () ->
try
  let l1 = read_line () in
  if l1.[0] != 'B' then failwith "end";
  let l2 = read_line () and l3 = read_line () and l4 = read_line () in
  let inp = l1 ^ "\n" ^ l2 ^  "\n" ^ l3 ^  "\n" ^ l4 ^ "\n" in
  let s = Scanf.sscanf inp "Before: [%d, %d, %d, %d]\n%d %d %d %d\nAfter:  [%d, %d, %d, %d]\n\n" make_sample in
  s :: (parse ());
with _ -> [] ;;

let l = parse ();;

let _ = read_line ();;

let rec parse_instr =  function () ->
try
  let l = read_line () in
  let s = Scanf.sscanf l "%d %d %d %d" make_instr in
  s :: (parse_instr ());
with _ -> [] ;;

let all_instr = parse_instr ();;

let matches s oper =
  let in_reg = Array.init 4 (fun i -> s.before.(i)); in
  process in_reg s.instr oper;
  s.after = in_reg;;

let result = List.fold_left (fun sum spl ->
    let match_count = List.fold_left (fun sum m -> sum + (if (matches spl m) then 1 else 0)) 0 all_ops in
    sum + (if match_count >= 3 then 1 else 0)
  ) 0 l ;;

Printf.printf "Part 1 = %d\n" result;;

module OpSet = Set.Make(
  struct
    let compare = Pervasives.compare
    type t = op
  end );;

let possible_ops = Array.init (List.length all_ops) (fun _-> OpSet.of_list all_ops) ;;

(* filter based on input *)
List.iter (fun spl ->
    let matching = List.filter (matches spl) all_ops in
    let possible = OpSet.of_list matching in
    possible_ops.(spl.instr.code) <- OpSet.inter possible_ops.(spl.instr.code) possible
  ) l;;

let true_ops = Array.make (List.length all_ops) UKN;;

(* filter based on already known operation. Note that this is greedy, in general we would need to backtrack if needed*)
while (Array.exists (fun o -> match o with |UKN->true |_->false) true_ops) do
  let affected = OpSet.of_seq (Seq.filter_map (fun ops ->
      if OpSet.cardinal ops == 1 then Some (OpSet.choose ops) else None)
      (Array.to_seq possible_ops)) in
  Array.iteri (fun i ops ->
      if (OpSet.cardinal ops) > 1 then
        possible_ops.(i) <- OpSet.diff ops affected
      else
        true_ops.(i) <- OpSet.choose ops) possible_ops;
done;;

let reg = Array.make 4 0;;


(* execute program *)
List.iter (fun i -> process reg i true_ops.(i.code)) all_instr;;

Printf.printf "Part 2 = %d\n" reg.(0);;