|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import * as _ from 'lodash'; |
| 4 | + |
| 5 | +class Generator { |
| 6 | + public type: 'generator' = 'generator'; |
| 7 | + constructor (public name: string) {} |
| 8 | + |
| 9 | + print () { |
| 10 | + return _.padEnd(this.name.toUpperCase(), 3) |
| 11 | + } |
| 12 | + |
| 13 | + clone () { |
| 14 | + return new Generator(this.name); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +class Microchip { |
| 19 | + public type: 'microchip' = 'microchip'; |
| 20 | + constructor (public name: string) {} |
| 21 | + |
| 22 | + print () { |
| 23 | + return _.padEnd(this.name.toLowerCase(), 3) |
| 24 | + } |
| 25 | + |
| 26 | + clone () { |
| 27 | + return new Microchip(this.name); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +class Floor { |
| 32 | + public generators: Generator[] = []; |
| 33 | + public microchips: Microchip[] = []; |
| 34 | + public elevator: Elevator = null; |
| 35 | + |
| 36 | + constructor (public number: number) {} |
| 37 | + |
| 38 | + print () { |
| 39 | + let toPrint = `| ${_.padEnd('Floor ' + this.number, 8)} ${this.elevator ? 'E' : ' '} | `; |
| 40 | + |
| 41 | + let gens = this.generators.map(item => item.print()).join(' | '); |
| 42 | + let micros = this.microchips.map(item => item.print()).join(' | '); |
| 43 | + toPrint += gens + (gens && micros ? ' | ' : '') + micros + ' | '; |
| 44 | + |
| 45 | + if (this.elevator) { |
| 46 | + this.elevator.slots.map(item => { |
| 47 | + if (!item) { |
| 48 | + return; |
| 49 | + } |
| 50 | + toPrint += ' | ' + item.print(); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + console.log(toPrint); |
| 55 | + } |
| 56 | + |
| 57 | + add (...items: (Generator | Microchip)[]) { |
| 58 | + items.map(item => { |
| 59 | + if (item instanceof Generator) { |
| 60 | + this.generators.push(item); |
| 61 | + } |
| 62 | + else { |
| 63 | + this.microchips.push(item); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + clone () { |
| 69 | + let floor = new Floor(this.number); |
| 70 | + floor.generators = this.generators.map(gen => gen.clone()); |
| 71 | + floor.microchips = this.microchips.map(gen => gen.clone()); |
| 72 | + floor.elevator = this.elevator ? this.elevator.clone(floor) : null; |
| 73 | + |
| 74 | + return floor; |
| 75 | + } |
| 76 | + |
| 77 | + isEquivalent (floor: Floor) { |
| 78 | + if (this.generators.length !== floor.generators.length) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + for (let i = 0; i < this.generators.length; i++) { |
| 82 | + if (this.generators[i].name !== floor.generators[i].name) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + } |
| 86 | + if (this.microchips.length !== floor.microchips.length) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + for (let i = 0; i < this.microchips.length; i++) { |
| 90 | + if (this.microchips[i].name !== floor.microchips[i].name) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (!this.elevator && floor.elevator) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + if (this.elevator && !this.elevator.isEquivalent(floor.elevator)) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +class Building { |
| 108 | + public floors: Floor[] = []; |
| 109 | + |
| 110 | + constructor (height: number | Building) { |
| 111 | + if (typeof height === 'number') { |
| 112 | + for (let i = 1; i <= height; i++) { |
| 113 | + this.floors.push(new Floor(i)); |
| 114 | + } |
| 115 | + } |
| 116 | + else { |
| 117 | + this.floors = height.floors.map(floor => floor.clone()); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + getFloor (number: number) { |
| 122 | + return this.floors[number]; |
| 123 | + } |
| 124 | + |
| 125 | + print () { |
| 126 | + for (let i = this.floors.length - 1; i >= 0; --i) { |
| 127 | + this.floors[i].print(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + clone () { |
| 132 | + return new Building(this); |
| 133 | + } |
| 134 | + |
| 135 | + isEquivalent (bldg: Building) { |
| 136 | + |
| 137 | + let a = [], b = []; |
| 138 | + |
| 139 | + for(let i = 0; i < this.floors.length; i++) { |
| 140 | + if (!this.floors[i].isEquivalent(bldg.floors[i])) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + } |
| 144 | + return true; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +class Elevator { |
| 149 | + public slots: [ Generator | Microchip, Generator | Microchip ] = [ null, null ]; |
| 150 | + private floor: Floor; |
| 151 | + |
| 152 | + constructor (floor: Floor) { |
| 153 | + this.move(floor); |
| 154 | + } |
| 155 | + |
| 156 | + move (floor: Floor) { |
| 157 | + if (this.floor) { |
| 158 | + this.floor.elevator = null; |
| 159 | + } |
| 160 | + this.floor = floor; |
| 161 | + this.floor.elevator = this; |
| 162 | + } |
| 163 | + |
| 164 | + clone (floor) { |
| 165 | + let elevator = new Elevator(floor); |
| 166 | + elevator.slots = this.slots.map(slot => slot && slot.clone()); |
| 167 | + return elevator; |
| 168 | + } |
| 169 | + |
| 170 | + isEquivalent (elevator: Elevator) { |
| 171 | + if (!elevator) { |
| 172 | + return false; |
| 173 | + } |
| 174 | + if (this.floor.number !== elevator.floor.number) { |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + function isEquivalent (a, b) { |
| 179 | + if (!a && !b) { |
| 180 | + return true; |
| 181 | + } |
| 182 | + if ((a && !b) || (!a && b)) { |
| 183 | + return false; |
| 184 | + } |
| 185 | + return a.name === b.name && a.type === b.type; |
| 186 | + } |
| 187 | + |
| 188 | + if ((isEquivalent(elevator.slots[0], this.slots[0]) && isEquivalent(elevator.slots[1], this.slots[1])) || |
| 189 | + (isEquivalent(elevator.slots[1], this.slots[0]) && isEquivalent(elevator.slots[0], this.slots[1]))) { |
| 190 | + return true; |
| 191 | + } |
| 192 | + return false; |
| 193 | + |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +function createPair (name: string) { |
| 198 | + return { |
| 199 | + microchip: new Microchip(name), |
| 200 | + generator: new Generator(name) |
| 201 | + }; |
| 202 | +} |
| 203 | + |
| 204 | +module.exports = [ |
| 205 | + // Challenge 1 |
| 206 | + (input) => { |
| 207 | + let building = new Building(4); |
| 208 | + let elevator = new Elevator(building.getFloor(0)); |
| 209 | + |
| 210 | + let a = createPair('a'); |
| 211 | + let b = createPair('b'); |
| 212 | + let c = createPair('c'); |
| 213 | + let d = createPair('d'); |
| 214 | + let e = createPair('e'); |
| 215 | + |
| 216 | + let first = building.getFloor(0); |
| 217 | + first.add(a.generator, b.generator, b.microchip, c.generator, d.generator, d.microchip, e.generator, e.microchip); |
| 218 | + let second = building.getFloor(1); |
| 219 | + second.add(a.microchip, c.microchip); |
| 220 | + |
| 221 | + building.print(); |
| 222 | + |
| 223 | + let building2 = building.clone(); |
| 224 | + console.log(building2.isEquivalent(building)); |
| 225 | + }, |
| 226 | + |
| 227 | + // Challenge 2 |
| 228 | + input => { |
| 229 | + |
| 230 | + } |
| 231 | + |
| 232 | +]; |
0 commit comments