Skip to content

Commit 71fe971

Browse files
committed
Added aoc2017 solutions - 6 days
1 parent e32b010 commit 71fe971

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2174
-469
lines changed

advent-of-code/2017/.vscode/tasks.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
{
55
"label": "test",
66
"type": "shell",
7-
"command": "mocha '${file}'",
7+
"command": "mocha -t 15000 '${file}'",
88
"group": {
99
"kind": "build",
1010
"isDefault": true
1111
},
1212
"problemMatcher": []
13-
},
13+
}
1414
{
1515
"label": "run",
1616
"type": "shell",

advent-of-code/2017/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
1010

1111
## Days
1212

13-
- [Day 1: ](day-01/)
14-
TBC
13+
- [Day 1: Inverse Captcha](day-01/)
14+
- [Day 2: Corruption Checksum](day-02/)
15+
- [Day 3: Spiral Memory](day-03/)
16+
- [Day 4: High-Entropy Passphrases](day-04/)
17+
- [Day 5: A Maze of Twisty Trampolines, All Alike](day-05/)
18+
- [Day 6: Memory Reallocation](day-06/)

advent-of-code/2017/day-01/partOne.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
const partOne = input => {
2-
1+
const partOne = captcha => {
2+
const circularCaptcha = captcha.slice(); // Shallow copy because partTwo will also use the captcha Array
3+
circularCaptcha.push(circularCaptcha[0]);
4+
5+
return circularCaptcha.reduce(
6+
(acc, n, i, arr) => acc + (n === arr[i + 1] ? n : 0),
7+
0
8+
);
39
};
410

5-
module.exports = partOne;
11+
module.exports = partOne;

advent-of-code/2017/day-01/partTwo.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
const partTwo = input => {
2-
1+
const partTwo = captcha => {
2+
const offset = captcha.length / 2;
3+
4+
return captcha.reduce(
5+
(acc, n, i, arr) => acc + (n === arr[(i + offset) % arr.length] ? n : 0),
6+
0
7+
);
38
};
49

510
module.exports = partTwo;
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const partOne = require('./partOne');
2-
// const partTwo = require('./partTwo');
2+
const partTwo = require('./partTwo');
33

44
const { readFileSync } = require('fs');
5-
const input = readFileSync(__dirname + '/input.txt', 'utf8');
5+
const INPUT = readFileSync(__dirname + '/input.txt', 'utf8').split('').map(x => parseInt(x));
66

7-
console.log('PART 1:', partOne(input));
7+
console.log('PART 1:', partOne(INPUT));
88

9-
// console.log('PART 2:', partTwo(input));
9+
console.log('PART 2:', partTwo(INPUT));

advent-of-code/2017/day-01/test.js

+33-32
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
const assert = require('assert');
22
const partOne = require('./partOne');
3-
// const partTwo = require('./partTwo');
3+
const partTwo = require('./partTwo');
44

5-
describe('Part 1', () => {
6-
it('should properly calculate 1122', () => {
7-
assert.equal(3, partOne('1122'));
8-
});
5+
describe('Day 1: Inverse Captcha', () => {
6+
describe('Part 1', () => {
7+
it('should evaluate captcha 1122', () => {
8+
assert.equal(3, partOne([1, 1, 2, 2]));
9+
});
910

10-
it('should properly calculate 1111', () => {
11-
assert.equal(4, partOne('1111'));
12-
});
11+
it('should evaluate captcha 1111', () => {
12+
assert.equal(4, partOne([1, 1, 1, 1]));
13+
});
1314

14-
it('should properly calculate 1234', () => {
15-
assert.equal(0, partOne('1234'));
16-
});
15+
it('should evaluate captcha 1234', () => {
16+
assert.equal(0, partOne([1, 2, 3, 4]));
17+
});
1718

18-
it('should properly calculate 91212129', () => {
19-
assert.equal(9, partOne('91212129'));
19+
it('should evaluate captcha 91212129', () => {
20+
assert.equal(9, partOne([9, 1, 2, 1, 2, 1, 2, 9]));
21+
});
2022
});
2123

22-
});
24+
describe('Part 2', () => {
25+
it('should evaluate captcha 1212', () => {
26+
assert.equal(6, partTwo([1, 2, 1, 2]));
27+
});
2328

24-
// describe('Part 2', () => {
25-
// it('should properly calculate 1212', () => {
26-
// assert.equal(6, partTwo('1212'));
27-
// });
29+
it('should evaluate captcha 1221', () => {
30+
assert.equal(0, partTwo([1, 2, 2, 1]));
31+
});
2832

29-
// it('should properly calculate 1221', () => {
30-
// assert.equal(0, partTwo('1221'));
31-
// });
33+
it('should evaluate captcha 123425', () => {
34+
assert.equal(4, partTwo([1, 2, 3, 4, 2, 5]));
35+
});
3236

33-
// it('should properly calculate 123425', () => {
34-
// assert.equal(4, partTwo('123425'));
35-
// });
37+
it('should evaluate captcha 123123', () => {
38+
assert.equal(12, partTwo([1, 2, 3, 1, 2, 3]));
39+
});
3640

37-
// it('should properly calculate 123123', () => {
38-
// assert.equal(12, partTwo('123123'));
39-
// });
40-
41-
// it('should properly calculate 12131415', () => {
42-
// assert.equal(4, partTwo('12131415'));
43-
// });
44-
// });
41+
it('should evaluate captcha 12131415', () => {
42+
assert.equal(4, partTwo([1, 2, 1, 3, 1, 4, 1, 5]));
43+
});
44+
});
45+
});

advent-of-code/2017/day-02/input.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
104 240 147 246 123 175 372 71 116 230 260 118 202 270 277 292
2+
740 755 135 205 429 822 844 90 828 115 440 805 526 91 519 373
3+
1630 991 1471 1294 52 1566 50 1508 1367 1489 55 547 342 512 323 51
4+
1356 178 1705 119 1609 1409 245 292 1434 694 405 1692 247 193 1482 1407
5+
2235 3321 3647 212 1402 3711 3641 1287 2725 692 1235 3100 123 144 104 101
6+
1306 1224 1238 186 751 734 1204 1275 366 149 1114 166 1118 239 153 943
7+
132 1547 1564 512 2643 2376 2324 2159 1658 107 1604 145 2407 131 2073 1878
8+
1845 91 1662 108 92 1706 1815 1797 1728 1150 1576 83 97 547 1267 261
9+
78 558 419 435 565 107 638 173 93 580 338 52 633 256 377 73
10+
1143 3516 4205 3523 148 401 3996 3588 300 1117 2915 1649 135 134 182 267
11+
156 2760 1816 2442 2985 990 2598 1273 167 821 138 141 2761 2399 1330 1276
12+
3746 3979 2989 161 4554 156 3359 173 3319 192 3707 264 762 2672 4423 2924
13+
3098 4309 4971 5439 131 171 5544 595 154 571 4399 4294 160 6201 4329 5244
14+
728 249 1728 305 2407 239 691 2241 2545 1543 55 2303 1020 753 193 1638
15+
260 352 190 877 118 77 1065 1105 1085 1032 71 87 851 56 1161 667
16+
1763 464 182 1932 1209 640 545 931 1979 197 1774 174 2074 1800 939 161

advent-of-code/2017/day-02/partOne.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
const partOne = input => {
2-
1+
const partOne = rows => {
2+
return rows.reduce(
3+
(checksum, row) => checksum + Math.max(...row) - Math.min(...row),
4+
0
5+
);
36
};
47

5-
module.exports = partOne;
8+
module.exports = partOne;

advent-of-code/2017/day-02/partTwo.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
const partTwo = input => {
2-
1+
const partTwo = rows => {
2+
return rows.reduce((checksum, row) => {
3+
for (let i = 0; i < row.length; i++) {
4+
const candidates = row.filter(x => x < row[i]);
5+
6+
for (let j = 0; j < candidates.length; j++) {
7+
if (row[i] % candidates[j] === 0) {
8+
return checksum + row[i] / candidates[j];
9+
}
10+
}
11+
}
12+
}, 0);
313
};
414

515
module.exports = partTwo;
+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const partOne = require('./partOne');
2-
// const partTwo = require('./partTwo');
2+
const partTwo = require('./partTwo');
33

44
const { readFileSync } = require('fs');
5-
const input = readFileSync(__dirname + '/input.txt', 'utf8');
5+
const INPUT = readFileSync(__dirname + '/input.txt', 'utf8')
6+
.split('\r\n')
7+
.map(line => line.split('\t').map(num => parseInt(num)));
68

7-
console.log('PART 1:', partOne(input));
9+
console.log('PART 1:', partOne(INPUT));
810

9-
// console.log('PART 2:', partTwo(input));
11+
console.log('PART 2:', partTwo(INPUT));

advent-of-code/2017/day-02/test.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
const assert = require('assert');
22
const partOne = require('./partOne');
3-
// const partTwo = require('./partTwo');
3+
const partTwo = require('./partTwo');
44

5-
describe('Part 1', () => {
5+
describe('Day 2: Corruption Checksum', () => {
6+
describe('Part 1', () => {
7+
it('should calculate checksum as 18', () => {
8+
const SPREADSHEET = [[5, 1, 9, 5], [7, 5, 3], [2, 4, 6, 8]];
69

7-
});
10+
assert.equal(18, partOne(SPREADSHEET));
11+
});
12+
});
813

9-
// describe('Part 2', () => {
14+
describe('Part 2', () => {
15+
it('should calculate second checksum as 9', () => {
16+
const SPREADSHEET = [[5, 9, 2, 8], [9, 4, 7, 3], [3, 8, 6, 5]];
1017

11-
// });
18+
assert.equal(9, partTwo(SPREADSHEET));
19+
});
20+
});
21+
});

advent-of-code/2017/day-03/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

advent-of-code/2017/day-03/input.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)