blob: 835b675ccc74171e5f58f87d43cc6246f551ff55 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
#!/usr/local/bin/node
//
// See ../README.md
//
//
// Run as: node ch-2.js -s SIZE < input-file
//
const NR_OF_LETTERS = 26
//
// Parse input
//
const argv = require ('yargs')
. option ('s', {
type: 'number',
})
. demandOption ('s')
. argv;
const size = argv . s
//
// Iterate over the input
//
require ('readline')
. createInterface ({input: process . stdin})
. on ('line', _ => {
let sum = 0
let sections = _ . length / size
//
// Iterate over the positions
//
for (let i = 0; i < size; i ++) {
//
// Count the number of zeros in a specific position
//
let zeros = 0
for (let j = 0; j < sections; j ++) {
let index = j * size + i;
if (_ . substring (index, index + 1) == "0") {
zeros ++
}
}
//
// Calculate the ones
//
let ones = sections - zeros
//
// Add the minimum of the zeros and ones
//
sum += zeros < ones ? zeros : ones
}
console . log (sum)
})
;
|