JeyP91
: JeyP91
JeyP91 |
Here is another solution written in Groovy.
For the the time being I keep it simple and just call println.
println "Hello World"
Iterate over text file with all masses.
For each mass calculate the fuel
Sum fuel and print
int calcFuel(int mass) {
return (mass / 3).trunc() - 2
}
Iterate over text file with all masses.
For each mass calculate the fuel
Recoursively recalculate additional fuel for transporting fuel
Sum fuel and print
while (tempFuel > 0) { tempFuel = calcFuel(tempFuel) < 0 ? 0 : calcFuel(tempFuel) fuel += tempFuel }
Iterate over text file with all masses.
For each mass calculate the fuel
Recoursively recalculate additional fuel for transporting fuel
Sum fuel and print
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.
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.
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 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()
}
}
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.
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
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
if(checkDoubleNumber(positions) && checkNeverDecrease(positions)) {
validNumbersPart1.add(number)
if(checkIsolatedDoubleNumber(positions)) {
validNumbersPart2.add(number)
}
}
Task is to add multiple additional opcodes to the Intcode computer of day 02.
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)
Each instruction execution basically consists out of 3 steps:
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]
instructions[resultLocation] = par1 < par2 ? 1 : 0
i = i + 4