JeyP91

7734078?v=4

JeyP91
: JeyP91

About me

Nothing here yet. Update your profile at /profiles/JeyP91.adoc

Day 00: groovy

Groovy

Here is another solution written in Groovy.

For the the time being I keep it simple and just call println.

println "Hello World"

Groovy

Part 1

  1. Iterate over text file with all masses.

  2. For each mass calculate the fuel

  3. Sum fuel and print

int calcFuel(int mass) {
    return (mass / 3).trunc() - 2
}

Part 2

  1. Iterate over text file with all masses.

  2. For each mass calculate the fuel

  3. Recoursively recalculate additional fuel for transporting fuel

  4. Sum fuel and print

    while (tempFuel > 0) {
        tempFuel = calcFuel(tempFuel) < 0 ? 0 : calcFuel(tempFuel)
        fuel += tempFuel
    }

Groovy

Part 1

  1. Iterate over text file with all masses.

  2. For each mass calculate the fuel

  3. Sum fuel and print

Part 2

  1. Iterate over text file with all masses.

  2. For each mass calculate the fuel

  3. Recoursively recalculate additional fuel for transporting fuel

  4. Sum fuel and print


Advent of Code - Day 05

Task is to check for the lowest distance to an intersection between to wire from a given starting point. Part 1 is the absolute distance through air between starting point and intersection. Part 2 is by following the wires to the first intersection.

Convert input to absolute sections

First step is to convert the input (R1,U2,L3) to wire sections with an absolute start coordinate and absolute end coordinate.

For this a class is used, which converts the string of one section provides some useful methods like orientation of the wire section or if a coordinate is located on the section.

Calculate intersections between the two wires

Seconds step is to calculate all intersections between the two wires. For this two loops are used which check for overlap of the coordinates.

Calculate distance from origin of the two wires to intersections

Calculate the absolute through air distance by just adding up the absolutes of the coordinates.

intersections.each { Coordinate coor ->
    int distance = Math.abs(coor.getX()) + Math.abs(coor.getY())
    if(shortestDistance == null || distance < shortestDistance) {
        shortestDistance = distance
    }
}

For calculating the manhatten distance each wire has to be followed until the intersection is found.

    int length = 0
    for(int i = 0; i < sections.size(); i++) {
        if(sections[i].isCoordinateOnSection(coor)) {
            if(coor.getX() == sections[i].getStartX() && coor.getX() == sections[i].getEndX()) {
                length += Math.abs(sections[i].getStartY() - coor.getY())
            }
            else if (coor.getY() == sections[i].getStartY() && coor.getY() == sections[i].getEndY()) {
                length += Math.abs(sections[i].getStartX() - coor.getX())
            }
            return length
        }
        else {
            length += sections[i].getLength()
        }
    }

Advent of Code - Day 04

Task is to check if six digit numbers are following certain rules and to find the total of these numbers in the range of 235741-706948.

Split number into single positions

To split a number into its single positions a combination of modulo and divions is used.

    positions[5] = Math.floor(number / 100000)
    positions[4] = Math.floor(number % 100000 / 10000)
    positions[3] = Math.floor(number % 10000 / 1000)
    positions[2] = Math.floor(number % 1000 / 100)
    positions[1] = Math.floor(number % 100 / 10)
    positions[0] = number % 10
Check rules

For each rule a method is implemented to evaluate the rule

Example: check if the digits do not decrease

    for(int i = 0; i < 5; i++) {
        if(positions[i] < positions [i+1]) {
            return false
        }
    }
    return true
While iterating over all numbers in range of 235741-706948, add them to the lists if the rules apply.
    if(checkDoubleNumber(positions) && checkNeverDecrease(positions)) {
        validNumbersPart1.add(number)
        if(checkIsolatedDoubleNumber(positions)) {
            validNumbersPart2.add(number)
        }
    }

Advent of Code - Day 05

Task is to add multiple additional opcodes to the Intcode computer of day 02.

Add opcode-parameters

Each instruction can have additional parameters now. To separate the actual opcode from the parameters modulo and division is used:

        this.opcode = Math.floor((instruction % 100))
        this.parameter1 = Math.floor((instruction % 1000) / 100)
        this.parameter2 = Math.floor((instruction % 10000) / 1000)
        this.parameter3 = Math.floor(instruction / 10000)

Instruction Execution

Each instruction execution basically consists out of 3 steps:

1. Calculate needed values

Here the interesting part is the usage of the parameters of the instruction. Depending on 0 or 1 either the value at the location is used, or the value is a pointer to another location from which the value shall be used.

            int par1 = inst.getParameter1() == 0 ? instructions[instructions[i+1]] : instructions[i+1]
            int par2 = inst.getParameter2() == 0 ? instructions[instructions[i+2]] : instructions[i+2]
            int resultLocation = instructions[i+3]
2. Execute instruction
            instructions[resultLocation] = par1 < par2 ? 1 : 0