-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
p2.ts
139 lines (122 loc) · 3.06 KB
/
p2.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
import { IntCodeComputer } from '@alexaegis/advent-of-code-intcode';
import { task } from '@alexaegis/advent-of-code-lib';
import { Direction, Vec2 } from '@alexaegis/advent-of-code-lib/model';
import packageJson from '../package.json';
import { parse } from './parse.js';
export enum Color {
WHITE = '#',
BLACK = '.',
}
export enum ColorCode {
WHITE = 1,
BLACK = 0,
}
export const colorCodeToColor = (cc?: ColorCode): Color => {
switch (cc) {
case ColorCode.BLACK: {
return Color.BLACK;
}
case ColorCode.WHITE: {
return Color.WHITE;
}
default: {
return Color.BLACK;
}
}
};
export const turnToDir = (cc: Turn): Direction => {
switch (cc) {
case Turn.LEFT: {
return Direction.WEST;
}
case Turn.RIGHT: {
return Direction.EAST;
}
}
};
export enum Turn {
LEFT = 0,
RIGHT = 1,
}
/*
export const drawMap = <T>(map: Map<string, T>, renderTile: (t: T) => string, defaultTile = '.'): string[][] => {
const res: string[][] = [];
let yS = 0;
for (const [s, t] of map.entries()) {
const pos = new Coord(s);
if (res.length === 0) {
res.push([]);
yS = pos.y;
} else if (pos.y < yS) {
for (let i = 0; i < yS - pos.y; i++) {
const newRow: string[] = [];
for (let j = 0; j < res[0].length; j++) {
newRow.push(defaultTile);
}
res.unshift(newRow);
}
yS = pos.y;
}
}
return res;
};*/
export const drawMap = <T>(
map: Map<string, T>,
renderTile: (t?: T) => string,
startY: number,
endY: number,
startX: number,
endX: number,
): string[][] => {
const res: string[][] = [];
for (let i = startY; i <= endY; i++) {
const row = [];
for (let j = startX; j <= endX; j++) {
row.push(renderTile(map.get(new Vec2(j, i).toString())));
}
res.push(row);
}
return res;
};
export const renderMatrix = (matrix: string[][]): string => {
return matrix
.map((row) => row.join(''))
.reverse()
.join('\n');
};
export const outputMap = <T>(
map: Map<string, T>,
renderTile: (t?: T) => string,
startY: number,
endY: number,
startX: number,
endX: number,
): void => {
console.log(renderMatrix(drawMap(map, renderTile, startY, endY, startX, endX)));
};
export const p2 = (input: string): string => {
const intcode = new IntCodeComputer(parse(input));
const iter = intcode.iter();
const map = new Map<string, ColorCode>();
const pos = new Vec2(0, 0);
let dir = Direction.NORTH;
let nextColor: IteratorResult<number>;
let nextDir: IteratorResult<number>;
let currentColor: ColorCode = ColorCode.WHITE;
let currentDir: Turn;
intcode.pushInput(currentColor);
nextColor = iter.next();
nextDir = iter.next();
while (!nextColor.done && !nextDir.done && !intcode.isHalt()) {
currentColor = nextColor.value as ColorCode;
currentDir = nextDir.value as Turn;
dir = currentDir === Turn.RIGHT ? dir.right() : dir.left();
map.set(pos.toString(), currentColor);
pos.addMut(dir);
intcode.pushInput(map.get(pos.toString()) ?? ColorCode.BLACK);
nextColor = iter.next();
nextDir = iter.next();
}
return renderMatrix(drawMap(map, colorCodeToColor, -6, 2, -2, 50));
};
await task(p2, packageJson.aoc); // AGALRGJE ~10ms