Advent of code

Generates kismet code for Advent of Code 2021, day 1 part 1open in new window


import { readFile } from 'fs/promises'
import { NodeKismetFile, Items } from '../../src/index.js'

const { 
    Events: { LevelLoaded }, 
    Conditions: { CompareInt }, 
    Actions: { Int, AddInt, DrawText, ConvertToString },
    Variables: { Integer, Player, String }
} = Items

const file = new NodeKismetFile({ projectName: 'AoC2021' })

const input = (await readFile('input.txt', { encoding: 'utf8' }))
    .split('\r\n')
    .map(n => {
        return new Integer().setValue(Number(n))
    })

const length = new Integer().setName('length')
const one = new Integer().setName('one').setValue(1)

file.mainSequence.addItems([...input, length, one])

let lastCompare = null, lastAdd = null

for (let i = 1; i < input.length; i++) {   
    const int = input[i]
    const intmin1 = input[i - 1]
    const emptyInt = new Integer()

    const addLength = new Int()
        .setVariable('Value', emptyInt)
        .setVariable('Target', length)

    const addInt = new AddInt()
        .setVariable('A', one)
        .setVariable('B', length)
        .setVariable('IntResult', emptyInt)
        .addOutputConnection({ name: 'Out' }, { name: 'In', item: addLength })

    const compare = new CompareInt()
        .setVariable('A', intmin1)
        .setVariable('B', int)
        .addOutputConnection({ name: 'A < B'} , { name: 'In', item: addInt })
        
    file.mainSequence.addItems([emptyInt, compare, addInt, addLength])

    if (i === 1) {
        const onLoaded = new LevelLoaded().on('Loaded and Visible', {
            item: compare
        })

        file.mainSequence.addItem(onLoaded)
    } else if (i + 1 <= input.length) {
        lastAdd?.addOutputConnection({ name: 'Out' }, { item: compare, name: 'In' });

        lastCompare
            ?.addOutputConnection({ name: 'A == B' }, { item: compare, name: 'In' })
            .addOutputConnection({ name: 'A > B' }, { item: compare, name: 'In' })
    } 

    lastCompare = compare
    lastAdd = addLength
        
    if (i + 1 === input.length){
        const players = new Player()
            .setAllPlayer(true)
            
        const string = new String()

        const draw = new DrawText()
            .setVariable('Target', players)
            .setVariable('String', string, false);

        draw.getConnection('variable', 'String')?.setHidden(false);

        const convertedString = new ConvertToString()
            .setVariable('Inputs', length)
            .setVariable('Output', string)
            .addOutputConnection({ name: 'Out' }, { item: draw, name: 'Show' })

        file.mainSequence.addItems([players, string, draw, convertedString])

        const outlink = { item: convertedString, name: 'In' }
        compare.addOutputConnection({ name: 'A == B' }, outlink)
            .addOutputConnection({ name: 'A > B' }, outlink)

        addLength.addOutputConnection({ name: 'Out' }, outlink)
    }
}

await file.copy()