Created
December 3, 2015 22:26
-
-
Save RGamberini/020a9fc895efab7da056 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package code.advent.day4; | |
/** | |
* Created by Nick on 12/3/2015. | |
*/ | |
public class Coordinate { | |
public int x; | |
public int y; | |
public Coordinate(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
public void add(Coordinate coordinate) { | |
this.x += coordinate.x; | |
this.y += coordinate.y; | |
} | |
@Override | |
public String toString() { | |
return "Coordinate{" + | |
"x=" + x + | |
", y=" + y + | |
'}'; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Coordinate that = (Coordinate) o; | |
if (x != that.x) return false; | |
if (y != that.y) return false; | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
int result = x; | |
result = 31 * result + y; | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package code.advent.day4; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.Scanner; | |
/** | |
* Created by Nick on 12/3/2015. | |
*/ | |
public class solve { | |
public static void main(String[] args) throws FileNotFoundException { | |
Scanner scan = new Scanner(new File("input.txt")); | |
String input = scan.nextLine(); | |
Coordinate coordinate = new Coordinate(0, 0); | |
HashMap<Character, Coordinate> hashMap = new HashMap<>(); | |
hashMap.put('^', new Coordinate(0,1)); | |
hashMap.put('v', new Coordinate(0,-1)); | |
hashMap.put('<', new Coordinate(-1,0)); | |
hashMap.put('>', new Coordinate(1,0)); | |
HashSet<Coordinate> hashSet = new HashSet(); | |
for (int i = 0; i < input.length(); i++) { | |
hashSet.add(new Coordinate(coordinate.x, coordinate.y)); | |
coordinate.add(hashMap.get(input.charAt(i))); | |
} | |
hashSet.add(new Coordinate(coordinate.x, coordinate.y)); | |
System.out.println(hashSet.size()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment