Byte Heist Home Leaderboard
Join the Heist (with Github)
Solve View

Binary Squash

Given a list of integers, each on their own line, combine the ones adjacent ones where the binary representation does not share a one.

For example, given the list:

1
6
8
3
4

Would output:

15
7

Each number will be at most 2<sup>16</sup>

Judge

(async function*(context: Context): Challenge {
	for (const run of [0,1,2]) {
		const numbers = new Array(1000).fill(0).map(k=>
			Math.floor(Math.random()*(1<<16)) &
			Math.floor(Math.random()*(1<<16)) &
			Math.floor(Math.random()*(1<<16))
		);

		if (run === 2) {
			numbers[numbers.length - 1]&=~15;
		}
		
		yield (await context.run(
			numbers.join("\n")
		)).assertEquals(
			numbers.slice(1).reduce(
				([a,...b],c)=>
					(a&c)?[c,a,...b]:[c|a,...b],
				[numbers[0]]
			).reverse().join("\n")
		);
	}
	// Finally, the challenge is passed if no test cases failed
	return context.noFailures();
})

Example Code

process.stdin.on('data',d=>{
	let numbers = (""+d).split("\n").map(i=>+i);
	console.log(numbers.slice(1).reduce(
		([a,...b],c)=>
			(a&c)?[c,a,...b]:[c|a,...b],
		[numbers[0]]
	).reverse().join("\n"))
})