-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.js
119 lines (116 loc) · 1.7 KB
/
day12.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
const fs = require('fs')
const infile = fs.readFileSync('input/day12', 'utf-8')
function day121(input){
input = input.split('\r\n')
let rot = 0
let ns = 0
let ew = 0
input.forEach((e)=>{
let [instr, num] = e.split(/(?<=[A-Z])/)
num = parseInt(num)
switch(instr){
case 'N':
ns+=num
break;
case 'S':
ns-=num
break;
case 'E':
ew+=num
break;
case 'W':
ew-=num
break;
case 'L':
rot+=num
break;
case 'R':
rot-=num
break;
case 'F':
switch((4+rot/90%4)%4){
case 0:
ew+=num
break;
case 1:
ns+=num
break;
case 2:
ew-=num
break;
case 3:
ns-=num
break;
}
break;
}
})
return Math.abs(ns)+Math.abs(ew)
}
function day122(input){
input = input.split('\r\n')
let x = 0
let y = 0
let wx = 10
let wy = 1
input.forEach((e)=>{
let [instr, num] = e.split(/(?<=[A-Z])/)
num = parseInt(num)
switch(instr){
case 'N':
wy+=num
break;
case 'S':
wy-=num
break;
case 'E':
wx+=num
break;
case 'W':
wx-=num
break;
case 'L':
switch((4+num/90%4)%4){
case 1:
m = wx
wx = wy*-1
wy = m
break;
case 2:
wx*=-1
wy*=-1
break;
case 3:
m = wx
wx = wy
wy = m*-1
break;
}
break;
case 'R':
switch((4+num/90%4)%4){
case 3:
m = wx
wx = wy*-1
wy = m
break;
case 2:
wx*=-1
wy*=-1
break;
case 1:
m = wx
wx = wy
wy = m*-1
break;
}
break;
case 'F':
x += wx*num
y += wy*num
}
})
return Math.abs(y)+Math.abs(x)
}
console.log(day121(infile))
console.log(day122(infile))