-
Notifications
You must be signed in to change notification settings - Fork 1
/
day10.js
133 lines (117 loc) · 3.79 KB
/
day10.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const {fetchData} = require('./util.js');
console.info('D10P1 sample:', part1('./day10sample.txt'));
console.info('D10P1 input:', part1('./day10input.txt'));
console.info('D10P2 sample:', part2('./day10sample2.txt'));
console.info('D10P3 sample:', part2('./day10sample3.txt'));
console.info('D10P2 input:', part2('./day10input.txt'));
function part1(file) {
const lines = fetchData(file);
const path = [findStart(lines)];
while (notCompleteLoop(path)) {
path.push(nextStep(path, lines));
}
// console.info('Path', path);
return Math.floor(path.length / 2);
}
function part2(file) {
const lines = fetchData(file);
const path = [findStart(lines)];
while (notCompleteLoop(path)) {
path.push(nextStep(path, lines));
}
// console.info('Path', path);
const splitLines = lines.map(l => l.split(''));
// Corners: left to right, you will hit an L or F first, then a J or 7
// L-J counts as 2 verticals; L-7 counts as one
// F-7 counts as 2; F-J as one
const verticals = []
for (const [row,col] of path) {
if (splitLines[row][col] === '|' ||
splitLines[row].slice(col).join('').match(/^L-*7/) ||
splitLines[row].slice(col).join('').match(/^F-*J/)
)
{
verticals.push([row, col]);
}
}
for (const [row,col] of path) {
splitLines[row][col] = 'p';
}
for (const [row,col] of verticals) {
splitLines[row][col] = 'P';
}
// If a . has an odd number of Ps to its left, it is inside
let insides = 0;
splitLines.forEach((line, row) => {
let pathCrossings = 0;
line.forEach((symbol, col) => {
if (!'Pp'.includes(symbol)) {
const pLeft = line.slice(0, col).filter(s => s === 'P').length;
if (pLeft %2) {
++insides;
splitLines[row][col] = '*';
}
}
})
});
// console.info(splitLines.map(arr => arr.join('')).join('\n'));
// 564 is too high
return insides;
}
function findStart(lines) {
const row = lines.findIndex(l => l.includes('S'));
if (row < 0) {
console.info('Not found in', lines);
}
const col = lines[row].indexOf('S');
return [row, col];
}
function notCompleteLoop(path) {
const start = path[0].toString();
const end = path.slice(-1)[0].toString();
return (path.length < 2 || start !== end);
}
function nextStep(path, lines) {
const [row, col] = path.slice(-1)[0];
const symbol = lines[row][col];
// console.info('Row/Col/Symbol', row, col, symbol);
if (symbol === 'S') {
// return whichever connection
if (row > 0 && '|7F'.includes(lines[row-1][col])) {
return [row-1, col];
}
if (row < lines.length - 1 && '|LJ'.includes(lines[row+1][col])) {
return [row+1, col];
}
if (col > 0 && '-7J'.includes(lines[row][col+1])) {
return [row, col+1];
}
if ('-LF'.includes(lines[row][col-1])) {
return [row, col-1];
}
console.info('** Cannot get started');
}
const cameFrom = path.slice(-2)[0].toString();
return candidates(row, col, symbol).find(loc => loc.toString() !== cameFrom);
}
function candidates(row, col, symbol) {
if (symbol === 'F') {
return [[row+1, col], [row, col+1]];
}
if (symbol === '7') {
return ([[row, col-1], [row+1, col]]);
}
if (symbol === 'L') {
return ([[row-1, col], [row, col+1]]);
}
if (symbol === 'J') {
return ([[row, col-1], [row-1, col]]);
}
if (symbol === '|') {
return ([[row-1, col], [row+1, col]]);
}
if (symbol === '-') {
return ([[row, col-1], [row, col+1]]);
}
console.info('*** Whoops', symbol);
}