Skip to content

Instantly share code, notes, and snippets.

# island finding with disjoint set union https://judge.yosupo.jp/submission/113653
from collections import defaultdict, Counter
class DSU:
def __init__(self, n):
self.par = list(range(n))
self.size = [1]*n
def find(self, x):
bkx = x
while x != self.par[x]:
x = self.par[x]
# ITERATIVE backtracking + pruning
import math
def decompose(n):
stk = [ [n**2, n-1] ] # "function stack", (squared, internal counter i (next number can't be larger or equal to...))
while stk:
squared, internal_counter_i = stk[-1]
if squared == 0:
stk.pop()
import math
def decompose(n):
res = None
ss = []
def rec(squared, next_number_cannot_be_larger_than):
nonlocal res
if res: return
if squared == 0:
res = ss[::-1]
# No two skyscrapers in a row or column may have the same number of floors
# The height of the skyscrapers is between 1 and 4
# so conservative upper bound is (4!)**4 = 331776
# A clue is the number of skyscrapers you can see.
import itertools
n=4
def solve_puzzle (clues):
grid=[]
def validate_column(clue, c, from_top):
/*Compiles with gcc -Wall -O2 -o wavwrite wavwrite.c*/
// Modified from https://stackoverflow.com/questions/23030980/creating-a-stereo-wav-file-using-c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <math.h>
# aoc_2022_day_24_tf.py
from collections import *
import math, sys, copy, functools, json, re
# In one minute, each blizzard moves one position in the direction it is pointing:
DIRS = [(0,-1),(-1,0),(0,1),(1,0)]
blizzards = defaultdict(list) # pos to direction
# f = open("day24.input")
f = open("day24_2.input")
grid = []
for line in f:
from collections import *
import math, sys, copy, functools, json
# !!! ONE ore-collecting robot in your pack that you can use to kickstart the whole operation
# which blueprint would maximize the number of opened geodes after 24 minutes
# needs at least 16GB ram to run pt 2...
from collections import *
import math
class DSU_Iterative_By_Size:
"""Iterative find, rank by size, and path compression
https://judge.yosupo.jp/submission/113655"""
def __init__(self, n):
self.par = list(range(n))
self.size = [1] * n
/*Compiles with gcc -Wall -O2 -o wavwrite wavwrite.c*/
// Modified from https://stackoverflow.com/questions/23030980/creating-a-stereo-wav-file-using-c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <math.h>

Designing an append() method that would concatenate a second queue onto the calling queue in Java. The method needed to traverse the appended queue’s nodes instead of dequeueing its items.

My solution at the time involved iterating until the node that was currently being checked had a null pointer and then transferring the node contents one more time after my while loop condition was met (since the node with the null pointer never got read/appended), but I could’ve iterated through by using the size() method at the start and iterating while i < [variable storing size()]

package jv.test;

import static java.lang.System.out;
import java.util.*;
import java.util.stream.*;