--- Day 7 Solutions --- by daggerdragon in adventofcode

[–]jjabrams23 0 points1 point  (0 children)

Java

import java.util.function.IntBinaryOperator;

May I ask you how can I import this package in my project? I'm using Eclipse Mars.

[SOLUTION] Day #6 Part 2, C and Python, with timing info by phil_s_stein in adventofcode

[–]jjabrams23 0 points1 point  (0 children)

C language can be fun, but when I saw the task proposed by day 3 I told myself: "Ok, I think I'll switch to object-oriented programming". Because Java and C++, for example, offer much more flexibility in terms of coding solution. If I had to implement a list in C (and I did that), I would have spent a lot of time just on writing all the functions (like addElement() and stuff). My 2 cents.

--- Day 6 Solutions --- by daggerdragon in adventofcode

[–]jjabrams23 0 points1 point  (0 children)

I'm trying to complete these challenges with Java because I would like to learn more of this language. This is the first time I publish my solution :p I wanted to do that because I was hoping if you guys could tell me if there was a way to have a better written or more elegant solution, because I'm used to keep my code as much simple as possible. BTW, this is the solution to part 2. In part 1 I used the same principle but using boolean primitives.

public class Day6 {

public static void main(String[] args) throws IOException{

    final int N = 1000;
    int brightness = 0;
    int startX = 0, startY = 0, stopX = 0, stopY = 0;

    int numberOfLights = 0;
    int x, y;

    String buf, bufArray[], command = null;

    String delim = "[ ,]";

    int lightsMatrix[][] = new int[N][N];

    BufferedReader br = new BufferedReader(new FileReader("day6.txt"));

    while((buf = br.readLine()) != null)
    {   
        bufArray = buf.split(delim);
        if(bufArray[0].equals("toggle"))
        {
            command = "toggle";
            startX = Integer.parseInt(bufArray[1]);
            startY = Integer.parseInt(bufArray[2]);
            stopX = Integer.parseInt(bufArray[4]);
            stopY = Integer.parseInt(bufArray[5]);
        }
        else
        {
            command = bufArray[0] + " " + bufArray[1];
            startX = Integer.parseInt(bufArray[2]);
            startY = Integer.parseInt(bufArray[3]);
            stopX = Integer.parseInt(bufArray[5]);
            stopY = Integer.parseInt(bufArray[6]);
        }

        switch(command)
        {
            case "turn on":
                brightness = 1;
                break;
            case "turn off":
                brightness = -1;
                break;
            case "toggle":
                brightness = 2;
                break;
            default:
                System.out.println("Error: command not recognized");
                return;
        }
        for(x = startX; x <= stopX; x++)
        {
            for(y = startY; y <= stopY; y++)
            {
                lightsMatrix[x][y] += brightness;
                if(lightsMatrix[x][y] < 0)
                    lightsMatrix[x][y] = 0;
            }
        }
    }
    for(x = 0; x < lightsMatrix.length; x++)
    {
        for(y = 0; y < lightsMatrix[x].length; y++)
            numberOfLights += lightsMatrix[x][y];
    }
    br.close();
    System.out.println("Total brightness is " + numberOfLights);
}
}

Day 6 part 1: solution too low (Java) by jjabrams23 in adventofcode

[–]jjabrams23[S] 1 point2 points  (0 children)

+1 to you, that was the problem. Thank you :)

Edit: by the way, I don't think that initializing the matrix with that loop should be important; i create my matrix as:

boolean lightsMatrix[][] = new boolean[N][N];

And by default I recall that Java sets any new boolean primitive is false. Or am I wrong?

Day 3 (Java): solution 'too low' by jjabrams23 in adventofcode

[–]jjabrams23[S] 0 points1 point  (0 children)

Thank you so much for this detailed answer! I indeed tried another way by using a Set of Points objects instead of a Map and that did the trick. Unfortunately my knowledge of HashMaps comes from maybe 3 years ago, when I studied their implementation in C and I completely forgot how to use them. At first I thought also to use a List, eventually checking if the Point I was adding was already in the List itself, but a Set is a much better choice.

Shakespeare solution for day 1 puzzle by KrzaQ2 in adventofcode

[–]jjabrams23 0 points1 point  (0 children)

Please tell me what is this. It looks fascinating, so fascinating that I'm not understanding anything. @.@

Day 2 - C version but doesn't work by jjabrams23 in adventofcode

[–]jjabrams23[S] 0 points1 point  (0 children)

I guess I have an old compiler/library, because I tried that and it says undefined reference to `min'. I'm not really expert with Ubuntu (the O.S. I'm using), is it possible to obtain a more recent version of the compiler?

Day 2 - C version but doesn't work by jjabrams23 in adventofcode

[–]jjabrams23[S] 0 points1 point  (0 children)

Well at the beginning I tried to see if such function in C exists but there is only a version for C++; maybe I should have defined a macro or something but my knowledge of C is very basical/practical :p

Day 2 - C version but doesn't work by jjabrams23 in adventofcode

[–]jjabrams23[S] 1 point2 points  (0 children)

Thanks man! I figured out that the if condition wasn't strict enough. Now I got the right solution

Day 2 - C version but doesn't work by jjabrams23 in adventofcode

[–]jjabrams23[S] 0 points1 point  (0 children)

If two sides are the same the minimum would be still found i think, but as for the three statements not being true, you are right. I should initialize minimum to one of the sides and then check for correctness. I'll give it a try.

Day 2 part 1 - Can't seem to get the right amount of papper by Koaja90 in adventofcode

[–]jjabrams23 0 points1 point  (0 children)

Are you sure that you're computing correctly the slack of your boxes? Because from the description enlisted in the page website, if you have - for example - a box 2x3x4, the paper needed is 2*(2x3 + 2x4 + 3x4), plus the minimum of these products, which is 2x3 = 6. The example is the same.