day 1-5
This commit is contained in:
commit
82f8e11ae4
42 changed files with 5836 additions and 0 deletions
89
day_3/code.py
Normal file
89
day_3/code.py
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import fileinput
|
||||
|
||||
data = []
|
||||
for inp in fileinput.input():
|
||||
val = inp.strip()
|
||||
if val != '':
|
||||
data.append(val)
|
||||
|
||||
commons = []
|
||||
epsilon = []
|
||||
gamma = []
|
||||
charLen = len(data[0])
|
||||
lineLen = len(data)
|
||||
for i in range(charLen):
|
||||
count = 0
|
||||
for j in range(lineLen):
|
||||
if data[j][i] == '1':
|
||||
count += 1
|
||||
|
||||
status = 'lt'
|
||||
#print(count, lineLen-count)
|
||||
if count == (lineLen-count): status = 'eq'
|
||||
if count > (lineLen-count): status = 'gt'
|
||||
|
||||
if status == 'gt':
|
||||
most,least = '1', '0'
|
||||
else:
|
||||
most,least = '0', '1'
|
||||
|
||||
gamma.append(most)
|
||||
epsilon.append(least)
|
||||
commons.append((status, most, least))
|
||||
|
||||
g = int(''.join(gamma), 2)
|
||||
e = int(''.join(epsilon), 2)
|
||||
|
||||
|
||||
def get_distribution(nbs):
|
||||
out = []
|
||||
for _i in range(len(nbs[0])):
|
||||
count = 0
|
||||
for _j in range(len(nbs)):
|
||||
if nbs[_j][_i] == '1': count += 1
|
||||
status = 'lt'
|
||||
if count == (len(nbs)-count): status = 'eq'
|
||||
if count > (len(nbs)-count): status = 'gt'
|
||||
most,least = ('1', '0') if status == 'gt' else ('0', '1')
|
||||
out.append((status, most, least))
|
||||
|
||||
return out
|
||||
|
||||
def filter_item(mode, status, most, least, x, i, full):
|
||||
print(f'{mode=}, {status=}, {most=}, {least=}, {x=}, {i=}, {full=}')
|
||||
if mode:
|
||||
if status == 'eq':
|
||||
return x == '1' # oxygen
|
||||
if x == most: return True
|
||||
|
||||
if not mode: # co2
|
||||
if status == 'eq':
|
||||
return x == '0'
|
||||
if x == least: return True
|
||||
|
||||
return False
|
||||
|
||||
def rating(inp, mode = True):
|
||||
nb = inp.copy()
|
||||
i = 0
|
||||
while len(nb) > 1:
|
||||
distri = get_distribution(nb)
|
||||
print(f'= {len(nb)} -> {nb} {distri=}')
|
||||
status, most, least = distri[i]
|
||||
nb = list(filter(lambda x: filter_item(mode, status, most, least, x[i], i, x), nb))
|
||||
i += 1
|
||||
print(f'after filter: {nb}')
|
||||
return int(nb[0], 2)
|
||||
|
||||
print(g, e)
|
||||
print(g*e)
|
||||
|
||||
print(commons)
|
||||
o = rating(data, True)
|
||||
c = rating(data, False)
|
||||
print('==')
|
||||
print(o) # oxygen
|
||||
print(c) # CO2
|
||||
print(o*c)
|
||||
|
||||
|
||||
12
day_3/input1.txt
Normal file
12
day_3/input1.txt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
||||
11001
|
||||
00010
|
||||
01010
|
||||
1000
day_3/input2.txt
Normal file
1000
day_3/input2.txt
Normal file
File diff suppressed because it is too large
Load diff
BIN
day_3/main
Normal file
BIN
day_3/main
Normal file
Binary file not shown.
66
day_3/main.c
Normal file
66
day_3/main.c
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int tobin(char *inp)
|
||||
{
|
||||
int len = strlen(inp);
|
||||
printf("%d", len);
|
||||
int out = 0;
|
||||
int i = 0;
|
||||
while (inp[i] != '\0') {
|
||||
if (inp[i] == '1') {
|
||||
out = out | (1 << len-i-1);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
char input;
|
||||
char lines[1024][32];
|
||||
int lineCount = 0;
|
||||
int charCount = 0;
|
||||
while (scanf("%c", &input) == 1) {
|
||||
if (input == '\n') {
|
||||
charCount = 0;
|
||||
lineCount++;
|
||||
} else {
|
||||
lines[lineCount][charCount] = input;
|
||||
charCount++;
|
||||
}
|
||||
}
|
||||
|
||||
charCount = strlen(lines[0]);
|
||||
|
||||
char gamma[32];
|
||||
char epsilon[32];
|
||||
printf("Recorded %d %d \n", lineCount, charCount);
|
||||
for (int i = 0; i < charCount; i++) {
|
||||
int oneCount = 0;
|
||||
for (int j = 0; j < lineCount; j++) {
|
||||
printf("%c", lines[j][i]);
|
||||
if (lines[j][i] == '1')
|
||||
oneCount++;
|
||||
}
|
||||
printf("\n count: %d \n", oneCount);
|
||||
if (oneCount > (lineCount-oneCount)) {
|
||||
epsilon[i] = '0';
|
||||
gamma[i] = '1';
|
||||
} else {
|
||||
epsilon[i] = '1';
|
||||
gamma[i] = '0';
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
gamma[i+1] = '\0';
|
||||
epsilon[i+1] = '\0';
|
||||
printf("gamma: %s epsilon: %s \n", gamma, epsilon);
|
||||
|
||||
int e = tobin(epsilon);
|
||||
int g = tobin(gamma);
|
||||
|
||||
printf("%d, %d \n", g, e);
|
||||
printf("ans: %d \n", e*g);
|
||||
}
|
||||
|
||||
8
day_3/tobin.py
Normal file
8
day_3/tobin.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
inp = '01011'
|
||||
out = 0
|
||||
for i in range(0, len(inp)):
|
||||
print(i, len(inp)-i-1)
|
||||
if inp[i] == '1':
|
||||
out = out | (1 << (len(inp)-i-1))
|
||||
print(out)
|
||||
Loading…
Add table
Add a link
Reference in a new issue