「ゆかた」ゲットチャレンジ!
JavaScript Closure Pattern
(function(stdin) {
// Define Function
const Temperature = () => {
let temp = 0;
return function(t) {
temp = (temp > 0) ? temp - 1 : temp;
temp += t;
return temp;
};
};
// Declare Variable
const inputs = stdin.toString().trim().split('\n');
const map = new Map(Array.from(inputs.slice(0, inputs.shift()),
l => l.split(' ')));
// Main Procedure
const result = (function(log) {
const temp = Temperature();
return Array.from(Array(24).fill(0),
(v, key) => ({in : 5, out : 3})[log.get(key.toString())] || v
)
.map(temp)
.map(v => (v > 0) ? 2 : 1)
.reduce((a, b) => a + b);
}(map));
// Display
console.log(result);
}(require('fs').readFileSync('/dev/stdin', 'utf8')));
JavaScript Object Pattern
(stdin => {
// Define Function
const Thermometer = (settings) => {
let temperature = 0;
return (action) => {
if (temperature > 0) temperature += settings.default;
return (temperature += (settings[action] || 0));
};
};
class Refrigerator {
constructor(actions, settings) {
this.log = this.create(new Map(actions), Thermometer(settings));
}
create(actions, thermometer) {
return new Map(
Array.from([...Array(24).keys()], key =>
[key, thermometer(actions.get(key.toString()))])
);
}
getCharges() {
return Array.from(this.log.values(), v =>
(v === 0) ? 1 : 2)
.reduce((a, b) => a + b);
}
}
// Declare Variable
const inputs = stdin.toString().trim().split('\n');
const matrix = Array.from(inputs.slice(0, inputs.shift()),
l => l.split(' '));
// Main Procedure
const result = (lines => {
let settings = {in: 5, out: 3, default: -1};
return new Refrigerator(lines, settings).getCharges();
})(matrix);
// Display
console.log(result);
})(require('fs').readFileSync('/dev/stdin', 'utf8'));