-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.ts
167 lines (138 loc) · 4.34 KB
/
day14.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
import { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts";
import { loadTestData, loadData } from "./utils.ts";
import md5 from "npm:md5";
const part1test = 136;
const part2test = 64;
const tiltNorth = (grid: string[][]): string[][] => {
const newGrid = [];
for (const line of grid) {
newGrid.push(line.slice())
}
let done = false;
while (!done) {
done = true;
for (let i = 1; i < newGrid.length; i++) {
for (let j = 0; j < newGrid[i].length; j++) {
if (newGrid[i - 1][j] == '.' && newGrid[i][j] == 'O') {
newGrid[i][j] = '.';
newGrid[i - 1][j] = 'O';
done = false;
}
}
}
}
return newGrid;
}
const tiltSouth = (grid: string[][]): string[][] => {
const newGrid = [];
for (const line of grid) {
newGrid.push(line.slice())
}
let done = false;
while (!done) {
done = true;
for (let i = 1; i < newGrid.length; i++) {
for (let j = 0; j < newGrid[i].length; j++) {
if (newGrid[i - 1][j] == 'O' && newGrid[i][j] == '.') {
newGrid[i][j] = 'O';
newGrid[i - 1][j] = '.';
done = false;
}
}
}
}
return newGrid;
}
const tiltWest = (grid: string[][]): string[][] => {
const newGrid = [];
for (const line of grid) {
newGrid.push(line.slice())
}
let done = false;
while (!done) {
done = true;
for (let i = 0; i < newGrid.length; i++) {
for (let j = 1; j < newGrid[i].length; j++) {
if (newGrid[i][j - 1] == '.' && newGrid[i][j] == 'O') {
newGrid[i][j] = '.';
newGrid[i][j - 1] = 'O';
done = false;
}
}
}
}
return newGrid;
}
const tiltEast = (grid: string[][]): string[][] => { // Future me: the issue is somewhere here
const newGrid = [];
for (const line of grid) {
newGrid.push(line.slice())
}
let done = false;
while (!done) {
done = true;
for (let i = 0; i < newGrid.length; i++) {
for (let j = 0; j < newGrid[i].length - 1; j++) {
if (newGrid[i][j + 1] == '.' && newGrid[i][j] == 'O') {
newGrid[i][j] = '.';
newGrid[i][j + 1] = 'O';
done = false;
}
}
}
}
return newGrid;
}
const part1 = (data: string) => {
const lines = data.split("\n");
const grid = lines.map(line => Array.from(line));
const tilted = tiltNorth(grid).reverse();
let sum = 0;
for (let i = 0; i < tilted.length; i++) {
for (let j = 0; j < tilted[i].length; j++) {
if (tilted[i][j] == 'O') {
sum += i + 1;
}
}
}
return sum;
};
const part2 = (data: string) => {
const lines = data.split("\n");
const grid = lines.map(line => Array.from(line));
let tilted = grid;
let done = false;
const hashes: Map<string, { state: string[][], count: number }> = new Map();
let count = 0;
while (!done) {
tilted = tiltNorth(grid);
tilted = tiltWest(grid);
tilted = tiltSouth(grid);
tilted = tiltEast(grid);
const str = tilted.toString();
const hash = md5(str);
if (hashes.has(hash)) {
const cycleSize = count - hashes.get(hash)!.count;
const firstOccurence = hashes.get(hash)!.count;
const index = (1000000000 - firstOccurence) % cycleSize;
tilted = [...hashes.entries()].filter(([_, { count }]) => count == index + firstOccurence)[0][1].state;
done = true;
} else {
hashes.set(hash, { state: tilted, count: count++ });
}
}
tilted = tilted.reverse();
let sum = 0;
for (let i = 0; i < tilted.length; i++) {
for (let j = 0; j < tilted[i].length; j++) {
if (tilted[i][j] == 'O') {
sum += i + 1;
}
}
}
return sum;
};
assertEquals(part1(await loadTestData(14)), part1test)
console.log(part1(await loadData(14)))
// assertEquals(part2(await loadTestData(14)), part2test)
// console.log(part2(await loadData(14)))