"use strict";

const fs = require("fs");
const R  = require("ramda");

const parse = (s) => {
  if (! s) return false;
  const params = s.replace(/\s*[a-zA-Z]+=\</g, "").split(",").map((n) => parseInt(n));
  return { x: params[0], y: params[1], z: params[2] };
};

const input = fs.readFileSync("./20_input.txt").toString().split("\n");

const particles = input.map((s, idx) => {
  const fields = s.replace(/^\s+|\s+$/g, "").split(">,");
  const pos    = parse(fields[0]);
  const vel    = parse(fields[1]);
  const acc    = parse(fields[2]);

  return {
    id: idx,
    x:  pos.x,
    y:  pos.y,
    z:  pos.z,
    vx: vel.x,
    vy: vel.y,
    vz: vel.z,
    ax: acc.x,
    ay: acc.y,
    az: acc.z
  }
});

let min_p   = { ax: Infinity, ay: Infinity, az: Infinity };
let min_acc = Infinity;

particles.map((p) => {
  const acc = Math.abs(p.ax) + Math.abs(p.ay) + Math.abs(p.az);

  if (acc < min_acc) {
    min_p   = p;
    min_acc = acc;
  }
  else if (acc == min_acc) {
    if ( (Math.abs(p.x) + Math.abs(p.y) + Math.abs(p.z)) 
       < (Math.abs(min_p.x) + Math.abs(min_p.y) + Math.abs(min_p.z)) ) {
      min_p = p;
    }
  }
});

console.log(min_p.id)
// answer: 243

