Skip to content

Instantly share code, notes, and snippets.

@roccodev
Last active December 13, 2017 13:09
Show Gist options
  • Select an option

  • Save roccodev/2de50db26022e6b9077e61365e6cf171 to your computer and use it in GitHub Desktop.

Select an option

Save roccodev/2de50db26022e6b9077e61365e6cf171 to your computer and use it in GitHub Desktop.
AdventOfCode2017/Day13
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Main {
private static Firewall[] wall;
public static void main(String[] args) throws IOException {
wall = new Firewall[100];
Files.readAllLines(Paths.get(new File("res/day13").getPath())).forEach(s -> {
String[] data = s.split(": ");
wall[Integer.parseInt(data[0])] = new Firewall(Integer.parseInt(data[1]));
});
int severity = 0;
for(int i = 0; i < wall.length; i++) {
if(wall[i] != null && !wall[i].open(i)) severity += i * wall[i].getDepth();
}
System.out.println(severity);
}
}
class Firewall {
private int depth;
public int getDepth() {
return depth;
}
public Firewall(int depth) {
this.depth = depth;
}
public boolean open(int pico) {
return !(pico % ((depth - 1) * 2) == 0);
}
}
public class Main {
private static Firewall[] wall;
public static void main(String[] args) throws IOException {
wall = new Firewall[100];
Files.readAllLines(Paths.get(new File("res/day13").getPath())).forEach(s -> {
String[] data = s.split(": ");
wall[Integer.parseInt(data[0])] = new Firewall(Integer.parseInt(data[1]));
});
int picos = 0;
while(!safe(picos)) picos++;
System.out.println(picos);
}
public static boolean safe(int time) {
for(int i = 0; i < 100; i++) {
int ind = i + time;
if(wall[i] != null && !wall[i].open(ind)) {
return false;
}
}
return true;
}
}
class Firewall {
private int depth;
public int getDepth() {
return depth;
}
public Firewall(int depth) {
this.depth = depth;
}
public boolean open(int pico) {
return !(pico % ((depth - 1) * 2) == 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment