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

Show parent comment

1

u/wzkx Dec 17 '18

OK, all is automatic now :)

use std::io::{BufRead,BufReader}; // lines() in BufRead
use std::collections::HashMap;
type U=usize;

fn exec( cmd:U, ops:&[U], rbf:&Vec<U> ) -> Vec<U>:
  let mut r = rbf.clone();
  let (a,b,c) = (ops[0],ops[1],ops[2]);
  match cmd:
    100 => /* addr ra+rb->rc */ { r[c] = r[a] + r[b]; },
    101 => /* addi ra+ b->rc */ { r[c] = r[a] +   b;  },
    102 => /* mulr ra*rb->rc */ { r[c] = r[a] * r[b]; },
    103 => /* muli ra* b->rc */ { r[c] = r[a] *   b;  },
    104 => /* borr ra|rb->rc */ { r[c] = r[a] | r[b]; },
    105 => /* bori ra| b->rc */ { r[c] = r[a] |   b;  },
    106 => /* banr ra&rb->rc */ { r[c] = r[a] & r[b]; },
    107 => /* bani ra& b->rc */ { r[c] = r[a] &   b;  },
    108 => /* setr ra   ->rc */ { r[c] = r[a]; },
    109 => /* seti  a   ->rc */ { r[c] =   a;  },
    110 => /* gtir  a>rb->rc */ { r[c] = if   a  >  r[b] {1} else {0}; },
    111 => /* gtri ra> b->rc */ { r[c] = if r[a] >    b  {1} else {0}; },
    112 => /* gtrr ra>rb->rc */ { r[c] = if r[a] >  r[b] {1} else {0}; },
    113 => /* eqir  a=rb->rc */ { r[c] = if   a  == r[b] {1} else {0}; },
    114 => /* eqri ra= b->rc */ { r[c] = if r[a] ==   b  {1} else {0}; },
    115 => /* eqrr ra=rb->rc */ { r[c] = if r[a] == r[b] {1} else {0}; }, _ => {}
  /* return */ r

fn main():
  let file = std::fs::File::open( "16.dat" ).unwrap();
  let mut d:Vec<(String,String,String)> = vec![]; // data
  let mut t:(String,String,String) = (String::new(),String::new(),String::new());
  let mut b = false; // 'before' is done, command is expected
  let mut p:Vec<String> = vec![]; // prorgam for part 2
  for optline in BufReader::new(file).lines():
    let line = optline.unwrap();
    if line.starts_with("Before"):
      t.0 = line[9..line.len()-1].to_string().replace(",","");
      b = true;
    else if line.starts_with("After"):
      t.2 = line[9..line.len()-1].to_string().replace(",","");
      d.push(t.clone());
      b = false;
    else if b:
      t.1 = line[..].to_string();
    else if line.len()>6:
      p.push( line );

  let mut dbl: HashMap<(U,Vec<U>),U> = HashMap::new(); // n, ops -> cmd
  let mut o = 0; // output, part 1
  for (b,c,a) in d: // (before, command, after)
    let rbf: Vec<U> = b.split_whitespace().filter_map( |x| x.parse().ok() ).collect();
    let raf: Vec<U> = a.split_whitespace().filter_map( |x| x.parse().ok() ).collect();
    let cmd: Vec<U> = c.split_whitespace().filter_map( |x| x.parse().ok() ).collect();
    let mut n = 0; // how many matched the result
    let mut cand: Vec<U> = vec![]; // candidates
    for i in 100..=115: // virtual op codes
      let res = exec( i, &cmd[1..], &rbf );
      if raf == res:
        cand.push( i );
        n += 1;
    let key=(n,cand);
    if dbl.contains_key( &key ):
      if *dbl.get( &key ).unwrap() != cmd[0] { panic!("not ready for that"); }
    else:
      dbl.insert( key, cmd[0] );
    if n>=3:
      o += 1;
  println!( "{}", o ); // part 1

  // analyse - build a map of real ops to virtual ops
  let mut op2vop = vec![0;16]; // maps op --> virtual op; will be the result of analysis
  let mut dd: Vec<(U,Vec<U>,U)> = dbl.iter().map( |(k,v)| (k.0,k.1.clone(),*v) ).collect();
  while dd.len()>0:
    dd.sort();
    let (n,vops,op) = dd.remove(0);
    if n!=1 { panic!("not ready for that"); }
    let vop = vops[0];
    op2vop[ op ] = vop;
    while let Some(p) = dd.iter().position( |x| x.2==op ):
      dd.remove( p );
    for i in 0..dd.len():
      if let Some(p) = dd[i].1.iter().position( |&x| x==vop ):
        dd[i].0 -= 1;
        dd[i].1.remove( p );
  // execute
  let mut r = vec![0;4];
  for c in p:
    let cmd: Vec<U> = c.split_whitespace().filter_map( |x| x.parse().ok() ).collect();
    let res = exec( op2vop[cmd[0]], &cmd[1..], &r );
    r = res;
  println!( "{}", r[0] ); // part 2