-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05.py
More file actions
98 lines (78 loc) · 2.33 KB
/
05.py
File metadata and controls
98 lines (78 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import re
f = open('05.input', 'r')
data = [x.strip() for x in f.readlines()]
def calc_id(seat):
transformation= { 'F': '0', 'B': '1', 'L': '0', 'R': '1'}
binary = "".join([transformation[x] for x in seat])
return int(binary,2)
def part1binary(data):
ids = [calc_id(x) for x in data]
return max(ids)
def part2binary(data):
ids = [calc_id(x) for x in data]
for i in range(max(ids)):
low = i - 1
high = i + 1
if low in ids and high in ids and i not in ids:
return i
print "Part 1 binary solution: %d " % part1binary(data)
print "Part 2 binary solution: %d " % part2binary(data)
def part1(data):
ids = []
for line in data:
current = 0
row = 64
for letter in line[:7]:
if letter == 'B':
current += row
row = row >> 1 # Shift right 1 bit
current = current << 3 # Shift left 3 bits
row = 4
for c in line[7:]:
if c == 'R':
current += row
row = row >> 1 # Shift right 1 bit
ids.append(current)
return max(ids)
def part2(data):
id_list = []
max_id = 0
for line in data:
current = 0
row = 64
for c in line[:7]:
if c == 'B':
current += row
row = row >> 1 # Shift right 1 bit
current = current << 3 # Shift left 3 bits
row = 4
for c in line[7:]:
if c == 'R':
current += row
row = row >> 1 # Shift right 1 bit
id_list.append(current)
id_list = sorted(id_list)
for i in range(940):
low = i - 1
high = i + 1
if low in id_list and high in id_list and i not in id_list:
return i
print "Part 1 solution: %d " % part1(data)
print "Part 2 solution: %d " % part2(data)
def part1sorting(data):
normalized = []
for line in data:
new_line = line.replace('R','B').replace('L','F')
normalized.append(new_line)
d = sorted(normalized)
return d[0]
print "Part 1 sorted solution: %s " % part1sorting(data)
def part1str(data):
d = sorted(data)
highest_row_value = d[0][:7]
i = 1
while d[i][:7] == highest_row_value:
i+=1
highest_value = d[i-1]
return calc_id(highest_value)
print "Part 1 string solution: %d " % part1str(data)