r/adventofcode • • Dec 06 '15

SOLUTION MEGATHREAD --- Day 6 Solutions ---

--- Day 6: Probably a Fire Hazard ---

Post your solution as a comment. Structure your post like the Day Five thread.

22 Upvotes

171 comments sorted by

View all comments

1

u/97WaterPolo Dec 06 '15

Java with output being "The total is: 543903 and it took 66ms"

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class Day6 {
	private static int[][] array;
	private static int SIZE = 1000;
	public static void main(String[] args) throws IOException{
		long startTime = System.currentTimeMillis();
		File f = new File("Data"+File.separator + "Day6"+ File.separator + "lights.txt");
		List<String> values = Files.readAllLines(Paths.get(f.toURI()));	
		array = new int[SIZE][SIZE];
		for (String s : values){
			String temp = s;
			temp = temp.replace(" ","");
			String[] points = temp.split("through");
			if (s.contains("on")){
				points[0] = points[0].substring(6);
				turnOn(translate(points[0]), translate(points[1]));
			}else if (s.contains("off")){
				points[0] = points[0].substring(7);
				turnOff(translate(points[0]), translate(points[1]));
			}else if (s.contains("toggle")){
				points[0] = points[0].substring(6);
				toggle(translate(points[0]), translate(points[1]));
			}
		}
		System.out.println("The total is: " + countLights() + " and it took " + (System.currentTimeMillis()-startTime) + "ms");
	}

	private static void toggle(int[] a1, int[] a2){
		for (int x = a1[0]; x <= a2[0]; x++)
			for (int y = a1[1]; y <= a2[1];y++){
				int val = array[x][y];
				if (val == 0)
					array[x][y] = 1;
				else
					array[x][y] = 0;
			}
	}

	private static void turnOff(int[] a1, int[] a2){
		for (int x = a1[0]; x <= a2[0]; x++)
			for (int y = a1[1]; y <= a2[1];y++){
				array[x][y] = 0;
			}
	}

	private static void turnOn(int[] a1, int[] a2){
		for (int x = a1[0]; x <= a2[0]; x++)
			for (int y = a1[1]; y <= a2[1];y++){
				array[x][y] = 1;
			}
	}

	private static int countLights(){
		int count = 0;
		for (int i =0; i < SIZE; i++)
			for (int j = 0; j < SIZE; j++)
				if (array[i][j] == 1)
					count++;
		return count;
	}
	
	private static int[] translate(String value){
		int[] array = new int[2];
		array[0] = Integer.parseInt(value.split(",")[0]);
		array[1] = Integer.parseInt(value.split(",")[1]);
		return array;
	}
}