Published
Edited
Dec 9, 2018
Insert cell
Insert cell
{ // 6.1
const P = inp6.map(parse6)
const is_infinite = p => p.x == min(P, p => p.x).value.x || p.y == min(P, p=> p.y).value.y
|| p.x == max(P, p => p.x).value.x || p.y == max(P, p=> p.y).value.y
// TODO improve is_infinite
return P.filter(is_infinite)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function pipe (A) {
return g => g ? A : pipe(g(A))
}
Insert cell
range = (n, fn = (_, i) => i) => [... new Array(n)].map(fn)
Insert cell
uniq = L => [...new Set(L)]
Insert cell
[1,2,3].sort((a, b) => b - a)
Insert cell
{ // 4.2
const A = mutable A4
const ids = mutable ids4
const time_slept_by_min = (id) => (min) => A.filter(day => day[min] == id).length
const greatest_minute = (id) => max(range(60).map(time_slept_by_min(id)))
const result = max(ids.map(id => ({id: id, max: greatest_minute(id)})), o => o.max.value).value
return parseInt(result.id) * result.max.index
}
Insert cell
guesses = [174463]
Insert cell
max = (L, fn = (e => e)) => {
const idx = L.reduce((M, v, i) => M < 0 ? i : fn(L[M]) > fn(v) ? M : i, -1)
return {
index: idx,
value: L[idx]
}
}
Insert cell
min = (L, fn = (e => e)) => {
const idx = L.reduce((M, v, i) => M < 0 ? i : fn(L[M]) < fn(v) ? M : i, -1)
return {
index: idx,
value: L[idx]
}
}
Insert cell
max([1, 3, 5, 7], e=> -e)
Insert cell
mutable ids4 = []
Insert cell
mutable A4 = []
Insert cell
{ // 4.1
let curr = null
const acts = inp4.map(parse4).sort((a, b) => moment.duration(a.date - b.date).asMinutes())
const n_days = acts.map(a => a.id).filter((e) => e).length // (number of shift starts)
const A = range(n_days, () => range(60, () => false))
const ids = uniq(acts.map(a => a.id).filter((e) => e))
const guards = ids.reduce((acc, id) => (acc[id] = [], acc), {})
let last = null
const fill = (day, start, end, id) => {
for (let i = start; i < end; i++) A[day][i] = id
}
let day = -1
let guard = null
acts.forEach(a => {
if (a.id) {
day += 1
guard = a.id
}
if (a.sleeping) {
last = a.date.minutes()
}
if (a.waking) {
fill(day, last, a.date.minutes(), guard)
}
})
const time_slept_by_id = (id) => A.reduce((s, day) => s + day.filter(id1 => id == id1).length, 0)
const time_slept_by_min = (id) => (min) => A.filter(day => day[min] == id).length
const f = time_slept_by_id
const sleepiest = ids.sort((a, b) => f(b) - f(a))[0]
const g = time_slept_by_min(sleepiest)
const sleepy_time = range(60).sort((a, b) => g(b) - g(a))[0]
mutable A4 = A
mutable ids4 = ids
return parseInt(sleepiest) * sleepy_time
}
Insert cell
parse4 = s => {
const action = /Guard/.exec(s)
return {
date: moment(s, 'YYYY-MM-DD hh:mm'),
id: (/#(\d+)/.exec(s) || [])[1],
waking: /wake/.exec(s),
sleeping: /sleep/.exec(s),
shift: /begin/.exec(s)
}
}
Insert cell
Insert cell
Insert cell
{ //3.2
const A = [...Array(1000)].map(() => [...Array(1000)].map(() => 0))
const alloc_claim = c => {
let prealloced = false
for (let i=0; i < c.w; i++) {
for (let j=0; j < c.h; j++) {
prealloced = prealloced || A[i+c.x][j+c.y] > 1
A[i+c.x][j+c.y] += 1
}
}
return prealloced
}
inp3.map(parse3).map(alloc_claim)
return inp3.map(parse3).filter(c => !alloc_claim(c))[0].id
}
Insert cell
{ //3.1
const A = [...Array(1000)].map(() => [...Array(1000)].map(() => 0))
const alloc_claim = c => {
for (let i=0; i < c.w; i++) {
for (let j=0; j < c.h; j++) {
A[i+c.x][j+c.y] += 1
}
}
}
inp3.map(parse3).map(alloc_claim)
return A.reduce((acc, row) => acc + row.reduce((acc, v) => acc + (v > 1 ? 1 : 0), 0), 0)
}
Insert cell
parse3 = s => {
const L = s.split(' ')
const f = parseInt
return {
id: L[0].slice(1),
x: f(L[2].split(',')[0]),
y: f(L[2].split(',')[1].slice(0, -1)),
w: f(L[3].split('x')[0]),
h: f(L[3].split('x')[1])
}
}
Insert cell
Insert cell
{ // 2.2, such a lame solution
const L = parse_inp(inp2)
for (let i=0; i < 26; i++) {
const L2 = L.map(s => s.substring(0, i) + s.substring(i + 1, s.length))
const result = L2.filter((s, i) => L2.slice(i+1, L2.length).find(s1 => s == s1))[0]
if (result) return result
}
return 'nope'
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{ //1.2
const seen = new Set()
const L = inp1.split('\n').map(s => parseInt(s))
let acc = 0
while (true) {
for (const v of L) {
acc += v
if (seen.has(acc)) return acc
seen.add(acc)
}
}
}
Insert cell
inp1.split('\n').map(s => parseInt(s)).reduce((acc, v) => acc + v, 0) //1.1
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more