-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
p1.ts
70 lines (59 loc) · 1.72 KB
/
p1.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
import { task } from '@alexaegis/advent-of-code-lib';
import {
CircularLinkedList,
CircularLinkedListNode,
} from '@alexaegis/advent-of-code-lib/linked-list';
import { max, min } from '@alexaegis/advent-of-code-lib/math';
import packageJson from '../package.json';
export const p1 =
(iterationCount = 100) =>
(input: string): number => {
const circle = [...input].map((s) => Number.parseInt(s, 10));
const high = circle.reduce(max);
const low = circle.reduce(min);
const ll = new CircularLinkedList<number>(circle);
let cursor = ll.head;
const m = new Map<number, CircularLinkedListNode<number>>();
for (const link of cursor.forward()) {
m.set(link.value, link);
}
for (let i = 0; i < iterationCount; i++) {
// pick up three cups
const front = cursor.next;
cursor.next = front.next.next.next;
// select destination
let destination: CircularLinkedListNode<number> | undefined;
let val = cursor.value;
let sub = 1;
while (
!destination ||
destination === front ||
destination === front.next ||
destination === front.next.next
) {
destination = m.get(val - sub);
sub++;
if (val - sub < low) {
sub = 0;
val = high;
}
}
// reinsert at destination
const after = destination.next;
destination.next = front;
front.prev = destination;
after.prev = front.next.next.next;
front.next.next.next = after;
// next
cursor = cursor.next;
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
let first = m.get(1)!.next;
let result = '';
while (first.value !== 1) {
result += first.value.toString();
first = first.next;
}
return Number.parseInt(result, 10);
};
await task(p1(10), packageJson.aoc); // 783895 ~22ms