Last active
December 24, 2020 16:47
-
-
Save PulseBeat02/051f8239714dfe5d3e778fea951d66a6 to your computer and use it in GitHub Desktop.
LobbyLayout.java
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 com.github.pulsebeat02; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class LobbyLayout { | |
public static void main(String[] args) throws IOException { | |
BufferedReader br = new BufferedReader(new FileReader("lobbylayout.txt")); | |
List<List<Instruction>> instructions = new ArrayList<>(); | |
String line = br.readLine(); | |
while (line != null) { | |
List<Instruction> inner = new ArrayList<>(); | |
for (int i = 0; i < line.length(); i++) { | |
if (line.charAt(i) == 's') { | |
if (line.charAt(i + 1) == 'e') { | |
inner.add(Instruction.SE); | |
i++; | |
} else if (line.charAt(i + 1) == 'w') { | |
inner.add(Instruction.SW); | |
i++; | |
} | |
} else if (line.charAt(i) == 'n') { | |
if (line.charAt(i + 1) == 'e') { | |
inner.add(Instruction.NE); | |
i++; | |
} else if (line.charAt(i + 1) == 'w') { | |
inner.add(Instruction.NW); | |
i++; | |
} | |
} else if (line.charAt(i) == 'e') { | |
inner.add(Instruction.E); | |
} else if (line.charAt(i) == 'w') { | |
inner.add(Instruction.W); | |
} | |
} | |
instructions.add(inner); | |
line = br.readLine(); | |
} | |
System.out.println("Part One: " + partOne(instructions)); | |
} | |
private static int partOne(List<List<Instruction>> instructions) { | |
// See: https://stackoverflow.com/questions/44359668/how-to-store-coordinates-to-act-like-a-hexagon-esque-structure | |
Map<String, HexagonalTile> tiles = new HashMap<>(); | |
tiles.put("0 0", new HexagonalTile(0, 0)); | |
for (List<Instruction> instructionsList : instructions) { | |
int currentX = 0; | |
int currentY = 0; | |
for (int i = 0; i < instructionsList.size(); i++) { | |
switch (instructionsList.get(i)) { | |
case E: | |
currentY++; | |
break; | |
case SE: | |
currentX--; | |
currentY++; | |
break; | |
case SW: | |
currentX--; | |
break; | |
case W: | |
currentY--; | |
break; | |
case NW: | |
currentX++; | |
currentY--; | |
break; | |
case NE: | |
currentX++; | |
break; | |
} | |
String key = currentX + " " + currentY; | |
if (!tiles.containsKey(key)) { | |
HexagonalTile tile = new HexagonalTile(currentX, currentY); | |
if (i == instructionsList.size() - 1) { | |
tile.toggled ^= true; | |
} | |
tiles.put(key, tile); | |
} | |
} | |
} | |
int count = 0; | |
for (HexagonalTile tile : tiles.values()) { | |
count += !tile.toggled ? 1 : 0; | |
} | |
return count; | |
} | |
private static class HexagonalTile { | |
private boolean toggled; | |
private final int x; | |
private final int y; | |
public HexagonalTile(int x, int y) { | |
this.toggled = true; | |
this.x = x; | |
this.y = y; | |
} | |
} | |
private enum Instruction { | |
E, SE, SW, W, NW, NE | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment