Task 01
This commit is contained in:
parent
deee2d5c53
commit
a4099b9139
File diff suppressed because it is too large
Load Diff
|
@ -2,7 +2,11 @@ import 'mocha';
|
|||
import assert from 'assert';
|
||||
import { readFileSync } from 'fs';
|
||||
|
||||
let example: number[] = [];
|
||||
let example: string =
|
||||
`1-3 a: abcde
|
||||
1-3 b: cdefg
|
||||
2-9 c: ccccccccc`
|
||||
;
|
||||
let expect01: number = 0;
|
||||
let expect02: number = 0;
|
||||
|
||||
|
@ -13,15 +17,36 @@ let input = readFileSync('./input/2020-day-02.txt', 'utf-8')
|
|||
let tasksResults: any[] = ['N/A', 'N/A'];
|
||||
|
||||
describe('Day 02', () => {
|
||||
|
||||
describe('task01', () => {
|
||||
describe('parse', () => {
|
||||
let rules: PassEntry[];
|
||||
before(() => {
|
||||
rules = getData(example);
|
||||
})
|
||||
it('matches a line', () => {
|
||||
assert.strictEqual(rules[0].min, 1);
|
||||
assert.strictEqual(rules[0].max, 3);
|
||||
assert.strictEqual(rules[0].letter, 'a');
|
||||
assert.strictEqual(rules[0].pass, 'abcde');
|
||||
});
|
||||
it('makes a table', () => {
|
||||
let table = mkTable(rules[0]);
|
||||
assert.strictEqual(table.a, 1);
|
||||
})
|
||||
it('validate rules', () => {
|
||||
let valid = checkRulesTask01(rules);
|
||||
assert.strictEqual(valid.length, 2);
|
||||
})
|
||||
});
|
||||
it('Calculate', () => {
|
||||
assert.strictEqual(task01(example), expect01);
|
||||
tasksResults[0] = task01(input)
|
||||
let rules = getData(readFileSync('./input/2020-day-02.txt', 'utf-8'));
|
||||
tasksResults[0] = checkRulesTask01(rules).length;
|
||||
});
|
||||
});
|
||||
describe('test02', () => {
|
||||
it('Calculate', () => {
|
||||
assert.strictEqual(task02(input), expect02);
|
||||
// assert.strictEqual(task02(example), expect02);
|
||||
tasksResults[1] = task02(input);
|
||||
});
|
||||
});
|
||||
|
@ -37,4 +62,41 @@ function task01(input: number[]): number {
|
|||
|
||||
function task02(input: number[]): number {
|
||||
return task01(input);
|
||||
}
|
||||
|
||||
interface PassEntry {
|
||||
min: number;
|
||||
max: number;
|
||||
letter: string;
|
||||
pass: string;
|
||||
}
|
||||
|
||||
let rePwLine = /^(\d+)-(\d+)\s(\w+):\s(.*)$/;
|
||||
function getData(raw: string): PassEntry[] {
|
||||
return raw.split('\n').map(line => {
|
||||
let match = line.match(rePwLine);
|
||||
if (!match) {
|
||||
throw new Error('Not a match: ' + line);
|
||||
}
|
||||
return {
|
||||
min: +match[1], max: +match[2], letter: match[3], pass: match[4]
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function checkRulesTask01(rules: PassEntry[]): PassEntry[] {
|
||||
return rules.filter((rule) => {
|
||||
let table = mkTable(rule);
|
||||
let c = table[rule.letter]
|
||||
return (c >= rule.min) && (c <= rule.max);
|
||||
});
|
||||
}
|
||||
|
||||
function mkTable(rule: PassEntry) {
|
||||
let table: any = {};
|
||||
for (let i = 0; i < rule.pass.length; i++) {
|
||||
let c = rule.pass.charAt(i);
|
||||
table[c] = (table[c]) ? table[c] + 1 : 1;
|
||||
}
|
||||
return table;
|
||||
}
|
Loading…
Reference in New Issue