Skip to content

Instantly share code, notes, and snippets.

View Sammons's full-sized avatar

Ben Sammons Sammons

View GitHub Profile
const fs = require('fs')
const input = fs.readFileSync('./input8.txt');
let mapOfValues = {}
function readChild(offset, input) {
let metaSum = 0;
let current = offset;
const numchildren = input[current];
console.log('NumChildren', numchildren);
type SettingTypeMap = {
 header: { color: keyof typeof colors; width: number };
 body: { width: number; height: number; logo: string; };
 footer: { fontSize: number; }
}
async function LookupSetting<
 SettingName extends keyof SettingTypeMap
 >(setting: SettingName): Promise<SettingTypeMap[SettingName]> {
 return {} as any; // replace with the actual logic to run a query
}
function Strictly<T extends { [key: string]: Values }, Values extends string | number, O extends {}>(target: T, augmentations?: O) {
return Object.assign(target, augmentations)
}
// for example when building a prototype object
const defaultConfig = Strictly(
{
state: 'MO', // "MO", not string
settingType: 'state-settings'
}, {
function AtPath<Target extends {}, Path1 extends keyof Target>(target: Target, path1: Path1): Target[Path1]
function AtPath<Target extends {}, Path1 extends keyof Target, Path2 extends keyof Target[Path1]>(target: Target, path1: Path1, path2: Path2): Target[Path1][Path2]
function AtPath<Target extends {}, Path1 extends keyof Target, Path2 extends keyof Target[Path1], Path3 extends keyof Target[Path1][Path2]>(target: Target, path1: Path1, path2: Path2, path3: Path3): Target[Path1][Path2][Path3]
function AtPath(target: {}, ...paths: string[]) {
let result = target as any;
for (let path of paths) {
result = result[path];
if (result === void 0) {
return result;
}
function StrEnum<Elements extends string[]>(...elements: Elements) {
const result = {} as { [K in Elements[number]]: K };
elements.forEach(e => {
(result as any)[e] = e
});
return result;
}
new RootBuilder().addFirewall('x').addNode('z', b => b.withFirewall('x')); // the only valid value here is 'x'
addNode<NodeName extends string, ReturnType extends AddNodeName<this, NodeName>>(
 nodeName: Exclude<NodeName, NodeNames>,
 builderCb: (b: NodeBuilder<this>) => NodeBuilder<this>
 ): ReturnType {
 // Yes you can totally infer the result type of builderCb and stick it in a RootBuilder generic param,
 // but I am not covering that here. ask if you want samples :)
 const result = builderCb(); 
 return this as RootBuilder as ReturnType
 }
type AddNodeName<Builder, NodeName extends string> =
 Builder extends RootBuilder<infer FirewallNames, infer NodeNames, infer NodesVolumes, infer DockerServiceNames>
 ? RootBuilder<FirewallNames, NodeNames | NodeName, NodesVolumes, DockerServiceNames>
 : never
withFirewall<FirewallName extends InferFirewallNames<Builder>>(firewallName: FirewallName) {
 // do builder things
 return this;
 }
class NodeBuilder<
 Builder
 > { }