Cake Cutting
The year is 2035. Cake cutting is the last profession humans have left, everything else has been replaced by AI. Your job is to change that. Given a cake on STDIN and a series of cutting instructions, output the resulting cake, with slices separated.
You will be given a cake followed by a series of instructions in either the form x=…
or y=…
. The coordinates are 0 indexed for the spaces in between that row or column. For example, x=0 would indicate that you need to slice between the 1st and 2nd columns of the cake.
Input: ### ### ### x=0 y=0 First step: (x=0) # ## # ## # ## Second step: (y=0) # ## # ## # ##
The cake is separated after each cut. If a cut lands between two slices, that indicates that the customer wants more space between those two slices:
Input: | | |||_ x=0 y=0 y=0 Expected output: | | | ||_
Judge
(async function*(context: Context): Challenge { const testcases = `\ ### ### ### x=0 y=0 # ## # ## # ## HelpI'm trapped inacake cutting machine x=0 x=1 y=3 y=0 x=6 x=7 H elpI 'm t rapp ed i naca ke c utti ng m achi ne * * * y=0 y=2 * * * * * * *========== * * * :::::::::: * * * *========== ::::::::::::::::: ================= x=6 y=2 * * * * ========== * * * :::::::::: * * * * ========== ::::::: :::::::::: ======= ========== +----------+ | Happy | | Birthday | +----------+ x=1 x=3 x=5 x=7 x=9 x=11 x=13 x=15 x=17 +- - - - - - - - - -+ | H a p p y | | B i r t h d a y | +- - - - - - - - - -+ . . . . . . . . . . . . . . . . . . . . . .`.split('\n\n\n').map(t => t.split('\n\n')) as [string, string][]; for (let [input, output] of testcases) { yield (await context.run(input)).assertEquals(output); } return context.noFailures(); })
Example Code
const data = require('fs').readFileSync(0).toString(); let cake_grid = []; const instructions = []; for (const line of data.split('\n')) { if (line.startsWith('x=') || line.startsWith('y=')) { instructions.push(line); } else { cake_grid.push([...line]); } } let cake_width = cake_grid[0].length; for (const instruction of instructions) { const instruction_type = instruction[0]; const instruction_data = parseInt(instruction.slice(2)); if (instruction_type === 'y') { cake_grid.splice(instruction_data + 1, 0, [...' '.repeat(cake_width)]) } if (instruction_type === 'x') { for (const row of cake_grid) { row.splice(instruction_data + 1, 0, ' ') } cake_width += 1; } } for (const row of cake_grid) { console.log(row.join('')) }