-
Notifications
You must be signed in to change notification settings - Fork 0
/
d10.js
162 lines (142 loc) · 3.78 KB
/
d10.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const fs = require('node:fs');
fs.readFile('inputs/d10_input','utf8',(err,data)=>{
if(err){
console.log(err);
return;
}
const lines = data.trim().split('\n');
console.log("Part 1:",answerP1(lines));
console.log("Part 2:",answerP2(lines));
});
function findS(lines){
for(let i = 0; i < lines.length ; i++){
for(let j = 0; j < lines[i].length ; j++){
if(lines[i][j] == 'S'){
return [i,j]
}
}
}
return [-1,-1];
}
function findFriends([i,j],lines){
const friends = [];
//up
if(i-1 >= 0 && ['|','F','7'].includes(lines[i-1][j])){
friends.push([i-1,j,-1,0]);
}
//down
if(i+1 < lines.length && ['|','L','J'].includes(lines[i+1][j])){
friends.push([i+1,j,1,0]);
}
//left
if(j-1 >=0 && ['-','F','L'].includes(lines[i][j-1])){
friends.push([i,j-1,0,-1]);
}
//right
if(j+1 < lines[i].length && ['-','7','J'].includes(lines[i][j+1])){
friends.push([i,j+1,0,1]);
}
if(friends.length > 2){
console.log("HMMM");
}
return friends;
}
function nextDirection(pipe,[i,j]){
switch(pipe){
case '|':
return [i,0];
case '-':
return [0,j];
case 'F':
return [-j,-i];
case 'L':
return [j,i];
case 'J':
return [-j,-i];
case '7':
return [j,i];
}
}
function setStartSymbol(lines,friends,start){
lines[start[0]] = lines[start[0]].split('');
let [a,b,i1,j1] = friends[0];
let [c,d,i2,j2] = friends[1];
let s = '';
let flag = false;
switch(Math.abs(i2-i1)){
case 0:
s = '-'
break;
case 1:
flag = true;
break;
case 2:
s = '|'
break;
}
//less lazy
const corners = [['J','L'],['7','F']];
if(flag){
let down = Math.max(i1+i2,0);
let right = Math.max(j1+j2,0);
s = corners[down][right];
}
lines[start[0]][start[1]] = s;
}
function answerP1(lines){
const start = findS(lines);
const friends = findFriends(start,lines);
//we start at a friend after all
let distance = 1;
let [i,j, ...d] = friends[0];
while(i != start[0] || j != start[1]){
const pipe = lines[i][j];
d = nextDirection(pipe,d);
i = i + d[0];
j = j + d[1];
distance ++;
}
return Math.floor(distance/2);
}
function answerP2(lines){
const start = findS(lines);
const friends = findFriends(start,lines);
setStartSymbol(lines,friends,start);
let [i,j, ...d] = friends[0];
const loop = new Map();
while(i != start[0] || j != start[1]){
loop.set(`${[i,j]}`,1);
const pipe = lines[i][j];
d = nextDirection(pipe,d);
i = i + d[0];
j = j + d[1];
}
//include the start
loop.set(`${[i,j]}`,1);
let area = 0;
let inner = false;
let s = ''
const vertical = ['|','F','L'];
//when tracing the loop, directional information + locality can make this faster/better
for(let i = 0; i < lines.length; i++){
inner = false;
s = '';
for(let j = 0; j < lines[i].length; j++){
if(loop.has(`${[i,j]}`)){
if(vertical.includes(lines[i][j])){
inner = !inner;
s = lines[i][j];
}else if(lines[i][j] == '7'){ //ugh F7 is two verticals up then down, FJ is upup
if(s != 'L') inner = !inner;
}else if (lines[i][j] == 'J'){
if(s != 'F') inner = !inner;
}
continue;
}
if(inner){
area++;
}
}
}
return area;
}