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;
    }

}
 

Lesson #1 – headache from Java Basics

So, I`m after first lesson of Java Basics. What can I say? It will be tuff road to achieve goal.

For now we have theory with some small coding. Coach introduced a lot of material that U can find in Udemy Java Basics. He explain everything from beginning. Said about basics type, methods, packages, basic math operations, declaration of class type, method type, differences of private, public, protected. But He always repeat – “You will learn about it later, we will talk about the details later!” So to be honest I cant wait for the weekend to go to school and learn.

Tip for beginner.  You should watch Udemy course before Your course. It will be much more easier to keep up with coach.

But I know theory is theory practice is practice! After 7 hours (sic) of lessons I get headache but I know  it`s worth it!

I write two tiny codes:

  1. Calculator:
  2. Homework

Calculator:

package day1;

public class Calculator {

    static int a = 5; //declaration class type
    static int b = 6; //declaration class type
    
    public static int sum() { //declaration method
    }

    public static int substraction() { // return method
        return a - b;
    }

    public static int multiplitaction() {
        return a * b;
    }

    public static double division() {
        double c = (double)a/b;  // variable projection 
        return c;
    }

    public static void main(String[] args) {
        int resultSum = sum();
        System.out.println("sum " + resultSum);
        int resultSubstraction = substraction();
        System.out.println("substraction " + resultSubstraction);
        int resultMultiplication = multiplitaction();
        System.out.println("multiplication " + resultMultiplication);
        double resultDivision = division();
        System.out.println("division " +  resultDivision);
    }
}

Homework – at first glance I thought that is easy to do. But after trying to write it I had to think a lot and I get headache 😀

Topic: Write method accepting two arguments int type and which returns result of divide those types in a fraction format. Example: 1 2/5. And You have to remember when You divide two int You get int and You cant division by zero. Catchy isn`t it?

Answer:

package homeworks;
public class Homework1 {
    public static void main(String[] args) {
        System.out.println(divide(7, 5));
        System.out.println(divide(10, 5));
        System.out.println(divide(10, 0));
    }
    static String divide(int i, int j) {
        if (j != 0) { // check denominator it cant be 0
            int div = i / j;
            if (i % j == 0) { // check modulo, when it is 0 return string whit out fraction
                return "" + div;
            } else { // else modulo is not 0 your result should be fraction
                int mod = i % j;
                return "" + div + " " + mod + "/" + j;
            }
        } else {
            System.out.println("You have been trying to divide by zero");
            return "";
        }
    }
}

Two difficult things. First. Method should returns String! Why? Because You have to return division in fraction as example is 1 2/5 not 1.4. Second I didn`t know that You can combine String with int(double etc).

Example:

 return "" + someNumber + "/" + someNumber2

Modulo is very ancillary calculation. Coach said that we are going to use it a lot of time.

Check this:  7%5 = 2. You know after that Your result is fraction. So You should return total number ( 1 because 5 in 7 is 1) + modulo + denominator.  Know You will ask what about result for ex.  4/8 ? You should correct to 1/2. Yes, You have right. Know I am going to correct this!  Stay tuned!