Day 6: Probably a Fire Hazard
import java.io.File
import java.io.BufferedReader
val lines = File("puzzle.aoc").readLines()
Part 1
Because your neighbors keep defeating you in the holiday house decorating contest year after year, you’ve decided to deploy one million lights in a 1000x1000 grid.
Furthermore, because you’ve been especially nice this year, Santa has mailed you instructions on how to display the ideal lighting configuration.
Lights in your grid are numbered from 0 to 999 in each direction; the lights at each corner are at 0,0, 0,999, 999,999, and 999,0. The instructions include whether to turn on, turn off, or toggle various inclusive ranges given as coordinate pairs. Each coordinate pair represents opposite corners of a rectangle, inclusive; a coordinate pair like 0,0 through 2,2 therefore refers to 9 lights in a 3x3 square. The lights all start turned off.
To defeat your neighbors this year, all you have to do is set up your lights by doing the instructions Santa sent you in order.
For example:
turn on 0,0 through 999,999would turn on (or leave on) every light.toggle 0,0 through 999,0would toggle the first line of1000lights, turning off the ones that were on, and turning on the ones that were off.turn off 499,499 through 500,500would turn off (or leave off) the middle four lights.
1. Create Data classes
data class Point(val x : Int, val y : Int)
/* op = 0 // turn off
* op = 1 // turn on
* op = 2 // toggle */
data class Instruction(val op : Int, val start : Point, val end : Point)
2. Create matrix and functions
var matrix = Array(1000) { BooleanArray(1000) }
fun interpret(instruction : Instruction) {
for (y in instruction.start.y..instruction.end.y) {
for (x in instruction.start.x..instruction.end.x) {
matrix[y][x] = when (instruction.op) {
0 -> false
1 -> true
else -> !matrix[y][x]
}
}
}
}
fun countActiveLights() : Int {
var activeLights = 0
for (y in 0..matrix.size-1) {
for (x in 0..matrix[y].size-1) {
if (matrix[y][x]) activeLights++
}
}
return activeLights
}
Task 1 : How many lights are lit?
3. Run analysis
for (line in lines) {
val instruction = line.split(" ")
var instr : Instruction
if (instruction.size > 4) {
val start = instruction[2].split(',')
val end = instruction[4].split(',')
instr = Instruction(
when (instruction[1]) {
"off" -> 0
"on" -> 1
else -> -1
},
Point(start[0].toInt(), start[1].toInt()),
Point(end[0].toInt(), end[1].toInt())
)
} else {
val start = instruction[1].split(',')
val end = instruction[3].split(',')
instr = Instruction(
2,
Point(start[0].toInt(), start[1].toInt()),
Point(end[0].toInt(), end[1].toInt())
)
}
interpret(instr)
}
4. Show result
countActiveLights()
Part 2
You just finish implementing your winning light pattern when you realize you mistranslated Santa’s message from Ancient Nordic Elvish.
The light grid you bought actually has individual brightness controls; each light can have a brightness of zero or more. The lights all start at zero.
The phrase turn on actually means that you should increase the brightness of those lights by 1.
The phrase turn off actually means that you should decrease the brightness of those lights by 1, to a minimum of zero.
The phrase toggle actually means that you should increase the brightness of those lights by 2.
For example:
turn on 0,0 through 0,0would increase the total brightness by1.toggle 0,0 through 999,999would increase the total brightness by2000000.
5. create Matrix and functions
var brightnessMatrix = Array(1000) { IntArray(1000) }
fun interpretAnew(instruction : Instruction) {
for (y in instruction.start.y..instruction.end.y) {
for (x in instruction.start.x..instruction.end.x) {
brightnessMatrix[y][x] = when (instruction.op) {
0 -> if (brightnessMatrix[y][x] == 0) 0 else brightnessMatrix[y][x] - 1
1 -> brightnessMatrix[y][x] + 1
else -> brightnessMatrix[y][x] + 2
}
}
}
}
fun totalBrightness() : Int {
var brightness = 0
for (y in 0..brightnessMatrix.size-1) {
for (x in 0..brightnessMatrix[y].size-1) {
brightness += brightnessMatrix[y][x]
}
}
return brightness
}
Task 2: What is the total brightness of all lights combined?
6. Run Analysis
for (line in lines) {
val instruction = line.split(" ")
var instr : Instruction
if (instruction.size > 4) {
val start = instruction[2].split(',')
val end = instruction[4].split(',')
instr = Instruction(
when (instruction[1]) {
"off" -> 0
"on" -> 1
else -> -1
},
Point(start[0].toInt(), start[1].toInt()),
Point(end[0].toInt(), end[1].toInt())
)
} else {
val start = instruction[1].split(',')
val end = instruction[3].split(',')
instr = Instruction(
2,
Point(start[0].toInt(), start[1].toInt()),
Point(end[0].toInt(), end[1].toInt())
)
}
interpretAnew(instr)
}
7. Show result
totalBrightness()