added day03
This commit is contained in:
parent
12b1058a27
commit
759dee0449
1000
03/input01.txt
Normal file
1000
03/input01.txt
Normal file
File diff suppressed because it is too large
Load Diff
12
03/input01_sample.txt
Normal file
12
03/input01_sample.txt
Normal file
@ -0,0 +1,12 @@
|
||||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
||||
11001
|
||||
00010
|
||||
01010
|
1000
03/input02.txt
Normal file
1000
03/input02.txt
Normal file
File diff suppressed because it is too large
Load Diff
25
03/solve01.py
Normal file
25
03/solve01.py
Normal file
@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
count1=[0,0,0,0,0,0,0,0,0,0,0,0]
|
||||
count=0
|
||||
code_len=12
|
||||
|
||||
with open("input01.txt", "r") as f:
|
||||
for line in f:
|
||||
count += 1
|
||||
for pos in range(code_len):
|
||||
if line[pos] == '1':
|
||||
count1[pos] += 1
|
||||
|
||||
gama=''
|
||||
epsilon=''
|
||||
|
||||
for pos in range(code_len):
|
||||
if (count1[pos] > count/2):
|
||||
gama=gama+'1'
|
||||
epsilon=epsilon+'0'
|
||||
else:
|
||||
gama=gama+'0'
|
||||
epsilon=epsilon+'1'
|
||||
|
||||
print(int(gama,2) * int(epsilon,2))
|
33
03/solve02.py
Normal file
33
03/solve02.py
Normal file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import copy
|
||||
|
||||
code_len=12
|
||||
input=[]
|
||||
|
||||
with open("input01.txt", "r") as f:
|
||||
for line in f:
|
||||
input.append(line)
|
||||
|
||||
input_oxygen = copy.copy(input)
|
||||
input_co2 = copy.copy(input)
|
||||
|
||||
# oxygen
|
||||
for pos in range(code_len):
|
||||
if len(input_oxygen) == 1:
|
||||
break
|
||||
tmp = [i for i in input_oxygen if i[pos] == '1']
|
||||
filter = '1' if (len(tmp) >= len(input_oxygen)/2) else '0'
|
||||
input_oxygen = [i for i in input_oxygen if i[pos] == filter]
|
||||
|
||||
|
||||
# co2
|
||||
for pos in range(code_len):
|
||||
if len(input_co2) == 1:
|
||||
break
|
||||
tmp = [i for i in input_co2 if i[pos] == '1']
|
||||
filter = '1' if (len(tmp) < len(input_co2)/2) else '0'
|
||||
input_co2 = [i for i in input_co2 if i[pos] == filter]
|
||||
|
||||
|
||||
print(int(input_oxygen[0], 2) * int(input_co2[0],2))
|
Loading…
x
Reference in New Issue
Block a user