From 40272006063da2cefa57780842400a54ab469da7 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Pires Date: Sun, 10 Dec 2023 01:35:55 +0100 Subject: [PATCH] feat: Day#5 Response --- src/Day5/day5.test.ts | 21 ++++++++++ src/Day5/day5.ts | 90 +++++++++++++++++++++++++++++++++++++++++ src/Day5/instruction.md | 42 +++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 src/Day5/day5.test.ts create mode 100644 src/Day5/day5.ts create mode 100644 src/Day5/instruction.md diff --git a/src/Day5/day5.test.ts b/src/Day5/day5.test.ts new file mode 100644 index 0000000..8449a9e --- /dev/null +++ b/src/Day5/day5.test.ts @@ -0,0 +1,21 @@ +import { cyberReindeer } from './day5'; + +describe('Day#5', () => { + test('Day#5 - Should output exactly the same path', () => { + const road = 'S..|...|..'; + const time = 10; // units of time + + expect(cyberReindeer(road, time)).toStrictEqual([ + 'S..|...|..', // initial state + '.S.|...|..', // sled advances on the road + '..S|...|..', // sled advances on the road + '..S|...|..', // sled stops at the barrier + '..S|...|..', // sled stops at the barrier + '...S...*..', // barrier opens, sled advances + '...*S..*..', // sled advances on the road + '...*.S.*..', // sled advances on the road + '...*..S*..', // sled advances on the road + '...*...S..', // passes through the open barrier + ]); + }); +}); diff --git a/src/Day5/day5.ts b/src/Day5/day5.ts new file mode 100644 index 0000000..c3288c9 --- /dev/null +++ b/src/Day5/day5.ts @@ -0,0 +1,90 @@ +export const cyberReindeer = (road: string, time = 0): string[] => { + // Define const + const SANTA = 'S'; + const ROAD = '.'; + const CLOSED_BARRIER = '|'; + const OPENED_BARRIER = '*'; + const BARRIER_OPENED_AT = 5; + + // We store the santa's journey in an array of strings like the expected output + const santaJourney: string[] = [road]; + + // We store the previous item + // Time unit 1: 'S..|...|..' + // Time unit 2: => Time unit 1: '.S.|...|..' + // ... + const lastJourneyItem = santaJourney[santaJourney.length - 1]; + + // destructure the previous item + // Usefull to find next or previous road state . | * + // Time unit 1: ['S','.','.','|','.','.','.','|','.','.'] + // Time unit 2: =>['.','S','.','|','.','.','.','|','.','.'] + // ... + const [...travelArray] = lastJourneyItem; + + // By default the previous road state is a Road + let previousJourneyStep = ROAD; + + // We save a flag if Santa has just passed a barrier to be able + // to replace the old item with an open barrier + // Time unit 5: ..S|...|.. + // Time unit 5: ...S...|.. + // Time unit 6: => ...*S..|.. + let previousItemIsABarrier = false; + + // Because we have to iteration on the santa path we have to store the currentIteration + // it will be used to leave the while loop + let currentIteration = 1; + + // Start the loop + while (currentIteration < time) { + // Get the current santa position + const santaPosition = travelArray.indexOf(SANTA); + + // Get the nextItem. It sould be one of: . | * + const nextItem = travelArray[santaPosition + 1]; + // Because at the start there is no previous we set to a ROAD element + previousJourneyStep = santaPosition !== 0 ? nextItem : ROAD; + + // If santa hit a barrier and all barriers are closed + if (CLOSED_BARRIER === nextItem && currentIteration < BARRIER_OPENED_AT) { + // Push a new time unit with the same path as the current path + santaJourney.push(santaJourney[santaPosition]); + } else { + // If santa is passing through a barrier + if ([CLOSED_BARRIER, OPENED_BARRIER].includes(previousJourneyStep)) { + // Set the current position to a road element + travelArray[santaPosition] = ROAD; + // Set a flag for next iteration + previousItemIsABarrier = true; + } else { + // If the barrier flag is true replace it by an OPENED_BARRIER or by a ROAD + travelArray[santaPosition] = previousItemIsABarrier ? OPENED_BARRIER : ROAD; + // Remove the flag + previousItemIsABarrier = false; + } + + // Move sante to the next position + travelArray[santaPosition + 1] = SANTA; + + // Create a new path as string + let newTravel = travelArray.join(''); + + // If the unit time is greater than or equal to BARRIER_OPENED_AT, + // we must replace all CLOSED_BARRIER with the OPENED_BARRIER string + // but | is a reserved char on regex but I can't use the constant with the escape character + // I use the pipe directly in the regex then I use the pipe directly in the regex + if (currentIteration >= BARRIER_OPENED_AT) { + newTravel = newTravel.replace(new RegExp(/\|/g), OPENED_BARRIER); + } + + // Push the new path + santaJourney.push(newTravel); + } + + // Increment the currentIteration var + currentIteration = currentIteration + 1; + } + + return santaJourney; +}; diff --git a/src/Day5/instruction.md b/src/Day5/instruction.md new file mode 100644 index 0000000..db1ea09 --- /dev/null +++ b/src/Day5/instruction.md @@ -0,0 +1,42 @@ +**Medium** + +Santa 🎅 is testing his new electric sled, the CyberReindeer, on a North Pole road. The road is represented by a string of characters, where: + +. = Road +S = Santa's Sled +* = Open barrier +| = Closed barrier +Example of a road: S...|....|..... + +Each unit of time, the sled moves one position to the right. If it encounters a closed barrier, it stops until the barrier opens. If it is open, it goes through directly. + +All barriers start closed, but after 5 units of time, they all open forever. + +Create a function that simulates the sled's movement for a given time and returns an array of strings representing the state of the road at each unit of time: + +``` +const road = 'S..|...|..' +const time = 10 // units of time +const result = cyberReindeer(road, time) + +/* -> result: +[ + 'S..|...|..', // initial state + '.S.|...|..', // sled advances on the road + '..S|...|..', // sled advances on the road + '..S|...|..', // sled stops at the barrier + '..S|...|..', // sled stops at the barrier + '...S...*..', // barrier opens, sled advances + '...*S..*..', // sled advances on the road + '...*.S.*..', // sled advances on the road + '...*..S*..', // sled advances on the road + '...*...S..', // passes through the open barrier +] +*/ +``` + +The result is an **array where each element shows the road at each unit of time.** + +Take into account that **if the sled is in the same position as a barrier, then it takes its place in the array.** + +The elves were **inspired by this Code Wars challenge.**