File tree Expand file tree Collapse file tree 3 files changed +1072
-0
lines changed Expand file tree Collapse file tree 3 files changed +1072
-0
lines changed Original file line number Diff line number Diff line change
1
+ #Advent of code 2021
2
+ # 12/03/21 day 3a
3
+ # Joe McFarland
4
+ # import sys
5
+ # import re
6
+
7
+ filename = "data3.txt"
8
+
9
+ file = open(filename)
10
+ filestr = file.read()
11
+ a_list = filestr.split("\n")
12
+ maxrows = len(a_list)
13
+ #print(a_list)
14
+
15
+ def read_bit(x, pos):
16
+ mask = 1 << pos
17
+ return (x & mask) >> pos
18
+
19
+ def arraytostr(arr):
20
+ mystr = ""
21
+ for index,col in enumerate(arr):
22
+ if arr[index] == 0:
23
+ mystr += "0"
24
+ elif arr[index] == 1:
25
+ mystr += "1"
26
+ else:
27
+ print(f"ERROR {arr}")
28
+ exit()
29
+ return mystr
30
+
31
+ maxcols = len(a_list[0])
32
+ gamma = [None]*maxcols
33
+ epsilon = [None]*maxcols
34
+ for col in range(maxcols):
35
+ zero_bits = one_bits = 0
36
+ for row in a_list:
37
+ if row[col] == "0":
38
+ zero_bits += 1
39
+ elif row[col] == "1":
40
+ one_bits += 1
41
+ else:
42
+ print(f"ERROR {row}")
43
+ exit()
44
+ if zero_bits > one_bits:
45
+ gbit = 0
46
+ ebit = 1
47
+ else:
48
+ gbit = 1
49
+ ebit = 0
50
+ gamma[col] = gbit
51
+ epsilon[col] = ebit
52
+
53
+ print(f"gamma={gamma}, epsilon={epsilon}")
54
+ g1 = arraytostr(gamma)
55
+ e1 = arraytostr(epsilon)
56
+ gamma_dec = int(g1,2)
57
+ epsilon_dec = int(e1,2)
58
+ print(f"gamma_str={g1}, epsilon_str={e1}")
59
+ print(f"gamma_dec={gamma_dec}, epsilon_dec={epsilon_dec}")
60
+ print(f"mul = {gamma_dec*epsilon_dec}")
You can’t perform that action at this time.
0 commit comments