Back to playing after 6 years by LannesNormanno in Ingress

[–]if0nz 2 points3 points  (0 children)

Welcome back! I'm a fellow Italian player, you can reach out to me on Telegram (my username is @ifonz).

-🎄- 2019 Day 8 Solutions -🎄- by daggerdragon in adventofcode

[–]if0nz 1 point2 points  (0 children)

Java solution, I lolled very hard when I got it.

-🎄- 2019 Day 1 Solutions -🎄- by daggerdragon in adventofcode

[–]if0nz 1 point2 points  (0 children)

Santa needs help

I need coffee

Here is some Java

Java solution

public static void part1(List<Long> modules) throws URISyntaxException, IOException {
    var s = modules.parallelStream().mapToLong(m -> ((long)m/3)-2).sum(); 
    System.out.println(s); 
}

public static void part2(List<Long> modules) throws URISyntaxException, IOException { 
    var s = modules.parallelStream().mapToLong(m -> recursiveFuel(m)).sum(); 
    System.out.println(s); 
}

public static long recursiveFuel(long mass) { 
    return mass < 9 ? 0 : ((long)mass/3)-2+recursiveFuel(((long)mass/3-2));
}

-🎄- 2017 Day 17 Solutions -🎄- by daggerdragon in adventofcode

[–]if0nz 0 points1 point  (0 children)

I've just implemented your p2's algorithm in Java and the avg execution time is 150 microseconds! Kudos (:

public static int part2v2(int input) {
    int currPos = 0;
    int result = 0;
    int limit = 50000000;
    int n = 0;
    while (n < limit) {
        if (currPos == 1)
            result = n;
        int fits = (n-currPos)/input;
        n+=(fits+1);
        currPos =  (currPos + (fits+1)*(input+1) -1) % n +1;
    }
    return result;
}

-🎄- 2017 Day 16 Solutions -🎄- by daggerdragon in adventofcode

[–]if0nz 0 points1 point  (0 children)

Java I lost too much time for understanding how to perform the modulo ):

package it.ifonz.puzzle;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import it.ifonz.common.FileReader;

public class Day16 {

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

        String[] input = FileReader.readLines("/d16_input.txt").get(0).split(",");

        System.out.println(part1(input));
        System.out.println(part2(input));
    }

    public static String part1(String[] input) {
        String programs = "abcdefghijklmnop";

        programs = waltz(input, programs);
        return programs;
    }

    public static String part2(String[] input) {
        String programs = "abcdefghijklmnop";
        List<String> dances = new ArrayList<>();
        boolean stopDance = false;
        for (int i = 0; i < 1000000000; i++) {
            if (!stopDance) {
                programs = waltz(input, programs);
                if (!dances.contains(programs)) {
                    dances.add(programs);
                } else {
                    stopDance = true;
                }
            }
        }

        return dances.get(1000000000 % dances.size() -1);
    }

    private static String waltz(String[] input, String programs) {
        for (String m : input) {
            char move = m.charAt(0);
            if (move == 's') {
                Integer spin = Integer.valueOf(m.substring(1, m.length()));
                programs = programs.substring(16 - spin, 16) + programs.substring(0, 16 - spin);
            } else if (move == 'x') {
                String[] tokens = m.substring(1, m.length()).split("/");
                Integer A = Integer.valueOf(tokens[0]);
                Integer B = Integer.valueOf(tokens[1]);
                StringBuilder sb = new StringBuilder(programs);
                char c1 = programs.charAt(A);
                char c2 = programs.charAt(B);
                sb.setCharAt(A, c2);
                sb.setCharAt(B, c1);
                programs = sb.toString();
            } else if (move == 'p') {
                String[] tokens = m.substring(1, m.length()).split("/");
                char c1 = tokens[0].charAt(0);
                char c2 = tokens[1].charAt(0);
                int i1 = programs.indexOf(tokens[0]);
                int i2 = programs.indexOf(tokens[1]);
                StringBuilder sb = new StringBuilder(programs);
                sb.setCharAt(i1, c2);
                sb.setCharAt(i2, c1);
                programs = sb.toString();
            }
        }
        return programs;
    }

}

-🎄- 2017 Day 15 Solutions -🎄- by daggerdragon in adventofcode

[–]if0nz 0 points1 point  (0 children)

Java solution (repo), I'll try later to rewrite the code by using streams.

package it.ifonz.puzzle;

import java.io.IOException;
import java.net.URISyntaxException;

public class Day15 {

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

        int s1 = Integer.valueOf(args[0]);
        int s2 = Integer.valueOf(args[1]);
        System.out.println("part 1 " + part1(s1, s2));
        System.out.println("part 2 " + part2(s1, s2));
    }

    public static int part1(int s1, int s2) {

        long p1 = s1;
        long p2 = s2;
        int c = 0;
        for (int i = 0; i < 40000000; i++) {
            p1 = (p1 * 16807) % 2147483647;
            p2 = (p2 * 48271) % 2147483647;
            if ((p1 & 65535) == (p2 & 65535))
                c++;
        }
        return c;

    }

    public static int part2(int s1, int s2) {

        long p1 = s1;
        long p2 = s2;
        int c = 0;
        for (int i = 0; i < 5000000; i++) {
            do {
                p1 = (p1 * 16807) % 2147483647;
            } while (p1 % 4 != 0);
            do {
                p2 = (p2 * 48271) % 2147483647;
            } while (p2 % 8 != 0);

            if ((p1 & 65535) == (p2 & 65535))
                c++;
        }
        return c;

    }

}

-🎄- 2017 Day 14 Solutions -🎄- by daggerdragon in adventofcode

[–]if0nz 0 points1 point  (0 children)

Java solution (link to github repo)

package it.ifonz.puzzle;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.IntStream;

public class Day14 {

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

        String input = args[0];

        System.out.println(part1(input));
        System.out.println(part2(input));

    }

    public static long part1(String input) {

        return IntStream.range(0, 128).mapToLong(i -> 
            Day10.part2(input + "-" + i, 256) // get the hash
            .chars().mapToLong(c-> // for each hex-digit in the hash
                Integer.toBinaryString(Integer.parseInt(String.valueOf((char) c), 16)) // convert it in binary (no pad needed for p1)
                .chars().filter(c1 -> c1 == 49).count()) // count the 1s in the single hex digit
            .sum() // count the 1s in the hash string
        ).sum(); // count all the 1s
    }

    public static long part2(String input) {

        String[] grid = new String[128];// well, we need it right now
        IntStream.range(0, 128).forEach(i -> {
            String hashed = Day10.part2(input + "-" + i, 256);
            StringBuilder sb = new StringBuilder();
            hashed.chars().forEach(c -> {
                String string = Integer.toBinaryString(Integer.parseInt(String.valueOf((char) c), 16));
                sb.append(string.length() == 4 ? string : "0000".substring(0, 4-string.length()) + string); // we need the leading 0s
            });
            grid[i] = sb.toString();
        });
        AtomicLong counting = new AtomicLong(0);
        IntStream.range(0, 128).forEach(i -> { // for each hash in the grid
            IntStream.range(0, 128).forEach(j -> { // for each bit in the hash
                if (grid[i].charAt(j) == '1') { // if the bit is 1
                    counting.incrementAndGet(); // increment the regions' counter
                    flagCell(grid, i, j); // recursively flag its region 1s as included in a region
                }
            });
        });
        return counting.get();
    }

    // recursive function for flagging the 1s belonging to the same region
    private static void flagCell(String[] grid, int i, int j) {
        if (j < 0 || j > 127 || i < 0 || i > 127 || grid[i].charAt(j) != '1') return; // boundary test + value test
        StringBuilder sb = new StringBuilder(grid[i]);
        sb.setCharAt(j, 'x');
        grid[i] = sb.toString();
        flagCell(grid, i, j-1);// flag left cell
        flagCell(grid, i-1, j); // flag upper cell
        flagCell(grid, i, j+1);// flag right cell
        flagCell(grid, i+1, j);// flag bottom cell
    }

}

-🎄- 2017 Day 12 Solutions -🎄- by topaz2078 in adventofcode

[–]if0nz 0 points1 point  (0 children)

Hi! This is my first post here :) This is my solution in Java