Skip to content

Commit 53112c6

Browse files
committed
day 23: #24, #687. for part 2 I considered the solution a few times ("I should use a linked list so inserts will be faster") but missed the critical step of having a value_map so finding is instant too. and then I fumbled around with the linked list for a while. I had it mostly done but not quite because I was missing the final set to end = current and that took a while to find
1 parent 5a9a9ff commit 53112c6

File tree

7 files changed

+272
-0
lines changed

7 files changed

+272
-0
lines changed

2020/solutions/day23/d23.zig

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const std = @import("std");
2+
3+
pub fn range(max: usize) []const void {
4+
return @as([]const void, &[_]void{}).ptr[0..max];
5+
}
6+
7+
pub fn main() !void {
8+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
9+
defer arena.deinit();
10+
const alloc = &arena.allocator;
11+
const array_space = try alloc.alloc(usize, 20_000_000);
12+
13+
var array: []usize = array_space[0..1_000_000];
14+
15+
for(range(1_000_000)) |_, i| {
16+
array[i] = i + 1;
17+
}
18+
for("389125467") |char, i| {
19+
array[i] = (char - '0');
20+
}
21+
22+
for(range(10_000_000)) |_, iter_index| {
23+
const current = array[0];
24+
const three = array[1..4];
25+
array = array[4..];
26+
array.len += 4;
27+
// this is going to be too slow isn't it
28+
var searchv = current - 1;
29+
if(searchv == 0) searchv = array.len;
30+
for(range(3)) |_| {
31+
for(three) |v| {
32+
if(searchv == v) searchv -= 1;
33+
if(searchv == 0) searchv = array.len;
34+
}
35+
}
36+
const foundi = for(array) |v, i| {
37+
if(v == searchv) break i;
38+
} else std.debug.panic("Did not find {} in array", .{searchv});
39+
// 2 1 0 3
40+
// 2 . . . 1 0 3
41+
// TODO if it's faster, go the other direction?
42+
std.mem.copyBackwards(usize, array[foundi + 4..], array[foundi + 1..array.len - 3]);
43+
std.mem.copy(usize, array[foundi + 1..foundi + 4], three);
44+
array[array.len - 1] = current;
45+
46+
if(iter_index % 1_000 == 0) std.log.emerg("iter index: {}", .{iter_index});
47+
}
48+
}

2020/solutions/day23/day23.1.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
input: string, lines: string[], dblines: string[][]
3+
copy(text: string) → clipboard
4+
error(message: string) → thrown error
5+
-5..mod(3) → @mod(-5, 3)
6+
*/
7+
8+
// Cardinals:
9+
// [[1,0],[-1,0],[0,1],[0,-1]]
10+
// +Diagonals:
11+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
12+
13+
export {};
14+
15+
let cups = input.split("").map(v => +v);
16+
17+
const move = () => {
18+
let current = cups.shift();
19+
let [a, b, c] = [cups.shift(), cups.shift(), cups.shift()];
20+
let findv = current;
21+
let foundi: number;
22+
while(true) {
23+
findv -= 1;
24+
findv = findv.mod(10);
25+
foundi = cups.findIndex((q) => q == findv);
26+
if(foundi == -1) continue;
27+
break;
28+
}
29+
[a, b, c].forEach((v, i) => {
30+
cups.splice(foundi + i + 1, 0, v);
31+
})
32+
cups.push(current);
33+
print(cups.join(", "));
34+
}
35+
36+
for(let i = 0; i < 100; i++) move();
37+
38+
print(cups.join(","));
39+
40+
while(cups[0] !== 1) {
41+
cups.push(cups.shift());
42+
}
43+
cups.shift();
44+
45+
print(cups.join(""));

2020/solutions/day23/day23.2.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
input: string, lines: string[], dblines: string[][]
3+
copy(text: string) → clipboard
4+
error(message: string) → thrown error
5+
-5..mod(3) → @mod(-5, 3)
6+
*/
7+
8+
// Cardinals:
9+
// [[1,0],[-1,0],[0,1],[0,-1]]
10+
// +Diagonals:
11+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
12+
13+
export {};
14+
15+
// 389125467
16+
// 28915467 … 3
17+
// 5467 … 891 3 2
18+
// … 891 3 467 2 5
19+
20+
let cups = "389125467".split("").map(v => +v);
21+
22+
let starti = cups.length + 1;
23+
print(starti);
24+
while(cups.length < 1000000) {
25+
cups.push(starti);
26+
starti += 1;
27+
}
28+
29+
// 10,000,000 times:
30+
// take four from the start
31+
// find the number either 1-, 2-, 3-, or 4- the first one taken, depending on the next three
32+
// insert the three after that
33+
// insert the first at the end
34+
// very fast
35+
36+
// so
37+
38+
print(cups);
39+
error("bye");
40+
41+
const move = () => {
42+
let current = cups.shift();
43+
let [a, b, c] = [cups.shift(), cups.shift(), cups.shift()];
44+
let findv = current;
45+
let foundi: number;
46+
while(true) {
47+
findv -= 1;
48+
findv = findv.mod(10);
49+
foundi = cups.findIndex((q) => q == findv);
50+
if(foundi == -1) continue;
51+
break;
52+
}
53+
[a, b, c].forEach((v, i) => {
54+
cups.splice(foundi + i + 1, 0, v);
55+
})
56+
cups.push(current);
57+
// print(cups.join(", "));
58+
}
59+
60+
for(let i = 0; i < 10000000; i++) move();
61+
62+
print(cups.join(","));
63+
64+
while(cups[0] !== 1) {
65+
cups.push(cups.shift());
66+
}
67+
cups.shift();
68+
69+
print(cups.shift(), cups.shift());

2020/solutions/day23/day23.3.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
input: string, lines: string[], dblines: string[][]
3+
copy(text: string) → clipboard
4+
error(message: string) → thrown error
5+
-5..mod(3) → @mod(-5, 3)
6+
*/
7+
8+
// Cardinals:
9+
// [[1,0],[-1,0],[0,1],[0,-1]]
10+
// +Diagonals:
11+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
12+
13+
14+
type thisv = [number, thisv]
15+
let cups: thisv[] = input.split("").map(v => [+v]) as any as thisv[];
16+
17+
let starti = cups.length + 1;
18+
print(starti);
19+
while(cups.length < 1000000) {
20+
cups.push([starti] as any as thisv);
21+
starti += 1;
22+
}
23+
24+
let start = cups[0];
25+
26+
cups.forEach((v, i) => {
27+
v[1] = cups[(i + 1) % cups.length];
28+
});
29+
30+
let end = cups[cups.length - 1];
31+
32+
const value_map: thisv[] = [];
33+
34+
cups.forEach(q => {
35+
value_map[q[0]] = q;
36+
});
37+
38+
// ok no one cares about the cups array anymore
39+
40+
const move = () => {
41+
42+
let current = start;
43+
start = start[1];
44+
45+
let a = start;
46+
start = start[1];
47+
let b = start;
48+
start = start[1];
49+
let c = start;
50+
start = start[1];
51+
52+
end[1] = start;
53+
54+
let findv = current[0] - 1;
55+
if(findv == 0) findv = cups.length;
56+
if(findv == a[0] || findv == b[0] || findv == c[0]) findv -= 1;
57+
if(findv == 0) findv = cups.length;
58+
if(findv == a[0] || findv == b[0] || findv == c[0]) findv -= 1;
59+
if(findv == 0) findv = cups.length;
60+
if(findv == a[0] || findv == b[0] || findv == c[0]) findv -= 1;
61+
if(findv == 0) findv = cups.length;
62+
63+
let found = value_map[findv];
64+
65+
if(found == end) end = c;
66+
c[1] = found[1];
67+
found[1] = a;
68+
69+
end[1] = current;
70+
current[1] = start;
71+
end = current;
72+
}
73+
74+
for(let i = 0; i < 10000000; i++) {
75+
move();
76+
}
77+
78+
const oneloc = value_map[1];
79+
80+
print(oneloc[1][0] * oneloc[1][1][0]);
81+

2020/solutions/day23/day23.4.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
input: string, lines: string[], dblines: string[][]
3+
copy(text: string) → clipboard
4+
error(message: string) → thrown error
5+
-5..mod(3) → @mod(-5, 3)
6+
*/
7+
8+
// Cardinals:
9+
// [[1,0],[-1,0],[0,1],[0,-1]]
10+
// +Diagonals:
11+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
12+
13+
export {};
14+

2020/solutions/day23/day23.5.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
input: string, lines: string[], dblines: string[][]
3+
copy(text: string) → clipboard
4+
error(message: string) → thrown error
5+
-5..mod(3) → @mod(-5, 3)
6+
*/
7+
8+
// Cardinals:
9+
// [[1,0],[-1,0],[0,1],[0,-1]]
10+
// +Diagonals:
11+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
12+
13+
export {};
14+

2020/solutions/day23/day23.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
362981754

0 commit comments

Comments
 (0)