Browse Source

f-strings

And Python3 fixes....
master
Chris Smith 6 years ago
parent
commit
1f3dd13e0c
2 changed files with 6 additions and 6 deletions
  1. 3
    3
      01.py
  2. 3
    3
      02.py

+ 3
- 3
01.py View File

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

+ 3
- 3
02.py View File

@@ -1,5 +1,5 @@
1 1
 import itertools
2 2
 with open('data/02.txt', 'r') as file:
3
-    sheet = map(lambda row: map(int, row.split('\t')), file.readlines())
4
-    print('Part one: %s' % sum(max(row) - min(row) for row in sheet))
5
-    print('Part two: %s' % sum(next(a//b for a,b in itertools.permutations(row, 2) if a % b == 0) for row in sheet))
3
+    sheet = list(map(lambda row: list(map(int, row.split('\t'))), file.readlines()))
4
+    print(f'Part one: {sum(max(row) - min(row) for row in sheet)}')
5
+    print(f'Part two: {sum(next(a//b for a,b in itertools.permutations(row, 2) if a % b == 0) for row in sheet)}')

Loading…
Cancel
Save