Skip to content

Commit 3f8fa95

Browse files
committed
day 3
1 parent 8f93f7d commit 3f8fa95

File tree

4 files changed

+467
-16
lines changed

4 files changed

+467
-16
lines changed

src/day2/day2.test.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,64 @@ import path from 'path';
22

33
import fs from 'fs-extra';
44

5-
import { getScore, getScore2 } from './day2';
5+
import { getElf, getElf2 } from './day2';
66

77
const realData = fs.readFileSync(path.join(__dirname, './input.txt'), {
88
encoding: 'utf-8',
99
});
1010

11-
describe('day2', () => {
12-
describe('getScore', () => {
11+
describe('day1', () => {
12+
describe('getElf', () => {
1313
const util = (input, expected) =>
1414
it(`should return ${expected}`, () => {
15-
expect(getScore(input)).toBe(expected);
15+
expect(getElf(input)).toBe(expected);
1616
});
1717

1818
util(
19-
`A Y
20-
B X
21-
C Z`,
22-
15,
19+
`1000
20+
2000
21+
3000
22+
23+
4000
24+
25+
5000
26+
6000
27+
28+
7000
29+
8000
30+
9000
31+
32+
10000`,
33+
24000,
2334
);
2435

25-
util(realData, 10816);
36+
util(realData, 70720);
2637
});
2738

28-
describe('getScore2', () => {
39+
describe('getElf2', () => {
2940
const util = (input, expected) =>
3041
it(`should return ${expected}`, () => {
31-
expect(getScore2(input)).toBe(expected);
42+
expect(getElf2(input)).toBe(expected);
3243
});
3344

3445
util(
35-
`A Y
36-
B X
37-
C Z`,
38-
12,
46+
`1000
47+
2000
48+
3000
49+
50+
4000
51+
52+
5000
53+
6000
54+
55+
7000
56+
8000
57+
9000
58+
59+
10000`,
60+
45000,
3961
);
4062

41-
util(realData, 11657);
63+
util(realData, 207148);
4264
});
4365
});

src/day3/day3.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
2+
export const part1 = input => {
3+
const parse = input =>
4+
input.split('\n').reduce((xs, next) => {
5+
if (next) {
6+
const at = next.length / 2;
7+
8+
xs.push([next.slice(0, at), next.slice(-at)]);
9+
}
10+
return xs;
11+
}, []);
12+
const parsed = parse(input);
13+
let score = 0;
14+
15+
for (let i = 0; i < parsed.length; i++) {
16+
const [a, b] = parsed[i];
17+
const as = a.split('');
18+
const bs = b.split('');
19+
20+
let common;
21+
let j = 0;
22+
while (!common) {
23+
if (bs.indexOf(as[j]) !== -1) {
24+
common = as[j];
25+
}
26+
j++;
27+
}
28+
score += str.indexOf(common) + 1;
29+
}
30+
31+
return score;
32+
};
33+
34+
export const part2 = input => {
35+
let i = 0;
36+
const parse = input =>
37+
input.split('\n').reduce((xs, next) => {
38+
if (next) {
39+
if (i % 3 === 0) {
40+
xs.push([]);
41+
}
42+
i++;
43+
xs[xs.length - 1].push(next.split(''));
44+
}
45+
return xs;
46+
}, []);
47+
48+
const parsed = parse(input);
49+
50+
const getF = (x, char) => (x.indexOf(char) === -1 ? 0 : 1);
51+
52+
let score = 0;
53+
54+
for (let i = 0, l = parsed.length; i < l; i++) {
55+
const [a, b, c] = parsed[i];
56+
let foundChar;
57+
58+
for (let j = 0, l = a.length; j < l && foundChar == null; j++) {
59+
const char = a[j];
60+
if (getF(b, char) + getF(c, char) === 2) {
61+
foundChar = char;
62+
}
63+
}
64+
score += str.indexOf(foundChar) + 1;
65+
}
66+
67+
return score;
68+
};

src/day3/day3.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import path from 'path';
2+
3+
import fs from 'fs-extra';
4+
5+
import { part1, part2 } from './day3';
6+
7+
const realData = fs.readFileSync(path.join(__dirname, './input.txt'), {
8+
encoding: 'utf-8',
9+
});
10+
11+
describe('day3', () => {
12+
describe('1', () => {
13+
const util = (input, expected) =>
14+
it(`should return ${expected}`, () => {
15+
expect(part1(input)).toBe(expected);
16+
});
17+
18+
util(
19+
`vJrwpWtwJgWrhcsFMMfFFhFp
20+
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
21+
PmmdzqPrVvPwwTWBwg
22+
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
23+
ttgJtRGJQctTZtZT
24+
CrZsJsPPZsGzwwsLwLmpwMDw`,
25+
157,
26+
);
27+
28+
util(realData, 8394);
29+
});
30+
31+
describe('2', () => {
32+
const util = (input, expected) =>
33+
it(`should return ${expected}`, () => {
34+
expect(part2(input)).toBe(expected);
35+
});
36+
37+
util(
38+
`vJrwpWtwJgWrhcsFMMfFFhFp
39+
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
40+
PmmdzqPrVvPwwTWBwg`,
41+
18,
42+
);
43+
util(
44+
`wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
45+
ttgJtRGJQctTZtZT
46+
CrZsJsPPZsGzwwsLwLmpwMDw`,
47+
52,
48+
);
49+
util(
50+
`vJrwpWtwJgWrhcsFMMfFFhFp
51+
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
52+
PmmdzqPrVvPwwTWBwg
53+
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
54+
ttgJtRGJQctTZtZT
55+
CrZsJsPPZsGzwwsLwLmpwMDw`,
56+
70,
57+
);
58+
59+
util(realData, 2413);
60+
});
61+
});

0 commit comments

Comments
 (0)