Skip to content

Instantly share code, notes, and snippets.

@JBou
Created December 3, 2015 10:15
Show Gist options
  • Save JBou/c4f3e012926e16a759c7 to your computer and use it in GitHub Desktop.
Save JBou/c4f3e012926e16a759c7 to your computer and use it in GitHub Desktop.
AdventofCode - Day 3
import java.lang.Math;
import java.util.ArrayList;
public class HelloWorld
{
public static void main(String[] args)
{
int part = 2;
int curX = 0, curY = 0;
int curRoboX = 0, curRoboY = 0;
boolean b = false;
ArrayList<House> houses = new ArrayList<House>();
String input = "^><^>>>^<^v<v^^vv^><<^<><<vv^<>^<^v>^vv<>v><vv^^<>>^^^v<<vv><<^>^<^v<^>^v><<<v^<v<<<v<<vv<v<^><^>><>v>v^<<v^^<^v<><^>^<<^^^>v>>v^^<v>>^>vv><v>>^>>v^>^v>^<^^v>^>^^v<v>^^<v<>>v^^v><^><^<<>v^<^<^v<v>v^>>>v^v^>^<>^v<^^vv<v>^>^<>^^<vv^<><<v<^<^^>vv<>^>v<^>^v>v^>^v<>^><>><vv<>v^v<><>v^v>>>>v^^>^><^^<v<^><^<v>>^v^v<>v<<<^<<vvvv<<v^vv^>v^^^<^^^<v>>v<^v>>>>>v<^^^^>v<^<><v>>>>><v>>v^vvvv^^<v^<>^v<^v^>v><^>^v<<>>vv^>v>v^^>vv^<^vvv<>><>><><^^^<v<>^<^^^<v><^v>>v>^v<v^vv^<>^^^>v^^^v>>^v^^<^>>^>^<<v>>>^^<>>^vv>v^<^>>>><v<><><^^v<><<<<^^<>>^<vvv^><>v<v<<<<><v<<v>v<v^><vv<v^>^<^>v^^><^v>^^>v<>^v^<>^vv^><v^^vv>vvv>v>^<vv^>>^>>^>><>>>^^^^v<vv>^<>v^^><v^>^<>v<^^v><v<<><^v><>^^^^^v^v>>^^v><<><<vv>^^^^><^>v>><<<^v>v^^>^v^<^^v>v<^<<>>^v<<<v<<>>v<^v^><vv<v^v>v^<v>><v>^v<<<vv^>v<v>>v>>v><v><v^>v^^v>^v^>>>><>^>v>^v^>>>>v^<<vv<^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v>^v><vv>v<<^>^>><<^^^vvv<<^vv<^^>v^^vv^<^^<<^^>>^^<vv<v<<v^^<<v<^vvv<<><<v>v^>>v^^>v<^>^><v<^>v<v^v<v^^<>v>><<v^v^v<^^^><v>v><^<^vv>^^v>^>v<<^vv><^^^^^^><<^>>>^v<>^^v<<<>><<<v^><>^<<<v>v^>^^^<^><v>^^^v<<>v<v>^<v^>><<^^<<^v<<>^v>>vv>><v<^><v<<<vvv><vv><<^v^^<v^vvv<^v>>v^v<v^v^>>^^v<><^^^<^^>v>^<><v<<v^^>vvv^v^^<v<v^v>^>v^^v<^><v^^<<<<>^^>>^v<><^><^<<^vv^<><<>v^vv^<v^<><<<^^>v<<>>>v<>v<><<<v>^v>^^v>^^>v>^>^>v<>><>^>^>^vvvv<^<v^<>^^^^v>v>><<v>>^<vv>>^<v<^v^vv>><>^^>v^^<<><^<v>><<<<>v>^^><v^^v<<v<><vv^v>^<v^^>v<<<<v^v<<>>vv<v<<<v>v>>v<^v>>v>v^<<<>^>^>^<>v<^^vv><^v<<^v<vvv^vv>v<^<<^^vv^^>vv<^>v>^^<<v^<<^^v<>^>v<<^^<^>^^^v^^<v<^<^>>>v^vv^<^v>^<>^<^<v<^v>>>^<^v<><v<^vv<v>v><v^v^^v<vv><^^<><>^>v<^<^vv>><^v><v<>^<>^^>^<><<<v^>>^<>><<><v>vvv^<<^<vv<v><v<^<<<^>^>>v<^>>vv>^v^^^v<>v<>><>^vv^>vv^";
char[] splitted = input.toCharArray();
houses.add(new House(curX, curY));
if(part == 2)
houses.get(0).deliver();
step:
for (char c : splitted) {
int x, y;
b = !b;
if (b || part != 2) {
y = curY;
x = curX;
} else {
y = curRoboY;
x = curRoboX;
}
if (c == '^')
y++;
else if (c == 'v')
y--;
else if (c == '>')
x++;
else if (c == '<')
x--;
if (b || part != 2) {
curY = y;
curX = x;
} else {
curRoboY = y;
curRoboX = x;
}
for (House h : houses) {
if (h.X == x && h.Y == y) {
h.deliver();
continue step;
}
}
houses.add(new House(x, y));
}
System.out.println(houses.size());
}
}
public class House {
public int X, Y;
private int delPresents;
public House(int x, int y) {
X = x;
Y = y;
delPresents = 1;
}
public void deliver() {
delPresents ++;
}
public int getDeliveredPresents() {
return delPresents;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment