Skip to content

Instantly share code, notes, and snippets.

@p7g
Last active March 12, 2021 04:48
Show Gist options
  • Save p7g/ac732df415853ba234e76b7ee441d95e to your computer and use it in GitHub Desktop.
Save p7g/ac732df415853ba234e76b7ee441d95e to your computer and use it in GitHub Desktop.
cbcvm: 6.585 total, Python: 10.573 total
from itertools import count
from lib.input import fetch
def main():
class node:
__slots__ = "label", "next"
def __init__(self, label):
self.label = label
self.next = None
data = fetch().strip()
all_cups = list(map(int, data))
for i in range(max(all_cups) + 1, 1_000_000 + 1):
all_cups.append(i)
lookup = [None] * 1_000_001
current = cups = lookup[all_cups[0]] = node(all_cups[0])
for label in all_cups[1:]:
cups.next = lookup[label] = node(label)
cups = cups.next
cups.next = current
min_cup = min(all_cups)
max_cup = max(all_cups)
for move in count(1):
a = current.next
b = a.next
c = b.next
current.next = c.next
dest = current.label - 1
if dest < min_cup:
dest = max_cup
while dest == a.label or dest == b.label or dest == c.label:
dest -= 1
if dest < min_cup:
dest = max_cup
dest_node = lookup[dest]
end = dest_node.next
dest_node.next = a
c.next = end
current = current.next
if move == 10_000_000:
break
one = lookup[1]
print(one.next.label * one.next.next.label)
if __name__ == "__main__":
main()
import array;
import arraylist;
import char;
import fs;
import hash;
import hashmap;
import iter;
import string;
function main() {
let data = string.strip(
fs.read_file("../advent-of-code-2020/days/day23/input.txt"),
);
struct node { label, next }
let all_cups = arraylist.from_array(
string.length(data),
array.map(string.chars(data), char.to_digit),
);
let max_cup = iter.max(arraylist.to_iter(all_cups));
for (let i = max_cup + 1; i < 1000001; i = i + 1) {
arraylist.push(all_cups, i);
}
all_cups = arraylist.to_array(all_cups);
let lookup = array.new(1000001);
let first_node = node { label = all_cups[0] };
lookup[all_cups[0]] = first_node;
let cups = first_node;
let current = cups;
let len = array.length(all_cups);
for (let i = 1; i < len; i = i + 1) {
let label = all_cups[i];
lookup[label] = cups = cups:next = node { label = label };
}
cups:next = current;
let min_cup = iter.min(all_cups),
max_cup = iter.max(all_cups);
for (let move = 0;; move = move + 1) {
let a = current:next;
let b = a:next;
let c = b:next;
current:next = c:next;
let dest = current:label - 1;
if (dest < min_cup) {
dest = max_cup;
}
while (dest == a:label || dest == b:label || dest == c:label) {
dest = dest - 1;
if (dest < min_cup) {
dest = max_cup;
}
}
let dest_node = lookup[dest];
let end = dest_node:next;
dest_node:next = a;
c:next = end;
current = current:next;
if (move == 10000000) {
break;
}
}
let one = lookup[1];
println(one:next:label * one:next:next:label);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment