Skip to content

Commit dc6b2eb

Browse files
authored
Add files via upload
1 parent 6711898 commit dc6b2eb

File tree

3 files changed

+1072
-0
lines changed

3 files changed

+1072
-0
lines changed

3a.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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}")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy