This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const STEAT_COUNTS = 8; | |
class Table { | |
constructor() { | |
this.first = new Seat(0); | |
this.seats = [this.first] | |
let preSeat = this.first; | |
for (let i = 1; i < STEAT_COUNTS; i++) { | |
const s = new Seat(i); | |
this.seats.push(s); | |
preSeat.setNextSeat(s); | |
preSeat = s; | |
} | |
preSeat.setNextSeat(this.first); | |
} | |
toStatusString() { | |
let s = ''; | |
this.seats.forEach((seat) => { | |
s += seat.status; | |
}) | |
return s; | |
} | |
toString() { | |
let s = ''; | |
this.seats.forEach((seat) => { | |
s += seat.isEmpty() ? '0' : '1'; | |
}) | |
return s; | |
} | |
// | |
transit(count) { | |
// 状態遷移させる | |
this.seats.forEach((seat) => { | |
seat.transit(); | |
}) | |
// 空いている席の最初のインデックスを取得 | |
const emptySeats = this.findEmptySeats(count); | |
if (!emptySeats || emptySeats.length == 0) { | |
return false; | |
} | |
if (emptySeats.length < count) { | |
return false; | |
} | |
// 席を埋める | |
for (let i = 0; i < count; i++) { | |
emptySeats[i].take(); | |
} | |
return true; | |
} | |
findEmptySeats(count) { | |
let emptySeats = []; | |
for (let searchStart = 0; searchStart < STEAT_COUNTS; searchStart++) { | |
emptySeats = this._findEmptySeats(searchStart); | |
if (!emptySeats || emptySeats.length == 0) { | |
continue; | |
} | |
if (emptySeats.length < count) { | |
continue; | |
} | |
return emptySeats; | |
} | |
return emptySeats; | |
} | |
_findEmptySeats(searchStart) { | |
const startIndex = this.findEmptySeatStartIndex(searchStart); | |
if (startIndex == -1) { | |
return []; | |
} | |
const emptySeats = []; | |
const start = this.seats[startIndex]; | |
let seat = start; | |
while (seat.isEmpty()) { | |
let next = seat.getNextSeat(); | |
emptySeats.push(seat); | |
if (next === start) { | |
return emptySeats;//全部空いている | |
} | |
seat = next; | |
} | |
return emptySeats; | |
} | |
findEmptySeatStartIndex(searchStart) { | |
for (let i = searchStart; i < STEAT_COUNTS; i++) { | |
if (this.seats[i].isEmpty()) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
} | |
const STATUS_EMTPY = 0; | |
const STATUS_WAITING = 1; | |
const STATUS_EATING = 2; | |
const STATUS_REST = 3; | |
class Seat { | |
constructor(number) { | |
this.empty = true; | |
this.number = number; | |
this.status = 0; | |
this._nextSeat = null; | |
} | |
getNextSeat() { | |
return this._nextSeat; | |
} | |
setNextSeat(seat) { | |
this._nextSeat = seat; | |
} | |
transit() { | |
if (this.status == STATUS_EMTPY) { | |
return; | |
} | |
if (this.status == STATUS_REST) { | |
this.status = STATUS_EMTPY; | |
} else { | |
this.status++; | |
} | |
} | |
take() { | |
this.status = STATUS_WAITING; | |
} | |
get getStatus() { | |
return this.status; | |
} | |
isEmpty() { | |
return this.status == STATUS_EMTPY; | |
} | |
} | |
var assert = require('assert'); | |
function test(input, output) { | |
let table = new Table(); | |
let i = 0; | |
//console.log("input:" + input); | |
while (i != input.length) { | |
if (table.transit(input[i])) { | |
i++; | |
} | |
//console.log(table.toStatusString()); | |
} | |
assert.equal(table.toString(), output); | |
} | |
test("3316", "11111110") | |
test("3342153", "11111111") | |
test("8", "11111111") | |
test("232624", "11110110") | |
test("1", "10000000") | |
test("224673432", "11111000") | |
test("123464334", "11111110") | |
test("44372332", "11111111") | |
test("2344", "11111111") | |
test("6448476233", "11111100") | |
test("4345174644", "11111111") | |
test("77743", "11111110") | |
test("2136524281", "10000000") | |
test("344373", "11100000") | |
test("434226", "11111111") | |
test("7433223", "11111110") | |
test("2246534", "11110111") | |
test("634", "11111110") | |
test("2222", "11111100") | |
test("2442343238", "11111111") | |
test("7243344", "11111111") | |
test("26147234", "10111111") | |
test("34424", "10011111") | |
test("6334", "11011111") | |
test("3828342", "11011110") | |
test("4431", "11110000") | |
test("2843212125", "11111111") | |
test("333336482", "11000000") | |
test("374", "11110000") | |
test("4382333", "11100111") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment