week 2 challenge
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
/SNAFU$Input$SNAFUIterator.class
|
||||
/SNAFU$Input.class
|
||||
/SNAFU.class
|
||||
/SNAFUChallenge.class
|
||||
@@ -0,0 +1,128 @@
|
||||
1-201-=2
|
||||
210--1211001
|
||||
120
|
||||
1==1-=1=-1=-=1-==-
|
||||
1--
|
||||
1-1-2221=-
|
||||
1021=
|
||||
2-=-21==02
|
||||
1-
|
||||
1-222122-0==020
|
||||
1-20122---12=2112-2=
|
||||
221
|
||||
12=220-2-=1=
|
||||
21
|
||||
1-20
|
||||
1-0=-0-2---
|
||||
10-=221=-0102
|
||||
212-020-1=00
|
||||
201=020=-1=
|
||||
2=0=01=2==
|
||||
2=1=0=11-2===20--
|
||||
2-0-221
|
||||
1--10=--1-0-2
|
||||
100=-11222=1
|
||||
11-=1
|
||||
11=--=1012--02-
|
||||
121
|
||||
1=11=1
|
||||
1==2---002--11=-1-
|
||||
11=0==--0
|
||||
2==0110=2=1==
|
||||
221=-=-=-02
|
||||
12-10
|
||||
2=
|
||||
2-0-=2
|
||||
1=0-0=2
|
||||
10==-2210=-=202
|
||||
2=-21
|
||||
2=-=1220
|
||||
201=2==1
|
||||
1==1=00-1-=
|
||||
1200-00-00-21-=-=0
|
||||
1=0=-22-200=-2-=--
|
||||
1==-=--12-1112-22
|
||||
1=0=-00====102
|
||||
20222010-1
|
||||
1---
|
||||
20--==2-201-
|
||||
1=1
|
||||
120=02=-=1-0=1
|
||||
1=-210-1=1
|
||||
1110--2
|
||||
1102-1=2----201=-
|
||||
1=01=2-01
|
||||
12=2--0-1001
|
||||
12=1
|
||||
102
|
||||
2=1=-11
|
||||
12=001022
|
||||
2=1=1=-01==
|
||||
101---=1--
|
||||
10=00001-0122=0
|
||||
12=-=--11=112=0202
|
||||
1-122====012100
|
||||
212=020=1022-0
|
||||
10=22=2
|
||||
1===212-02=1
|
||||
2=-2===-01010-121
|
||||
122=10
|
||||
2-0-1=-0--1001=-2
|
||||
1121
|
||||
1-21--100=1=2=1=2-
|
||||
1==0
|
||||
1=-0=0-2=0-=1===0
|
||||
2-02-==
|
||||
20-=-2022===-222
|
||||
1=1100-1
|
||||
12=-1=-02-2-20-=1-=
|
||||
10=
|
||||
1=1-==2
|
||||
101-=01221-==-2-=-
|
||||
12-=111=2=11-=1212
|
||||
12==202=121
|
||||
22022=000-121001-
|
||||
12=2
|
||||
1==0==1--1
|
||||
222
|
||||
10
|
||||
12-0122=01-
|
||||
10-0=01120
|
||||
210-0===-011=-=12=
|
||||
1=-1=0=2-102--0
|
||||
10111000--0-===0
|
||||
1=0-=-2-
|
||||
2-==22100
|
||||
1-2=-2-1-020=-2-
|
||||
1=001-1=02=--2
|
||||
1==0-211-21=-
|
||||
1-02=2=--221=-012==
|
||||
1-011-221-0=
|
||||
10-20=
|
||||
1=-0--=
|
||||
10-=022101=1--22
|
||||
2-0-1202==-==-
|
||||
1=0-
|
||||
11=
|
||||
2200
|
||||
2-22-212==-0==2=
|
||||
1012
|
||||
1=2=12===2-=
|
||||
1==211-02-
|
||||
1-001==2101220
|
||||
2-001
|
||||
10-=2-=
|
||||
102110==21=12--02
|
||||
1-=1122212002=010
|
||||
1-10-201=221=021-
|
||||
2-00=2221
|
||||
2--1
|
||||
11=12=0
|
||||
2-21=-
|
||||
20--2-1=22220-0
|
||||
2=22=1-02=21
|
||||
10==10112=1
|
||||
1-2=0=0=02==
|
||||
2201=2
|
||||
10-=00121
|
||||
10-012-12
|
||||
@@ -0,0 +1,85 @@
|
||||
package fall23.Week13;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Provides code for the input and testing of the SNAFU Challenge. This
|
||||
* challenge and input is from Day 25, year 2022 from AdventOfCode.com.
|
||||
*/
|
||||
public class SNAFU {
|
||||
/**
|
||||
*
|
||||
* @return returns an Iterable form of the input.
|
||||
*/
|
||||
public static Iterable<String> iterableInput() {
|
||||
return new Input();
|
||||
}
|
||||
|
||||
/**
|
||||
* Classes that implement Iterable tell Java that the class can be trusted to
|
||||
* have an Iterator. A very common and nice use is in for loops. See, Arrays and
|
||||
* a number of other datastructures implement Iterable, and you can stick any
|
||||
* Iterable class in the for-each loop.
|
||||
*/
|
||||
private static class Input implements Iterable<String> {
|
||||
/**
|
||||
* This is the only method required by Iterable.
|
||||
*/
|
||||
@Override
|
||||
public Iterator<String> iterator() {
|
||||
return new SNAFUIterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the manual version of the for each loop. There are two methods
|
||||
* inside. Next() gives the next input (and iterates). HasNext() is called
|
||||
* before to check if Next() can be safely run. The for loop basically keeps
|
||||
* calling Next() while HasNext() is true. Next() is actually the input given to
|
||||
* the left half of the for each loop.
|
||||
*
|
||||
* In this case, this Iterator keeps going until the next line it has ready is
|
||||
* null.
|
||||
*/
|
||||
private class SNAFUIterator implements Iterator<String> {
|
||||
private final static String PATH = "Input.txt";
|
||||
|
||||
private BufferedReader reader;
|
||||
private String currLine;
|
||||
|
||||
private SNAFUIterator() {
|
||||
try {
|
||||
reader = new BufferedReader(new FileReader(PATH));
|
||||
iterateToNextLine();
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("File Not Found");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void iterateToNextLine() {
|
||||
try {
|
||||
currLine = reader.readLine();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error With Reader");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return (currLine != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String next() {
|
||||
String returnStr = currLine;
|
||||
iterateToNextLine();
|
||||
return returnStr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package fall23.Week13;
|
||||
|
||||
/**
|
||||
* Full Instructions and Explanation: https://adventofcode.com/2022/day/25
|
||||
*
|
||||
* Find the SNAFU sum of the SNAFUInput.txt file.
|
||||
*/
|
||||
public class SNAFUChallenge {
|
||||
public static void main(String[] args) {
|
||||
String expectedSnafu = "2-02===-21---2002==0";
|
||||
|
||||
long sum = 0;
|
||||
for (String lineOfSNAFU : SNAFU.iterableInput()) {
|
||||
System.out.println(lineOfSNAFU);
|
||||
sum += decode(lineOfSNAFU);
|
||||
}
|
||||
|
||||
System.out.println("=== RESULT ===");
|
||||
System.out.println("result : " + encode(sum));
|
||||
System.out.println("expected result : " + expectedSnafu);
|
||||
}
|
||||
|
||||
private static String encode(long num) {
|
||||
return ""; // TODO: Convert a long into SNAFU.
|
||||
}
|
||||
|
||||
/**
|
||||
* The easy part. Go right to left on the SNAFU string and add the number to the
|
||||
* sum. Translates from SNAFU base-5 to base-10.
|
||||
*
|
||||
* @param SNAFU The SNAFU base-5 number code to decode.
|
||||
* @return The base-10 result
|
||||
*/
|
||||
private static long decode(String SNAFU) {
|
||||
return 0l; // TODO: Convert a String of SNAFU into a long.
|
||||
}
|
||||
}
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/bin/zsh
|
||||
|
||||
javac *.java
|
||||
java SNAFUChallenge
|
||||
|
||||
rm *.class
|
||||
Reference in New Issue
Block a user