Skip to content

Commit

Permalink
Advent of code day 8 and 9 added
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason committed Jun 10, 2021
1 parent 8f4efc3 commit 3dbe969
Show file tree
Hide file tree
Showing 11 changed files with 2,085 additions and 0 deletions.
Binary file added build/classes/AdventOfCodeDay8/NewMain.class
Binary file not shown.
Binary file added build/classes/AdventOfCodeDay8/Puzzle1.class
Binary file not shown.
Binary file added build/classes/AdventOfCodeDay8/Puzzle2.class
Binary file not shown.
Binary file added build/classes/AdventOfCodeDay9/Puzzle1.class
Binary file not shown.
Binary file added build/classes/AdventOfCodeDay9/Puzzle2.class
Binary file not shown.
647 changes: 647 additions & 0 deletions puzzleInput8.txt

Large diffs are not rendered by default.

1,000 changes: 1,000 additions & 0 deletions puzzleInput9.txt

Large diffs are not rendered by default.

114 changes: 114 additions & 0 deletions src/AdventOfCodeDay8/Puzzle1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package AdventOfCodeDay8;

import java.util.*;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*
* @author Strawluck
*/
public class Puzzle1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

ArrayList<String> inputArList = new ArrayList<String>(); // creates an array list to store the booklist.txt data
BufferedReader br = null;

try { // stores the data in booklist.txt in the array list
br = new BufferedReader(new FileReader("puzzleInput8.txt"));
String word;
while ((word = br.readLine()) != null) {
inputArList.add(word);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

String[] input = new String[inputArList.size()]; // converts the ArrayList to an Array
inputArList.toArray(input);

Map<Integer, Integer> lineCounter = new HashMap<>();
boolean isDone = false;
int accumulator = 0;
int lineNum = 0;


// A loop that continues until a line is recalled again
while(!isDone)
{
String[] instruction = input[lineNum].split(" ");

String operation = instruction[0];
char sign = instruction[1].charAt(0);
int argument = Integer.parseInt(instruction[1].substring(1));

/*
If the lineNum was never added to the hashmap, then add it. If it
has already been added, then it is being called for the second time so end the while loop
*/
if(lineCounter.get(lineNum) == null)
{
int i = 1;
lineCounter.put(lineNum, i);
} else{
isDone = true;
break;
}


if(operation.equals("acc"))
{

if(sign == '+')
{
accumulator += argument;
}
else if(sign == '-')
{
accumulator -= argument;
}
lineNum++;

}
else if(operation.equals("jmp"))
{
if (sign == '+')
{
/*if(lineNum == 647){
lineNum = 0;
}*/
lineNum += argument;
}
else if (sign == '-')
{
lineNum -= argument;
}
}
else if(operation.equals("nop"))
{
lineNum++;
}


}
System.out.println("Answer " + accumulator);
}
}
143 changes: 143 additions & 0 deletions src/AdventOfCodeDay8/Puzzle2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package AdventOfCodeDay8;

import java.util.*;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author Strawluck
*/
public class Puzzle2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ArrayList<String> inputArList = new ArrayList<String>(); // creates an array list to store the booklist.txt data
BufferedReader br = null;

try { // stores the data in booklist.txt in the array list
br = new BufferedReader(new FileReader("puzzleInput8.txt"));
String word;
while ((word = br.readLine()) != null) {
inputArList.add(word);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

String[] input = new String[inputArList.size()]; // converts the ArrayList to an Array
inputArList.toArray(input);

Map<Integer, Integer> lineCounter = new HashMap<>();

int accCorrupted = 0;


for(int i =0; i < input.length; i++)
{
System.out.println(i);
int accumulator = 0, lineNum = 0;
boolean isDone = false;

if(input[i].contains("acc"))
{
isDone = true;
}
while(!isDone)
{
//System.out.println("hello");
if (lineNum >= 647 || lineNum < 0) { // >= 647 because in the input array, it goes from 0-646 ever tho there's 647 lines of code. so input[647] doesn't exist
System.out.println(lineNum);
accCorrupted = accumulator;
System.out.println("Answer " + accCorrupted);
isDone = true;
i = input.length + 1;
break;
}
else
{
String[] instruction = input[lineNum].split(" ");

String operation;
char sign = instruction[1].charAt(0);
int argument = Integer.parseInt(instruction[1].substring(1));

if(input[i].contains("jmp") && lineNum == i)
{
operation = "nop";
}
else if(input[i].contains("nop") && lineNum == i)
{
operation = "jmp";
}
else
{
operation = instruction[0];
}

//System.out.println(instruction[1]);
if(lineCounter.get(lineNum) == null)
{
int j = 1;
lineCounter.put(lineNum, j);
} else{
//lineCounter.put(lineNum, lineCounter.get(lineNum)+1);
isDone = true;
break;
}


if(operation.equals("acc"))
{

if(sign == '+')
{
accumulator += argument;
}
else if(sign == '-')
{
accumulator -= argument;
}
lineNum++;

}
else if(operation.equals("jmp"))
{
if (sign == '+')
{
lineNum += argument;
}
else if (sign == '-')
{
lineNum -= argument;
}
}
else if(operation.equals("nop"))
{
lineNum++;
}
}
}
lineCounter.clear();
}

}

}


87 changes: 87 additions & 0 deletions src/AdventOfCodeDay9/Puzzle1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package AdventOfCodeDay9;

import java.util.*;
import java.io.*;
/**
*
* @author Strawluck
*/
public class Puzzle1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ArrayList<String> input = new ArrayList<String>(); // creates an array list to store the booklist.txt data
BufferedReader br = null;
int int1, int2, sum;

try { // stores the data in booklist.txt in the array list
br = new BufferedReader(new FileReader("puzzleInput9.txt"));
String word;
while ((word = br.readLine()) != null) {
input.add(word);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

// Created the preamble that is using the first 25 numbers in the input
ArrayList<Integer> preamble = new ArrayList<Integer>();
for(int i = 0; i < 25; i++)
{
preamble.add(Integer.parseInt(input.get(i)));
}

/*
Starts from the 26th number and finds the first num that can't be added
by two of the 25 previous numbers in the preamble to equal the num.
*/
for(int i = 25; i < input.size(); i++)
{
boolean valid = false;
int nextNum = Integer.parseInt(input.get(i));

// Finds if any 2 numbers in the preamble can be added to equal nextNum
for(int j = 0; j < preamble.size(); j++)
{
for(int k = j+1; k < preamble.size(); k++)
{
int1 = preamble.get(j);
int2 = preamble.get(k);
sum = int1 + int2;

// If the sum = nextNum, then remove the first number in the preamble
// Add nextNum to the preamble (to keep the 25 previous numbers in the preamble)
if(sum == nextNum){
valid = true;
preamble.remove(0);
preamble.add(nextNum);
j = preamble.size(); // better way than this? to break out of outer for loop
break;
}
}
}

// No 2 nums in the preamble summed up to equal nextNum so that num is not valid
if(valid == false){
System.out.println(nextNum);
break;
}
}

}

}
Loading

0 comments on commit 3dbe969

Please sign in to comment.