added day03

This commit is contained in:
Peter Hudec 2021-12-03 08:24:32 +01:00
parent 12b1058a27
commit 759dee0449
5 changed files with 2070 additions and 0 deletions

1000
03/input01.txt Normal file

File diff suppressed because it is too large Load Diff

12
03/input01_sample.txt Normal file
View File

@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010

1000
03/input02.txt Normal file

File diff suppressed because it is too large Load Diff

25
03/solve01.py Normal file
View 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
View 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))