Tuesday, December 25, 2018

Advent of Code 2015 - Day 6: Probably a Fire Hazard

Part 1

Description

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,999 would turn on (or leave on) every light.
  • toggle 0,0 through 999,0 would toggle the first line of 1000 lights, turning off the ones that were on, and turning on the ones that were off.
  • turn off 499,499 through 500,500 would turn off (or leave off) the middle four lights.
After following the instructions, how many lights are lit?

Input


Solution

For this problem, at first I thought of making a grid of 1000*1000 cells, marked with coordinates of (0,0) through (999,999). But, then it will be long to make a calculation in such vast area. So, I decided to use VBA, reading the input, and constructing the grid in a two dimensional array. Here is the split input in Excel.


And, the code:

Sub Hazard3()
    Dim c, x0, y0, x1, y1, x2, y2, rows As Integer
    Dim map(0 To 999, 0 To 999) As Variant
    Dim action As String
    ActiveSheet.Range("h3").Select
    x0 = 11
    y0 = 11
Start:
    ActiveCell.Offset(1, 0).Select
    rows = ActiveCell.Row
    x1 = Cells(rows, 3).Value
    y1 = Cells(rows, 4).Value
    x2 = Cells(rows, 6).Value
    y2 = Cells(rows, 7).Value
    action = Cells(rows, 2).Value
    If IsEmpty(Cells(rows, 3)) Then
        GoTo Finish
        Else
        If action = "turnon" Then
                For c = x1 To x2
                For d = y1 To y2
                map(c, d) = 1
                Next d
                Next c
            ElseIf action = "turnoff" Then
                For c = x1 To x2
                For d = y1 To y2
                map(c, d) = 0
                Next d
                Next c
            Else
                For c = x1 To x2
                For d = y1 To y2
                map(c, d) = (map(c, d) + 1) Mod 2
                Next d
                Next c
        End If
    End If
    ActiveSheet.Cells(rows, 7).Select
    GoTo Start
Finish:
ActiveSheet.Cells(3, 10).Select
ActiveCell.Value = Application.WorksheetFunction.Sum(map)
End Sub

This code basically modify the value of corresponding array (map) according to the input value and the instruction (toggle, turn on or turn off) and finally sum up the array.

Part 2

Description

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.
What is the total brightness of all lights combined after following Santa's instructions?
For example:
  • turn on 0,0 through 0,0 would increase the total brightness by 1.
  • toggle 0,0 through 999,999 would increase the total brightness by 2000000.

Solution

For the part 2, only the instruction is changed. So, I modified the code into this:

Sub Hazard2()
    Dim c, x0, y0, x1, y1, x2, y2, rows As Integer
    Dim map(0 To 999, 0 To 999) As Variant
    Dim action As String
    ActiveSheet.Range("h3").Select
    x0 = 11
    y0 = 11
Start:
    ActiveCell.Offset(1, 0).Select
    rows = ActiveCell.Row
    x1 = Cells(rows, 3).Value
    y1 = Cells(rows, 4).Value
    x2 = Cells(rows, 6).Value
    y2 = Cells(rows, 7).Value
    action = Cells(rows, 2).Value
    If IsEmpty(Cells(rows, 3)) Then
        GoTo Finish
        Else
        If action = "turnon" Then
                For c = x1 To x2
                For d = y1 To y2
                map(c, d) = map(c, d) + 1
                Next d
                Next c
            ElseIf action = "turnoff" Then
                For c = x1 To x2
                For d = y1 To y2
                If map(c, d) > 0 Then
                map(c, d) = map(c, d) - 1
                End If
                Next d
                Next c
            Else
                For c = x1 To x2
                For d = y1 To y2
                map(c, d) = map(c, d) + 2
                Next d
                Next c
        End If
    End If
    ActiveSheet.Cells(rows, 7).Select
    GoTo Start
Finish:
ActiveSheet.Cells(5, 10).Select
ActiveCell.Value = Application.WorksheetFunction.Sum(map)
End Sub
Share:

0 komentar:


Recent Posts

Categories

Blog Archive