|
@@ -0,0 +1,34 @@
|
|
1
|
+#!/usr/bin/python3
|
|
2
|
+
|
|
3
|
+"""Solution for day 18 of Advent of Code 2016.
|
|
4
|
+
|
|
5
|
+Each line of the maze is represented as a list of booleans, with True for safe spaces and False for traps.
|
|
6
|
+
|
|
7
|
+The four separate trap rules can be simplified to just "left is different to right", which is handled in the line
|
|
8
|
+method (with some annoying fudging to handle the first and last elements whose parents are 'walls').
|
|
9
|
+
|
|
10
|
+The solution is obtained by repeatedly generating lines and summing them (Python booleans are a subclass of ints,
|
|
11
|
+with True being equal to 1 and False to 0, so a simple call to sum counts the number of safe squares). Only the most
|
|
12
|
+recent line is kept, everything else is discarded once counted.
|
|
13
|
+"""
|
|
14
|
+
|
|
15
|
+seed = [x == '.' for x in
|
|
16
|
+ '^^.^..^.....^..^..^^...^^.^....^^^.^.^^....^.^^^...^^^^.^^^^.^..^^^^.^^.^.^.^.^.^^...^^..^^^..^.^^^^']
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+def line(previous):
|
|
20
|
+ return [(i == 0 or previous[i - 1]) == (i == len(previous) - 1 or previous[i + 1]) for i in range(len(previous))]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+def calculate(num):
|
|
24
|
+ last_line = seed
|
|
25
|
+ safe_spaces = sum(last_line)
|
|
26
|
+ lines = 1
|
|
27
|
+ while lines < num:
|
|
28
|
+ last_line = line(last_line)
|
|
29
|
+ safe_spaces += sum(last_line)
|
|
30
|
+ lines += 1
|
|
31
|
+ return safe_spaces
|
|
32
|
+
|
|
33
|
+print('Part one: %s' % calculate(40))
|
|
34
|
+print('Part two: %s' % calculate(400000))
|