diff --git a/day3/day3.py b/day3/day3.py new file mode 100644 index 0000000..7b7d0ff --- /dev/null +++ b/day3/day3.py @@ -0,0 +1,67 @@ +def calculate_gamma(inp): + gamma_rate = [0] * len(inp[0]) + for line in inp: + for index, char in enumerate(line): + gamma_rate[index] += int(char) + gamma_rate = [0 if x < len(inp) // 2 else 1 for x in gamma_rate] + return gamma_rate + + +def part1(inp): + gamma = calculate_gamma(inp) + epsilon = [0 if x == 1 else 1 for x in gamma] + # power consumption = dec(gamma_rate) * dec(epsilon_rate) + power = int("".join(str(x) for x in gamma), 2) * int("".join(str(x) for x in epsilon), 2) + print("Part 1, power consumption : ", power) + + +def calculate_most_common(inp, pos): + sum = 0 + for line in inp: + sum += int(line[pos]) + return 0 if sum < len(inp) // 2 else 1 + + +def filter_oxygen(inp, pos, most_common): + result = [] + for line in inp: + if int(line[pos]) == most_common: + result.append(line) + return result + + +def oxygen_rating(inp): + result = inp[:] + for pos in range(len(inp[0])): + most_common = calculate_most_common(result, pos) + result = filter_oxygen(result, pos, most_common) + if len(result) == 1: + return result + + +def co2_rating(inp): + result = inp[:] + for pos in range(len(inp[0])): + least_common = 1 - calculate_most_common(result, pos) + result = filter_oxygen(result, pos, least_common) + if len(result) == 1: + return result + + +def part2(inp): + oxygen = oxygen_rating(inp) + co2 = co2_rating(inp) + res = int("".join(str(x) for x in oxygen), 2) * int("".join(str(x) for x in co2), 2) + print(f"Part 2 : {res}") + + + +def main(input_file): + with open(input_file) as f: + entries = [x.rstrip() for x in f.readlines()] + part1(entries) + part2(entries) + + +if __name__ == "__main__": + main("input.txt") diff --git a/day3/input.txt b/day3/input.txt new file mode 100644 index 0000000..6ed7c05 --- /dev/null +++ b/day3/input.txt @@ -0,0 +1,1000 @@ +110001101000 +111011011100 +100001101100 +111011011010 +001001101100 +100001111100 +110110101101 +110010110000 +111101010011 +101101010110 +000010101110 +110110010001 +011101010111 +000101101011 +010110011010 +101110110000 +111110101011 +010001111110 +010011111110 +101010111001 +110010010111 +110001100101 +111111100101 +101001111010 +111011111000 +100011101000 +011100111001 +100000100110 +001101000100 +010110110100 +011000111111 +101011111000 +011011110011 +000110011101 +001000001010 +101111100000 +011000111110 +011110000001 +101001110100 +110000001110 +010010110110 +110001110011 +001100000101 +000001001101 +111011011011 +000101100111 +010111100000 +000110100010 +001000110101 +110000011100 +101011000111 +111001011110 +001110011101 +100110011000 +000000001101 +011011101101 +110100010011 +110101110011 +111110101010 +111010000001 +111000101101 +000011111111 +000110100011 +101100111011 +100011111110 +111000010000 +010001011111 +101011101001 +110111110010 +100111101100 +101110111001 +101100010000 +101100000000 +010010011111 +101000101011 +111000000000 +001101101100 +001010110100 +101110010111 +010011011111 +001101101111 +000110000010 +101010011100 +010011101010 +001001011111 +010011100100 +011001010000 +101101000110 +000000010010 +110101011000 +000011001001 +110111110110 +010100000110 +001010101100 +001010100000 +101001100110 +011111011000 +001111111101 +001000010001 +011000011001 +100110110000 +100110001100 +110101101001 +101100010111 +100111001110 +010000100101 +111100101100 +000000011001 +110010101101 +111001110100 +011111100001 +010011011011 +100100100111 +011010011000 +100100110010 +111111100000 +111010111110 +001001111101 +101010110001 +101100000011 +011011110010 +100010000101 +110111010011 +101001001111 +000011000111 +010110110001 +100010001101 +000011111110 +100111110110 +110100001110 +000010000000 +011010010101 +000110011010 +110111011101 +110111001100 +100100010100 +100101101011 +001001001001 +101111001010 +010010010100 +010101000001 +111011101100 +011100111110 +011111110010 +001011001110 +110101001110 +000000000010 +100111010000 +000010001010 +110110001111 +001101110111 +010010101010 +011100001011 +110111001010 +001001011001 +000000000100 +110111001000 +010101110011 +110000011011 +101111000000 +001011010100 +001010000100 +111110101111 +000011000100 +100011110110 +010011000010 +000110001001 +111110010110 +100110011010 +110010111101 +001111011101 +100001101111 +110100000001 +011110000000 +011010001001 +110110011111 +100110001111 +001001011100 +010010100010 +011100001100 +011001001011 +011111100010 +101001100101 +011100100110 +101000010011 +110010101010 +000111111100 +010000000101 +111100011010 +101100011010 +011111000011 +011000111101 +111000011011 +110101111101 +001000100101 +100100110101 +000111101110 +010000111011 +001011011101 +111011110101 +100110000101 +111101011111 +011101000011 +010010110101 +000001011101 +111110000100 +010100011100 +101000100011 +101010000011 +111011001011 +010101010111 +111111000111 +001011110100 +000000111010 +110100001111 +110100001001 +011111000100 +001101111101 +001000110000 +100011110100 +111110010100 +001100111110 +010010001110 +000100110101 +111011100111 +001000000110 +100100100011 +000011001101 +000111001110 +000101111101 +011101110010 +011110111111 +000111000011 +101001000101 +101111111110 +100101110100 +101010100010 +000010111010 +001011101011 +100100000111 +011000010001 +100000100101 +001010001010 +001100100001 +001000010010 +110101110001 +100111010111 +100101101001 +100010101101 +011101100101 +011111101011 +101101011010 +101001111011 +111010011101 +010010001100 +110001000010 +101011101010 +010001010011 +101111111000 +001110101000 +111000000111 +010111011100 +101000100100 +010011010111 +011000100010 +111001111101 +101010010011 +110011011000 +100111010101 +100001101110 +011011110110 +110000010111 +001100101111 +000110111111 +111001011011 +001010100100 +011111100011 +011110001110 +011011111000 +010101111101 +100001100011 +111111011111 +100010111101 +110101010100 +101000111001 +001001000001 +110011110011 +001101001000 +001100101110 +010001111100 +101100111000 +001011000011 +110001011101 +101001011010 +110011110110 +011010111101 +001001100001 +011010010100 +010111101011 +000110010101 +110010100011 +001111001101 +100110111001 +100010000110 +010111011011 +001110011110 +011010111100 +110100100010 +001010001110 +010011111010 +101010100111 +111010100101 +010000000000 +000001010101 +111001000001 +011110000010 +001010111111 +100101000001 +001000010111 +101000011011 +011100010000 +111000010100 +010001011100 +111111010001 +011010101110 +011111110001 +000110101100 +111000011001 +001010011100 +000011011100 +110111111101 +001001010101 +011110000011 +001111111001 +110010100000 +101111010100 +100100011000 +010101000111 +111100110111 +010010111011 +001101011111 +011101011000 +111111111111 +010100110110 +111000001010 +000100000110 +000110010000 +110000011000 +001000011001 +011110000100 +101011100011 +000011100001 +110100000011 +100100011100 +011110010111 +101000111111 +001010001100 +100010000100 +011100110110 +101010011000 +100000001111 +010001111111 +101101001011 +100010100101 +011011100000 +111110100111 +000100011000 +011000100001 +110000111000 +010011100101 +000100010011 +101110101011 +011111001111 +010001001101 +111100011111 +010000100000 +001111000101 +100000010010 +000111101000 +101111010000 +110110010000 +110111110001 +111110100000 +101110001010 +011111011001 +000001111101 +101000010100 +110111101010 +001111111011 +000101010101 +110110001011 +100011001010 +101010010111 +111100011110 +010000101101 +111110000010 +000100100111 +000110001100 +011110000111 +101110100001 +110001101010 +110100010100 +111010101101 +110010001001 +000010011110 +001100100110 +011111010011 +111110000001 +110111111100 +011100000110 +000011100111 +001101110100 +100111011101 +011100010010 +011001110110 +110110010010 +011000011011 +110111101100 +101100101100 +100101111001 +101001111000 +101000101001 +011101010100 +001110000100 +011100100000 +110000001010 +010001000111 +001100000110 +000100111011 +101001011101 +011100011110 +001011001000 +111101101100 +001110011111 +100011100110 +011000100101 +111010111111 +111010001110 +111011101111 +010001110011 +000011010100 +000000101001 +010100100001 +101111111011 +100111100000 +111110100101 +000100001000 +000101011101 +110001001110 +110111001001 +110111110011 +111101000111 +010111000100 +000010101000 +000110101101 +100000001010 +010111100101 +110010000100 +000011010111 +000101001001 +100111101110 +111110011111 +011001000010 +110010001101 +001111110010 +101110111111 +100100111101 +010111100110 +000101110001 +010111000001 +001011101010 +101101100110 +110010111111 +001111101010 +110110100100 +101110100101 +011001110010 +000000100011 +110000111100 +011110001011 +001110011000 +000010011011 +011100011100 +110001001010 +100000111010 +010010000111 +010010000001 +001110100100 +111110111101 +001100001011 +110101000000 +111100010110 +100010011100 +001011110011 +100000111001 +000101000111 +100011101011 +011010001000 +111111111001 +100011011001 +101001010011 +110001111001 +001100101101 +111000111100 +101000110110 +000011110011 +000110010001 +110110010011 +011100000000 +101101110001 +100001111101 +110010010000 +100001011011 +100010010000 +000000000110 +001011001101 +000001000101 +011100100010 +000100010001 +001111001110 +100001101001 +010100011111 +000001111011 +110100111110 +101000101000 +000101100101 +011000001101 +001010101000 +100100100110 +101010111101 +010000111100 +000000010000 +101011110100 +111011111010 +000011011111 +011111000010 +001001001110 +001100010111 +011001110100 +101011000110 +001000011101 +111101000010 +011101001001 +100011101110 +110000100101 +011111101100 +101000001001 +011011001110 +001100011110 +010110100001 +011100001000 +001111011010 +110001100100 +000001100011 +101011110001 +100000010111 +010011001000 +101111101110 +110001011100 +110000001011 +010001100000 +100111000111 +110000000100 +000011010010 +000101111001 +000100101000 +111000111111 +011110100111 +111110010010 +111111000010 +011111110111 +101101000100 +110101001111 +011000000101 +011010100110 +100101101000 +011001111000 +010001110101 +011011110111 +101110110100 +100011100101 +000101100000 +100101011011 +010011000110 +000110110111 +011110010010 +101111001111 +101001110111 +100001011001 +101101001111 +011111110101 +110001111111 +010111001011 +100100001010 +000100101111 +110111110111 +001101010100 +001011111100 +100110111101 +000001001001 +100111111101 +011010101101 +100010100001 +110101111110 +010010011100 +111001111000 +101011100001 +011101111110 +110101101011 +011100101100 +110110000001 +010110110110 +101011010111 +110111011011 +010011101001 +000001101010 +000010111000 +101000101010 +111100001000 +001001001100 +010110000010 +010010101111 +101101011111 +111111010011 +111111011101 +100011010000 +001011100111 +011001011110 +110010001110 +001010010100 +000010000010 +100011100001 +011011100110 +100001001111 +111000101010 +111000110100 +110100011011 +111010010101 +010010011001 +010111101111 +001111001100 +000111010011 +010010010001 +011101001100 +011111011110 +000101101010 +001011110111 +010000100011 +111101110001 +110001110010 +011000010101 +111111000110 +100110110101 +010001000110 +010000001111 +100000000110 +100101101110 +010101111111 +111000011010 +011000000100 +010100011110 +001101111111 +111011000100 +011011101100 +011001100011 +010001000010 +011001000111 +001010000101 +111001011001 +110100111101 +100101011010 +011100010101 +100000000010 +110101101110 +110011011011 +100010111000 +010110000100 +100000011000 +011001011100 +010011001110 +001100010101 +111100000000 +001000110001 +000011010101 +110101000010 +001100101001 +100110011100 +100001101011 +111010011011 +011010110010 +010101000000 +000111011111 +111110110101 +101010110101 +111011100100 +010111111111 +101010101111 +000011110010 +100101001010 +101000001100 +000000101100 +011111010001 +110101001010 +101010101110 +000101000100 +000110101001 +010011011110 +111101101111 +110100111000 +011011000111 +010000001000 +110000000110 +010100101111 +010110001001 +000111101111 +101110110001 +101000101110 +011101100001 +010001010110 +010011111111 +011111111101 +010111001010 +010010011011 +000010010001 +101100000010 +011001101001 +011100011011 +011010011100 +111101110101 +001001001011 +011110011101 +100001010100 +010010111110 +010011111101 +110000101001 +101110000101 +110010001100 +011001110111 +010011000000 +000011100101 +101010101100 +001000111000 +110101000001 +111110001000 +110000011001 +100000111000 +010111110001 +011011000001 +101101100100 +111110111011 +101000000101 +011110110110 +011001100111 +100101001011 +001110110100 +011010100101 +011110010001 +110010101110 +111101001111 +100100000100 +100000110001 +100100110011 +110011101000 +000110101011 +011110010101 +010101010000 +111111011110 +101111111111 +001111000010 +100001111001 +011110010000 +100101001101 +111010001100 +001101010010 +100100111110 +011011100001 +110010101001 +010101101101 +101000100101 +001100101011 +111111001010 +001000001011 +011101100111 +100101000010 +111011011110 +001000000101 +001000000000 +000101010110 +001010111011 +110010000000 +010011100001 +001001011011 +000101000101 +010011000111 +100100101101 +011110010110 +100100010000 +101100100010 +110101011011 +000001111111 +101001110010 +000100101011 +110001000000 +011010111011 +001111101110 +100110111111 +011100000100 +111100010101 +100001111111 +100000110000 +101111110000 +100000110101 +001101101010 +101010011110 +011000110010 +001110110110 +101000110010 +000111001111 +011101111001 +010000110010 +101011000000 +001011001010 +001101111100 +010010111001 +010101100010 +011001100000 +011110110011 +010101011100 +011000100011 +001110101010 +100111110001 +101110110111 +110100110101 +101000110111 +100011110111 +010000010010 +001001111111 +010100111110 +101111111101 +110111101111 +011010110101 +101100111001 +110001111010 +000101110010 +110000010110 +010110100011 +111011111111 +001100011010 +011010000010 +000101011000 +101000001000 +011110110101 +000001001011 +010011110001 +110101100000 +101011000001 +101001000010 +111111100111 +011010010010 +100010001100 +111011011101 +010110001111 +001001010011 +111001001111 +101000010101 +110101101111 +011111111001 +111001101010 +100111011010 +001010010011 +110001110100 +011110001010 +110111010101 +001001100110 +001001110011 +100001110011 +001001001101 +100110100100 +010001101101 +101110000111 +001110001111 +010100111100 +010110011111 +100100000011 +100110011001 +011101111011 +011000010000 +010101101001 +100010110011 +000100000010 +111001001000 +001110100000 +110000111011 +000001100111 +000110110000 +111001110000 +100001011100 +101101001101 +111101110011 +100011011100 +011010110111 +111001110001 +101011111100 +110010110110 +011100111010 +100111110101 +010011010010 +011101110111 +101110010100 +011100100001 +001011100011 +110101010001 +101000000111 +000101100100 +100011000011 +111011001101 +101001100111 +000000000111 +010100110011 +011111001101 +111101110000 +000111111000 +110100101011 +100101110001 +001010100011 +110101100001 +100101010110 +011100111011 +100100111001 +000100110001 +001101011100 +000110100110 +010001001111 +110000000000 +001010101101 +100001100001 +110010100111 +000010011111 +101110011001 +110010010101 +010101011101 +010001110010 +011101011111 +100100001011 +001100011100 +100111101001 +110010011001 +001010000011 +101010100000 +111111111110 +011010101001 +011110100010 +110000111101 +111010100001 +100111100110 +000000011010 +011011001100 +001101111011 +011111111010 +000101110111 +101100011011 +100111110000 +111011010100 +000100001001 +010000110000 +010111110000 +000110011001 +101111001110 +001100010000 +011011011011 +001001001000 +110110111011 +001000010100 +010100010000 +010101110111 +001111010100 +001010000010 +011110011001 +100001001001 +001101000101 +010001010101 +101110001001 +011100100111 +111000001111 +101001111111 +101010001011 +111110110011 +101101101000 +011001111010 +000010011001 +100000010001 +101010011111 +000100011101 +010010100000 +000001100101 +001001011110 +100101000101 +011111110110 +111101111001 +100011101100 \ No newline at end of file