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!

32 Upvotes

387 comments sorted by

View all comments

Show parent comment

1

u/kn0where Dec 06 '18 edited Dec 06 '18

Nice variable names and modularity. I just banged it out.

// const input = `
// 124, 262
// 182, 343
// `
const cs = input.trim().split('\n').map(c=>c.split(', ').map(s=>parseInt(s)))
const run = (b0, b1) => {
  const ps = {}
  for(let y = b0[1]; y < b1[1]; ++y){
    for(let x = b0[0]; x < b1[0]; ++x){
      const cd = cs
        .map(c=> [c, Math.abs(c[0]-x) + Math.abs(c[1]-y)])
        .reduce((a, b)=>{
          if(a[1] < b[1]) return a
          if(b[1] < a[1]) return b
          return [[NaN,NaN], a[1]]
        })
      ps[cd[0]] = (ps[cd[0]] || 0) + 1
    }
  }
  return ps
}
const x_min = cs.reduce((a, c) => a[0] < c[0] ? a : c)[0]
const x_max = cs.reduce((a, c) => a[0] > c[0] ? a : c)[0]
const y_min = cs.reduce((a, c) => a[1] < c[1] ? a : c)[1]
const y_max = cs.reduce((a, c) => a[1] > c[1] ? a : c)[1]
const bb = run([x_min, y_min], [x_max, y_max])
const bb2 = run([x_min-1, y_min-1], [x_max+1, y_max+1])
const a = cs
  .filter(c=> bb[c] === bb2[c])
  .reduce((a,b)=> (bb[b] && (bb[a] < bb[b])) ? b : a)
console.log(bb[a])
const run2 = (b0, b1) => {
  let v = 0
  for(let y = b0[1]; y < b1[1]; ++y){
    for(let x = b0[0]; x < b1[0]; ++x){
      if(
        cs.map(c=> Math.abs(c[0]-x) + Math.abs(c[1]-y))
          .reduce((a, b)=> a + b)
        < 10000
      ){
        v += 1
      }
    }
  }
  return v
}
console.log(
  run2([x_min, y_min], [x_max, y_max])
)