Advent of Code 2021 - Day 7

A giant whale has decided your submarine is its next meal, and it’s much faster than you are. There’s nowhere to run!

Day 7a

export const d7a = ({input = inputs.d7}: {input?: string}) => {
  const ns = ints(input)
  const costs = R.range(min(ns), max(ns)).map(pos =>
    sum(ns.map(n => Math.abs(n - pos))),
  )
  return min(costs)
}

Day 7a sample:

Day 7a mine:

Day 7b

export const d7b = ({input = inputs.d7}: {input?: string}) => {
  const ns = ints(input)
  // cost-per-distance lookup table
  const {cpd} = R.range(0, max(ns) - min(ns) + 1).reduce(
    ({cpd, rate}, d) => {
      cpd[d + 1] = cpd[d] + rate
      return {cpd, rate: rate + 1}
    },
    {cpd: [0], rate: 0},
  )
  cpd.shift() // avoids needing (cpd[d] || 0) in the reduction
  const costs = R.range(min(ns), max(ns)).map(pos =>
    sum(ns.map(n => cpd[Math.abs(n - pos)])),
  )
  return min(costs)
}

Day 7b sample:

Day 7b mine: