## DEV Community π©βπ»π¨βπ» is a community of 967,911 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Jules

Posted on • Updated on

# Advent of Code 2015 - Day 6

This is another puzzle that makes me wish I had learned more about popular libraries, NumPy in this case. Python doesn't do multi-dimensional arrays out of the box, so if you need to populate a 1000 x 1000 grid, as in this case, you have a few options:

1. learn NumPy
2. Use nested lists
3. Use some other workaround, as I ended up doing with this puzzle

Here's Part 1:

As we probably don't need to keep track of all million(!) lights, I decided to use a dictionary, keyed on the coordinates of each light I switch on, as a tuple.

I decided I could get rid of `'turn '` in `'turn on'` and `'turn off'`, so we were left with three instructions, 'on', 'off', and 'toggle'. Here's how I parsed the instructions:

``````#Parse an instruction from the input file
def parse_instruction(s):
words = s.replace('turn ', '').replace(',', ' ').split()
instruction, x1, y1, _, x2, y2 = words
return instruction, int(x1), int(x2), int(y1), int(y2)
``````

I like Python's ability to discard unused variables by assigning the to underscore, and obviously its ability to unpack lists straight into individual variables.

Here's the body of the code: lights that are off either aren't in `lights{}` yet, or are set to '0', and of course, lights that are on are '1'. By the end of the data, we can simply sum up the values of items in the dictionary to find out how many lights are left on:

``````lights = {}
for line in data:
instruction, x1, x2, y1, y2 = parse_instruction(line)
for x in range(x1, x2 + 1):
for y in range(y1, y2+1):
key = (x, y)
if instruction == 'off':
lights[key] = 0
elif instruction == 'on':
lights[key] = 1
else: #toggle
if key in lights:
if lights[key] == 0:
lights[key] = 1
else:
lights[key] = 0
else:
lights[key] = 1
print(sum(lights.values()))
``````

It's not the fastest code I've ever seen. but it does work, and several people in the megathread were reporting completion times in seconds.

Part 2, as ever, tweaks the scenario a little:

All we're changing here is what happens with `off` and `toggle`. The main thing with `off` is to make sure we don't dip into negative numbers. And `toggle` is simply a double strength `on`!

``````if instruction == 'off':
if key in lights:
if lights[key] > 0:
lights[key] -= 1
else:
if instruction == 'on':
incr = 1
else: #toggle
incr = 2
if key in lights:
lights[key] += incr
else:
lights[key] = incr
``````

I didn't see any golfing today (not in Python anyway), and my code was far from the longest. Not great, but it gets us to Day 7, and I'll add Numpy to my list of Python topics I need to learn. In other languages though, there were some very short solutions; this one's in K, which I didn't even know was a language!

``````e:{"SJJJJ"\$'@[;0 2 4 8 10]\$["turn"~*x;2;0]_x}'![-4]'0:`:/Users/geocar/e.txt
a:1000 1000#0;{p:x[1 2]+!:'1 1+x[3 4]-x[1 2];.[`a;p;o@*x]}'e;+//a
o:`off`on`toggle!(0 0;1 1;1 0)
``````

And no, I have no idea what it means!