Browse Source

Add numpy version

master
Chris Smith 6 years ago
parent
commit
0baf2ef31e
1 changed files with 7 additions and 0 deletions
  1. 7
    0
      01.py

+ 7
- 0
01.py View File

@@ -3,3 +3,10 @@ with open('data/01.txt', 'r') as file:
3 3
     length = len(captcha) // 2
4 4
     print('Part one: %s' % sum(int(captcha[i]) for i in range(length) if captcha[i] == captcha[i+1]))
5 5
     print('Part two: %s' % sum(int(captcha[i]) for i in range(length) if captcha[i] == captcha[i + length // 2]))
6
+
7
+# Alternatively...
8
+import numpy as np
9
+with open('data/01.txt', 'r') as file:
10
+    captcha = file.readline().strip()
11
+    print('Part one: %s' % sum(int(i) for i, j in zip(captcha, np.roll(list(captcha), 1).tolist()) if i == j))
12
+    print('Part two: %s' % sum(int(i) for i, j in zip(captcha, np.roll(list(captcha), len(captcha)//2).tolist()) if i == j))

Loading…
Cancel
Save