advent of code challenge, 2022 day 25.
This commit is contained in:
Binary file not shown.
+82
-2
@@ -1,3 +1,8 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Scanner;
|
||||
import java.io.File;
|
||||
|
||||
public class App {
|
||||
|
||||
/**
|
||||
@@ -19,7 +24,34 @@ public class App {
|
||||
* @return starting number of the longest Collatz Sequence
|
||||
*/
|
||||
private static int collatzSequence() {
|
||||
return 1; // TODO
|
||||
int n = 1;
|
||||
int maxChain = 0;
|
||||
int correctNum = 0;
|
||||
|
||||
while (n < 1_000_000) {
|
||||
int chainLength = collatz(n, 0);
|
||||
|
||||
if (chainLength > maxChain) {
|
||||
maxChain = chainLength;
|
||||
correctNum = n;
|
||||
}
|
||||
|
||||
n--;
|
||||
}
|
||||
|
||||
System.out.println(correctNum);
|
||||
return correctNum;
|
||||
}
|
||||
|
||||
private static int collatz(int n, int chainLength) {
|
||||
if(n == 1) return ++chainLength; // Base case and final link.
|
||||
|
||||
if(n % 2 == 0) n /= 2;
|
||||
else n = (3 * n) + 1;
|
||||
|
||||
++chainLength;
|
||||
|
||||
return collatz(n, chainLength);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,17 +92,65 @@ public class App {
|
||||
* @return the total of the name scores.
|
||||
*/
|
||||
private static int nameScores() {
|
||||
return 1; // TODO
|
||||
ArrayList<String> names = readNames();
|
||||
int sum = 0;
|
||||
|
||||
Collections.sort(names);
|
||||
|
||||
int counter = 1;
|
||||
for (String name : names) {
|
||||
int nameScore = 0;
|
||||
char[] n = name.toCharArray();
|
||||
for (char c : n) {
|
||||
nameScore += (c - 64);
|
||||
}
|
||||
|
||||
nameScore *= counter;
|
||||
sum+= nameScore;
|
||||
|
||||
counter++;
|
||||
}
|
||||
|
||||
System.out.println("sum of name scores: " + sum);
|
||||
return sum;
|
||||
}
|
||||
|
||||
private static ArrayList<String> readNames() {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
|
||||
File f = new File("names.txt");
|
||||
|
||||
try (Scanner scan = new Scanner(f)) {
|
||||
String str = scan.nextLine();
|
||||
|
||||
String[] strings = str.split(",");
|
||||
|
||||
for (String s : strings)
|
||||
list.add(s.substring(1, s.length() - 1));
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error reading file.");
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private static void printNames(ArrayList<String> names) {
|
||||
for (String name : names)
|
||||
System.out.println(name);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
//System.out.println(collatz(13, 0));
|
||||
if(collatzSequence() != 837799) System.out.println("Your solution is incorrect...");
|
||||
else System.out.println("Your Collatz Sequence solution is correct!");
|
||||
|
||||
/*
|
||||
if(countingSundays() != 171) System.out.println("Your solution is incorrect...");
|
||||
else System.out.println("Your Counting Sundays solution is correct!");
|
||||
|
||||
if(nameScores() != 871198282) System.out.println("Your solution is incorrect...");
|
||||
else System.out.println("Your Name Scores solution is correct!");
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
+5163
File diff suppressed because it is too large
Load Diff
@@ -2,3 +2,5 @@
|
||||
|
||||
javac *.java
|
||||
java App
|
||||
|
||||
rm *.class
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
Gui g = new Gui();
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.Color;
|
||||
|
||||
public class Button extends JButton {
|
||||
private boolean selected;
|
||||
private Color bg;
|
||||
private Color fg;
|
||||
|
||||
/**
|
||||
* @param String buttonLabel
|
||||
* @param Color fg
|
||||
* @param Color bg
|
||||
*/
|
||||
public Button(String buttonLabel, Color bg, Color fg) {
|
||||
super(buttonLabel);
|
||||
this.bg = bg;
|
||||
this.fg = fg;
|
||||
|
||||
applyCommon();
|
||||
deselect();
|
||||
}
|
||||
|
||||
// Apply default appearance to the button upon exiting the bounds.
|
||||
public void deselect() {
|
||||
selected = false;
|
||||
setBackground(bg);
|
||||
setForeground(fg);
|
||||
}
|
||||
|
||||
// Apply focused appearance to the button upon hover.
|
||||
public void select() {
|
||||
selected = true;
|
||||
setBackground(fg);
|
||||
setForeground(bg);
|
||||
}
|
||||
|
||||
// Apply hover functionality to all buttons, as well as removing certain default stylings.
|
||||
private void applyCommon() {
|
||||
// TODO: Implement hover functionality.
|
||||
|
||||
setFocusPainted(false);
|
||||
setBorder(new EmptyBorder(3,3,3,3));
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import javax.swing.*;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class Gui extends JFrame {
|
||||
public Gui() {
|
||||
super("Java Swing Challenge");
|
||||
init();
|
||||
|
||||
add(new Panel(Color.DARK_GRAY, Color.WHITE));
|
||||
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private void init() {
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
setLayout(new BorderLayout());
|
||||
setSize(new Dimension(750, 550));
|
||||
setResizable(false);
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error with cross platform look and feel.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -1,20 +0,0 @@
|
||||
import javax.swing.*;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class Panel extends JPanel {
|
||||
private Color bg;
|
||||
private Color fg;
|
||||
|
||||
public Panel(Color bg, Color fg) {
|
||||
super();
|
||||
this.bg = bg;
|
||||
this.fg = fg;
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
// TODO: Add 2 buttons to the Panel object, and set the background and foreground colors.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
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,35 @@
|
||||
/**
|
||||
* 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.
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
#!/bin/zsh
|
||||
|
||||
javac *.java
|
||||
java App
|
||||
java SNAFUChallenge
|
||||
|
||||
rm *.class
|
||||
|
||||
Reference in New Issue
Block a user