feb 2015 problem 1 / 3 / 4

This commit is contained in:
Xevion
2020-02-21 05:45:14 -06:00
parent bc11562f49
commit ba32bcbd62
6 changed files with 178 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
# Automaton
Run my solution on **[repl.it](https://repl.it/@Xevion/A-Computer-Science-February-2015-Automaton)!**
A rather simple problem, despite the overwhelming complicated background information to understand what it's talking about in the first paragraph.
I recommend skipping the first paragraph, and simply looking at the second one very carefully. In short, the entire problem is basically Regex.
All you have to do is manipulate the input data, create a function for matching, loop until you find a input that works, and you're done.
There was one big problem though I encountered, one that would have been a failed submission in the end.

View File

@@ -0,0 +1,39 @@
import static java.lang.System.out;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
class Automaton {
private String match;
private String input;
public Automaton(String match, String input) {
match = String.format("^%s$", match);
this.match = match.replace('U', '|');
this.input = input;
}
public String search() {
String cInput = this.input;
while(cInput.length() > 0) {
// out.println(String.format("%s %s", match, cInput));
if(cInput.matches(this.match))
if(cInput.length() == this.input.length())
return "YES";
else
return String.format("NO %s", cInput.length());
cInput = cInput.substring(0, cInput.length() - 1);
}
return "NO 0";
}
}
class Main {
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(new File("automaton.dat"));
while(s.hasNextLine()) {
Automaton fsa = new Automaton(s.next(), s.next());
out.println(fsa.search());
}
}
}

View File

@@ -0,0 +1,11 @@
# Climb
Run my solution on **[repl.it](https://repl.it/@Xevion/A-Computer-Science-February-2015-Climb)!**
This problem is very basic indeed, a simple while loop with two if statements is all one needs. Perhaps this could be made into an equation, but it's best if your pursue the simpler routes.
I added a `if statement` inside the `while loop` to make sure it didn't decrement following the ant reaching the summit of the hill, which would report an extra "climb" operation and a slightly higher distance traversed.
Taking your time on these Competition problems is not what is asked, as these are timed in real competition scenarios, so one should aim to simply solve the test cases, not solve world hunger while you're at it.
This problem is one of those that should be attempted first, to attain easy points immediately before starting difficult to solve problems.

View File

@@ -0,0 +1,29 @@
import static java.lang.System.out;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
class Main {
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(new File("climb.dat"));
for(int i = s.nextInt(); i > 0; i--) {
int up = s.nextInt();
int down = s.nextInt();
int hole = -1 * s.nextInt();
int attempts = 0;
int traverse = 0;
while(hole < 0) {
attempts++;
hole += up;
traverse += up;
if(hole < 0) {
hole -= down;
traverse += down;
}
}
out.println(String.format("%s %s", attempts, traverse));
}
}
}

View File

@@ -0,0 +1,20 @@
# Fate
Run my solution on **[repl.it](https://repl.it/@Xevion/A-Computer-Science-February-2015-Fate)!**
This one was interesting in all honesty. It's one of the few problems that stumped me, but I eventually got past. The rest simply stumped me and wouldn't budge.
I ended up implementing a much more robust class using ArrayLists, HashMaps, and Collections to support me.
My implementation essentially revolved around create a `Hand` object, using a `.add(String card)` method to add cards, which checked if it was above the specified card limit, and then consequently chose
one to remove. I used a `HashMap<String, Integer>` to count and then decide upon which card to remove.
I first used the cards as keys and counted up the value by 1 each time I iterated over it (once each in ArrayList). I then iterated over the `HashMap` using the `.keySet()` method (which returns a `Set` object you can iterate over easily) and simply found the card with the least count and the lowest card value.
Card Value was implemented using `String.compareTo`, which you'll remember is simply `this - that`, i.e. a capital letter `A` has a value of `65`, `B` has `66` and so on. As such, if `this - that` (e.g. 65 - 66) returned a negative value when comparing the new possible letter versus the current selected 'minimum card', I swapped as usual. This was all done in a single if statement.
After that, I simply used `ArrayList.remove()` to remove the String, which by the way, has `String.equals`, allowing one to remove without requiring a reference to the variable you pick.
Once completed, I began printing, which started with sorting the `ArrayList` using `Collections.sort`, which sorted it in "Alphabetical" format as the problem specified. Then, I printed to `sysout` in the correct format using `String.join(" ", array)` for simplicity.
A interesting problem, but it did stump me when I forgot to add necessary code - although I forget exactly *what* I forgot to add that made it difficult to debug...

View File

@@ -0,0 +1,68 @@
import static java.lang.System.out;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collections;
import java.io.File;
import java.io.IOException;
class Hand {
private ArrayList<String> hand;
Hand() {hand = new ArrayList<String>();}
Hand(String[] initial) {
this();
for(String card : initial)
hand.add(card);
}
public void print() {
Collections.sort(this.hand);
out.println(String.join(" ", this.hand));
}
public void add(String card) {
this.hand.add(card);
// Begin process to remove a card from hand if exceeding 5 card limit
if(this.hand.size() > 5) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
// Count each card into map
for(String next : this.hand)
if(map.get(next) != null)
map.put(next, map.get(next) + 1);
else
map.put(next, 1);
// Search for what card to remove
int min = 0;
String mintype = "";
for(String type : map.keySet()) {
int cur = map.get(type);
// if found is less, or found is equal, but foundtype is of less value than curmintype
if(cur < min || (cur == min && !mintype.equals(type) && type.compareTo(mintype) < 0) || mintype.equals("")) {
min = cur;
mintype = type;
}
}
// Remove card from map
hand.remove(mintype);
}
}
}
class Main {
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(new File("fate.dat"));
while(s.hasNextLine()) {
String input = s.nextLine();
Scanner r = new Scanner(input);
Hand hand = new Hand();
while(r.hasNext())
hand.add(r.next());
hand.print();
}
}
}