This repository has been archived on 2023-12-02. You can view files and clone it, but cannot push or open issues or pull requests.
logan_2022/day4
2022-12-04 01:19:16 -07:00
..
advent_of_code_day4.txt Added Day 4 solutions in E2 2022-12-03 23:05:51 -07:00
advent_of_code_day4_part2.txt Added Day 4 solutions in E2 2022-12-03 23:05:51 -07:00
input.txt Added Day 4 solutions in E2 2022-12-03 23:05:51 -07:00
input2.txt Added Day 4 solutions in E2 2022-12-03 23:05:51 -07:00
README.md Added README to Day 4 because language is weird 2022-12-04 01:19:16 -07:00

This is my solution to the Day 4 puzzle in the 2022 Advent of Code.

It is written in a language called Expression 2, a scripting language for the Wiremod addon in Garry's Mod.

Due to the unusual nature of the language, here is an annotated verison of Part 1:

@name Advent of Code
@inputs
@outputs
# These variables persist through multiple executions of the script
@persist Input:array [I Overlaps]:number
@trigger

# When the file has successfully uploaded, run this script
runOnFile(1)

# Run this script every 100ms
interval(100)

# When you first place the E2 entity down, upload the input dataset to the server
if(first()) {
    fileLoad("input.txt")
}

# If the current reason the script is being executed is the file being uploaded, parse it.
if(fileClk()) {
    Input = fileRead():explode("\n")
    Input:removeString(Input:count())
    I = 1
    Overlaps = 0
}

# If the file is fully loaded and the index variable is below the maximum of the array, loop through it.
if(fileLoaded() && I <= Input:count()) {
    #printTable(Input)

    # This function will loop forever until the execution time limit for the current game tick is almost met, then the loop will break and wait until the next time the script is ran to continue
    while(perf()) {
        #print(Input[I,string])
        Thing = Input[I,string]:explode(",")

        Range1 = Thing[1,string]:explode("-")
        Range2 = Thing[2,string]:explode("-")

        if(Range1[1,string]:toNumber() >= Range2[1,string]:toNumber()
        && Range1[2,string]:toNumber() <= Range2[2,string]:toNumber()) {
            #print(I+": True1")
            Overlaps++
        } elseif(Range2[1,string]:toNumber() >= Range1[1,string]:toNumber()
        && Range2[2,string]:toNumber() <= Range1[2,string]:toNumber()) {
            #print(I+": True2")
            Overlaps++
        } else {
            #print(I+": False")
        }
        I++
        if(I > Input:count()) {
            break
        }
    }
}

# If the file is fully loaded and I is above the array size (meaning it is done), print the results
if(fileLoaded() && I > Input:count()) {
    print(Overlaps)
    print(I)
    runOnTick(0)
    stoptimer("interval")
}