-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
214 lines (180 loc) · 4.67 KB
/
util.ts
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Solves the common reformatting task of turning,
* [
* 'a',
* 'b',
* '',
* 'x',
* 'y'
* ]
* Into,
* [
* [ 'a', 'b' ],
* [ 'x', 'y' ]
* ]
*/
export const groupLines = (lines: readonly string[], separator: string = ''): string[][] => {
let result = [[]]
lines.forEach(line => {
if (line === separator) result.push([])
else result[result.length - 1].push(line)
})
return result
}
/**
* Produces a range of numbers from start (inclusive) to end (exclu
* Most useful for iterating arrays by index
*
* for (i in range(0, arr.length)) { arr[i] }
*/
export const range = (start: number, end: number): number[] => Array.from({ length: end - start }, (_, i) => i + start)
export const sumReducer = (p: number, c: number) => p + c
export const productReducer = (p: number, c: number) => p * c
/**
*
* Virtual machine
*
*/
export const operations = ['acc', 'jmp', 'nop'] as const
export type Operation = typeof operations[number]
export type Instruction = {
op: Operation
arg: number
}
export type State = {
acc: number
instructionPtr: number
}
export const runProgram = (prog: readonly Instruction[], state: State = { instructionPtr: 0, acc: 0 }): State => {
while (!isComplete(prog, state)) {
state = runInstruction(prog, state)
}
return state
}
export const isComplete = (prog: readonly Instruction[], state: State): boolean =>
state.instructionPtr === prog.length
export const runInstruction = (prog: readonly Instruction[], state: State = { instructionPtr: 0, acc: 0 }): State => {
if (isComplete(prog, state)) return state
const inst = prog[state.instructionPtr]
// console.log(`Running: ${inst.op} ${inst.arg}. ${JSON.stringify(state)}`)
let newState: State = { ...state }
if (inst.op === 'nop') newState.instructionPtr += 1
if (inst.op === 'jmp') newState.instructionPtr += inst.arg
if (inst.op === 'acc') {
newState.acc += inst.arg
newState.instructionPtr += 1
}
return newState
}
/**
*
* Grids and map manipulation
*
*/
export type Coord = { y: number, x: number }
/**
* Map problems use ascii tiles illegible to the average human.
*
* Converts a map key,
* {
* '#': 'occupied',
* '.': 'space'
* }
*
* into a quick and dirty bidirectional map,
* {
* '#': 'occupied',
* '.': 'space',
* 'occupied': '#',
* 'space': '.'
* }
*/
export const mapping = (key: { [s: string]: string }) => {
const result = {}
for (const [k, v] of Object.entries(key)) {
result[k] = v
result[v] = k
}
return result
}
export class Grid<T extends string> {
grid: T[][]
/**
*
* An input file of,
* ABC
* DEF
*
* Should be parsed into,
* ['ABC', 'DEF']
*
* And will be indexed as,
* this.grid[y][x]
*
* where [0,0] is the top left corner
*
*/
constructor(lines: string[]) {
this.grid = lines.map((line) => line.split('')) as T[][]
}
flat(): T[] {
return this.grid.flat()
}
forEach(callback: (t: T, y: number, x: number) => void) {
for (const y of range(0, this.grid.length)) {
for (const x of range(0, this.grid[y].length)) {
callback(this.grid[y][x], y, x)
}
}
}
adjacentOcta(y: number, x: number): (T | undefined)[] {
return [
this.get(y - 1, x - 1),
this.get(y - 1, x),
this.get(y - 1, x + 1),
this.get(y, x + 1),
this.get(y + 1, x + 1),
this.get(y + 1, x),
this.get(y + 1, x - 1),
this.get(y, x - 1),
]
}
findFirstAlong(
y: number, x: number,
dy: number, dx: number,
criteria: T[]
): T | undefined {
while (true) {
y += dy
x += dx
if (!this.validTile(y, x)) break
const t = this.get(y, x)
if (criteria.includes(t)) return t
}
}
findInLineOfSight(y: number, x: number, criteria: T[]): (T | undefined)[] {
return [
this.findFirstAlong(y, x, -1, -1, criteria),
this.findFirstAlong(y, x, -1, 0, criteria),
this.findFirstAlong(y, x, -1, 1, criteria),
this.findFirstAlong(y, x, 0, 1, criteria),
this.findFirstAlong(y, x, 1, 1, criteria),
this.findFirstAlong(y, x, 1, 0, criteria),
this.findFirstAlong(y, x, 1, -1, criteria),
this.findFirstAlong(y, x, 0, -1, criteria),
]
}
set(y: number, x: number, t: T): void {
if (!this.validTile(y, x)) throw new Error(`Index out of bounds (${y}, ${x})`)
this.grid[y][x] = t
}
get(y: number, x: number): T | undefined {
return this.validTile(y, x) ? this.grid[y][x] : undefined
}
validTile(y: number, x: number): boolean {
return 0 <= y && y < this.grid.length && 0 <= x && x < this.grid[y].length
}
print(): void {
this.grid.forEach(line => console.log(line.join('')))
}
}