r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-

--- Day 6: Chronal Coordinates ---


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 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


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 0:26:52!

30 Upvotes

387 comments sorted by

View all comments

Show parent comment

1

u/wzkx Dec 06 '18 edited Dec 06 '18

Finally, it's about 40 non-empty lines — one can see the whole program w/o scrolling.

use std::io::{BufRead,BufReader}; // lines() is in BufRead

type U = usize;  // it's easier to have one type everywhere
const E:U = 999; // EMPTY CELL MARK

fn dist( x1: U, y1: U, x2: U, y2: U ) -> U: // Manhattan distance
  (if x1>=x2 {x1-x2} else {x2-x1}) + (if y1>=y2 {y1-y2} else {y2-y1})

fn main():
  let reader = BufReader::new( std::fs::File::open( "06.dat" ).unwrap() );
  let mut v: Vec<(U,U)> = vec![]; // (x,y)
  let (mut nx, mut ny) = (0,0);
  for optline in reader.lines():
    let xy: Vec<U> = optline.unwrap().split(", ").map( |x| x.parse().unwrap() ).collect();
    let (x,y) = (xy[1],xy[0]); // x,y in math but row,column in IT
    v.push( (x, y) );
    if x>nx { nx=x; } if y>ny { ny=y; }
  let n = v.len();
  let mut c: Vec<(U,U)> = (0..n).map( |i| (0,i) ).collect(); // (count,v-idx)

  nx += 2; ny += 2;
  let mut m: Vec<Vec<U>> = vec![vec![E;ny];nx]; // I didn't know Rust could do this.
                                                // But I tried and it could.
  for i in 0..nx:
    for j in 0..ny:
      let mut dd: Vec<(U,U)> = (0..n).map( |k| (dist( i,j, v[k].0,v[k].1 ), k) ).collect();
      dd.sort();
      if dd[0].0 != dd[1].0: // not equidistant, so let's register it
        m[i][j] = dd[0].1;
        c[dd[0].1].0 += 1;

  for i in 0..nx: // remove those going out behind borders
    if m[i][0   ]!=E { c[m[i][0   ]].0 = 0; }
    if m[i][ny-1]!=E { c[m[i][ny-1]].0 = 0; }
  for j in 0..ny:
    if m[0   ][j]!=E { c[m[0   ][j]].0 = 0; }
    if m[nx-1][j]!=E { c[m[nx-1][j]].0 = 0; }

  c.sort();
  println!( "{}", c[c.len()-1].0 );

  const MAXDIST: U = 10000;
  let mut r = 0;
  for i in 0..nx:
    for j in 0..ny:
      if (0..n).map( |k| dist( i,j, v[k].0,v[k].1 ) ).sum::<U>() < MAXDIST:
        r += 1;
  println!( "{}", r );