Skip to content

Instantly share code, notes, and snippets.

@roccodev
Last active December 19, 2017 14:03
Show Gist options
  • Select an option

  • Save roccodev/8599eb8b317763ea699635e4e717134b to your computer and use it in GitHub Desktop.

Select an option

Save roccodev/8599eb8b317763ea699635e4e717134b to your computer and use it in GitHub Desktop.
AdventOfCode2017/Day19
ArrayList<String> bloccs = new ArrayList<String>(
Files.readAllLines(Paths.get(new File("res/day19").getPath())).stream().collect(Collectors.toList()));
Step step = new Step(bloccs.get(0).indexOf('|'), 0);
Grid grid = new Grid(bloccs);
Dir dir = Dir.SOUTH;
StringBuilder seen = new StringBuilder();
while(true) {
Step next = step.add(new Step(dir.getX(), dir.getY()));
if(grid.limited(step) || grid.isLetter(step)) {
if(grid.isLetter(step)) seen.append(grid.mark(step));
boolean dest = true;
for(Dir d : Dir.values()) {
Step next1 = step.add(new Step(d.getX(), d.getY()));
if(d != dir.getOpposite() && grid.inGrid(next1) && grid.mark(next1) != ' ') {
dir = d;
next = next1;
dest = false;
break;
}
}
if(dest) break;
}
step = next;
if(!grid.inGrid(step)) break; // Check if we're done
}
System.out.println(seen.toString().trim());
ArrayList<String> bloccs = new ArrayList<String>(
Files.readAllLines(Paths.get(new File("res/day19").getPath())).stream().collect(Collectors.toList()));
Step step = new Step(bloccs.get(0).indexOf('|'), 0);
Grid grid = new Grid(bloccs);
Dir dir = Dir.SOUTH;
StringBuilder seen = new StringBuilder();
int steps = 0;
while(true) {
Step next = step.add(new Step(dir.getX(), dir.getY()));
steps++;
if(grid.limited(step) || grid.isLetter(step)) {
if(grid.isLetter(step)) seen.append(grid.mark(step));
boolean dest = true;
for(Dir d : Dir.values()) {
Step next1 = step.add(new Step(d.getX(), d.getY()));
if(d != dir.getOpposite() && grid.inGrid(next1) && grid.mark(next1) != ' ') {
dir = d;
next = next1;
dest = false;
break;
}
}
if(dest) break;
}
step = next;
if(!grid.inGrid(step)) break; // Check if we're done
}
System.out.println(steps);
public enum Dir {
NORTH(0, -1), SOUTH(0, 1), WEST(-1, 0), EAST(1, 0);
private int x, y;
Dir(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Dir getOpposite() {
switch(this) {
case NORTH:
return SOUTH;
case SOUTH:
return NORTH;
case WEST:
return EAST;
case EAST:
return WEST;
}
return SOUTH;
}
}
public class Grid {
private List<String> ting = new ArrayList<String>();
public Grid(List<String> ting) {
this.ting = ting;
}
public char mark(Step step) {
return ting.get(step.getY()).charAt(step.getX());
}
public boolean inGrid(Step step) {
return 0 <= step.getX() && step.getX() < ting.get(0).length() && 0 <= step.getY() && step.getY() < ting.size();
}
public boolean limited(Step step) {
return mark(step) == '+';
}
public boolean isLetter(Step step) {
return Character.isLetter(mark(step));
}
}
public class Step {
private int x, y;
public Step(int x, int y) {
this.x = x;
this.y = y;
}
public Step add(Step z) {
return new Step(x + z.x,y + z.y);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment