Advent of Code 2018

I find this challenge on twitter: adventofcode.com

Awesome challenge! I decided to manage with it. But for the beginner is very hard! After 5 days java lessons I couldn’t resolve second advent day! But I am not going to quit it! I think this is good opportunity to learn thinking as a developer. So after 7 days java lessons I resolve second advent day! I am very proud of myself. 

To be honest,  for one day adventOfCode You get two puzzles. One puzzle is for silver star (basic and easier puzzle and You have to resolve it!) second is for gold star (much difficult puzzle but this is additional puzzle) I didn’t resolve second puzzles in first and second day. Maybe later I will come back and resolve them. So I going to do this right now! 

  1. first day of advent code: title Frequency: https://adventofcode.com/2018/day/1

My code is: 

 
/*
 * Copyright (c) 2018. by 10bashi - I just started 
learning programming. So please forgive for my mistakes!
 */

package advent.of.code.day1;

public class Frequency {
    private static int frequency = 0;
    private static String input = "-14\n" +
            //you should paste in Your input here
12\n" +
            "-114806";
    private String[] tableOfFrequency;
    private int[] intTable;
    private int[] frequences;

    public static void main(String[] args) {
        zmienTablice();
        wpiszDoTablicy();
    }


    private static void zmienTablice() {
        String[]table = input.split("\n");
        tableOfFrequency = new String[table.length];
        intTable = new int[table.length];
        for (int i = 0; i<table.length; i++) {
            intTable[i] = Integer.parseInt(table[i]);
        }
    }

    private static void wpiszDoTablicy() {
        int i = 0;
        for (int b : intTable) {
            frequency = frequency + (b);
            i++;
        }
        System.out.println(i);
        frequences = new int[i - 1];
        System.out.println(frequences);
        checkExist(frequency);
    }

    private static boolean checkExist(int a) {

        for (int b : frequences) {   // for each
            if (b == a) {
                System.out.println(true);
                return true;
            }
        }
        System.out.println(false);
        return false;
    }
}
 

2. second day of advent code – title: Checksum: https://adventofcode.com/2018/day/2

/*
 * Copyright (c) 2018. by 10bashi - I just started learning programming. So please forgive for my mistakes!
 */

package advent.of.code.day2;

import java.util.HashMap;
import java.util.Map;

public class Checksum {

   private int allTwo = 0;
    private int allThree = 0;
   private int checksum = 0;

    private static String input = "ubkfmdjxyzlbgkrotcepvswaqx\n" +
// you should paste in Your input here - You can get in from url 
            "uikfmdkuyzlbgerotcepvswaqh\n" ;

    public static void main(String[] args) {

        String[] data = input.split("\n");

        for (int i = 0; i<data.length; i++) {
            System.out.println(i + " /////////");
            outPutMap2(countNumberOfLetters(data[i]));

        }

    }

    private static void outPutMap2(Map<Character, Integer> map) {

        int wystapenieDwochTakichSamychLiter = 0;
        int wystapienieTrzechTakichSamychLiter = 0;
        for (Map.Entry<Character, Integer>entry : map.entrySet()) {  
            if (entry.getValue() == 2) {
                wystapenieDwochTakichSamychLiter++;
            }
            if (entry.getValue() == 3) {
                wystapienieTrzechTakichSamychLiter++;
            }

        }

        if (wystapenieDwochTakichSamychLiter <= 1) {
            allTwo += 1;
        }
        if (wystapienieTrzechTakichSamychLiter>= 1) {
            allThree += 1;
        }


        checksum = allTwo * allThree;
        System.out.println("ilość wystąpienia 2 takich samych liter: " + wystapenieDwochTakichSamychLiter);
        System.out.println("ilośc wystąpenia 3 takich samich liter: " + wystapienieTrzechTakichSamychLiter);

        System.out.println("suma wszystkich par:" + allTwo);
        System.out.println("suma wszystkich potrójnych :" + allThree);

        System.out.println("checksum is: " + checksum);
    }

    private static Map<Character, Integer> countNumberOfLetters(String text) {
        Map<Character, Integer>map = new HashMap<>();
        for (int i = 0; i <text.length(); i++) {
            char s = text.charAt(i);
            if (map.get(s) == null) {
                map.put(s, 1);
            } else {
                //int liczbaWystapein = map.get(s);
                map.put(s, map.get(s) + 1); 
            }
        }
        return map;
    }

}