From 1531e43ffc5aa6cd08e88d46d1313f5231f4e9ff Mon Sep 17 00:00:00 2001 From: Noettore Date: Fri, 3 Dec 2021 14:24:04 +0100 Subject: [PATCH] AoC 2021: day3 solutions --- 2021/inputs/day_03 | 1000 ++++++++++++++++++++++++++++++++++++++ 2021/solutions/day_03.py | 127 +++++ 2 files changed, 1127 insertions(+) create mode 100644 2021/inputs/day_03 create mode 100644 2021/solutions/day_03.py diff --git a/2021/inputs/day_03 b/2021/inputs/day_03 new file mode 100644 index 0000000..729ba9c --- /dev/null +++ b/2021/inputs/day_03 @@ -0,0 +1,1000 @@ +010001110001 +110100000001 +111001001011 +111100001000 +000111101001 +101111001001 +111111000110 +000101110010 +001101001010 +000110001101 +011111111110 +101100010001 +110101100001 +000000000110 +010011000011 +010101110010 +011001001001 +010011010111 +001101010110 +100000101000 +011100110010 +001011110001 +101110110011 +100011011101 +000101111011 +000010011000 +110101101111 +011010101100 +111001011010 +011100110011 +000101000111 +111000110101 +110111010000 +111110010001 +010100100110 +010001011010 +101100100001 +001000101010 +011111010010 +101011100110 +100111010110 +101000110100 +011010110001 +000010001000 +100001110111 +101001011100 +100100001011 +111010101110 +001101101001 +000111111100 +011011111000 +111111111011 +101100011010 +011101001001 +111001001000 +010101010001 +100000011011 +100110100011 +010101110111 +111110110000 +000001011111 +111110100111 +000100101000 +000100100011 +000011110001 +000001101101 +011000001000 +110111011010 +010000011010 +110101111011 +110110000001 +111101000100 +101010100111 +101110110001 +000111110101 +001010000100 +010100000101 +111110000010 +110011001011 +010011000101 +010001101110 +010001101000 +110111000100 +111000010010 +010000000101 +111110001100 +100110000110 +100100000011 +011101101011 +011001100010 +110011111011 +011001111000 +110001001110 +011111001000 +100010001011 +111001001101 +100000110000 +100000001000 +011001011010 +111110100011 +100111000100 +011000111010 +011101001100 +000110011001 +101101111011 +001101001011 +100101111100 +111001001110 +010100011110 +100101011010 +110101110000 +111101110110 +001010101100 +011010000011 +110001010011 +101000100011 +110010111110 +100000010100 +101011100000 +111110111000 +000000100011 +100010000100 +011111001100 +010100111011 +101110001100 +101101111100 +011000101110 +001010010010 +011001011000 +010011100001 +011111111101 +000001111011 +011010110100 +011001100000 +111101000110 +010110101001 +011100001111 +000000001110 +110010110111 +100110011101 +101010000000 +010100011111 +110111001110 +101100011000 +101101101111 +110110111001 +101111111010 +000011010101 +000000001000 +100011000110 +000000110110 +100011000001 +110011111110 +110001000101 +110110100100 +010001101010 +110100000011 +110100001010 +000010110111 +100011001010 +001111111011 +000110111010 +011110001100 +011110110000 +100100110000 +100011111110 +100000000011 +100101110010 +101010110110 +110000011001 +101110111101 +000110001000 +001110111111 +110000010001 +100101111111 +000001001001 +111100101011 +111110101110 +000110000111 +000000000001 +110100101111 +011010100110 +000010110001 +011000100111 +110011010011 +001101101010 +000011111001 +010110001111 +110000011000 +111010100000 +011101110111 +010001111001 +100000111111 +101110100111 +000110001011 +011110001110 +100000000000 +011010100000 +100111100100 +010011000001 +100100011100 +010010101100 +100010101001 +000101001110 +001101011010 +011100100111 +001010100100 +110010100000 +100111110001 +001111001000 +111000101011 +011011101001 +110001011001 +111011110111 +110010000001 +001011010010 +001011011100 +101000110001 +011000010010 +111011000110 +001011110101 +000111001001 +110110100110 +011101111111 +111001101101 +000100011110 +100011101010 +011111111011 +011110110001 +001010111111 +100101001011 +001101110110 +100100000001 +111000111001 +000101100100 +110011011000 +101010011011 +101100110111 +111011011100 +000011111110 +101001101010 +011000111100 +001101010000 +110000000011 +100110011000 +001000011100 +010110100101 +000111101100 +110010111101 +010010101010 +001001001011 +001010011011 +111010101011 +111111001110 +011011110001 +011101010101 +011111110010 +000111111110 +000110110011 +111100001101 +000000100101 +000010111111 +111010100101 +100101111000 +000100101100 +000111100010 +001010010000 +100100010001 +100011000101 +001000001010 +001000000011 +011100011010 +101110011111 +000011101101 +111011010110 +110100111011 +101110110110 +011110001011 +111101001111 +011100101110 +101110101000 +000101000001 +100010110110 +101101001101 +110000001010 +000111001111 +000000010001 +001110100101 +100100001001 +100011111000 +110011000101 +110111111000 +101100101111 +000000110100 +101111111011 +111001010101 +110110010111 +011100011001 +111100110010 +011011101000 +110110010010 +011000001010 +110101000110 +100000100101 +010010101101 +110100001011 +110000010011 +100011001101 +001100011000 +100010110111 +110100110000 +011011001000 +110110100000 +010000101001 +111100011010 +000000010100 +010110000000 +110010000100 +100001010110 +000010100110 +110011110000 +000000111101 +010011010101 +111101100011 +001001101010 +111110010110 +101100001010 +000001000110 +110111001000 +000100111000 +001110111000 +010010110101 +010001001100 +100101100001 +111010101010 +111101000000 +101010101100 +001000001100 +110001001010 +101100111100 +110110100001 +011110000001 +001011011000 +101101101100 +100110000101 +010101110000 +110101001110 +100011011001 +011100001000 +000110111001 +010101111101 +101101100010 +011101101100 +001011110111 +101001001100 +101001110100 +000010011011 +110001101111 +110110001010 +100101101010 +110110011001 +001011110000 +000111100111 +110111100001 +110010100100 +110010001001 +011010001110 +010100110101 +011011111001 +111111101101 +111111001000 +110001000100 +001101010100 +010010001111 +000000001111 +111000101010 +101101101010 +011010010100 +011011100100 +111001110101 +101000011101 +111100001100 +011001010011 +111001100111 +000100000100 +100011000111 +010100001111 +110010111100 +100100001111 +110101110110 +100111010101 +001100110111 +111010110001 +011000011100 +101100100100 +011100010111 +000001101110 +011101000111 +011100101011 +001111011010 +101100100011 +001000101110 +001100011011 +010010011111 +111100101001 +110111111001 +110111010110 +111000110010 +000010101100 +010101101101 +100110111110 +010110011001 +001100010111 +010101111001 +011011001100 +111100110000 +011110000011 +101010111110 +011000100010 +000001110100 +011111100010 +111011110011 +011110101000 +000110011010 +111001101010 +011000101000 +110000100000 +100101111001 +100001101100 +101100100111 +100010000001 +010100010001 +001101011001 +110101101000 +101110001011 +101101000000 +010000100110 +001101000010 +100011001001 +000010100001 +101000011111 +110000101001 +110101000111 +101101000011 +111111110100 +111000100010 +100000111011 +000100010010 +100010100110 +100110110110 +011101011011 +101101000010 +000100111110 +111101111110 +000100001011 +011100101001 +101010100001 +110110001011 +000001010001 +001000100110 +101100000111 +100010111111 +100000010101 +111100010100 +010001100010 +110001001001 +000100011111 +010110111101 +111111110111 +100011011110 +001011011110 +010110001011 +010100001010 +001101101011 +110110111110 +110101010010 +111010011001 +000001000010 +100100110110 +100110100010 +001101110111 +001110101010 +001110101011 +011101100010 +101101001010 +110100011101 +110010101100 +010010011101 +000101011111 +010111100011 +001111101011 +100000110001 +110101011110 +000000010111 +000010101101 +000111010100 +001100000001 +111011011101 +110001110110 +111010101111 +101101011100 +010000101010 +011111000010 +000100100110 +100011011100 +010110011101 +011001001011 +001001010111 +101100101011 +101000110111 +010000011111 +101011111100 +100100100001 +111011110000 +001100110011 +110011100000 +111101100101 +011100010010 +010001100001 +110001100011 +000000001100 +010101110100 +110011010001 +000110001100 +101101000110 +000010010000 +000010100111 +001111111100 +010110010000 +000110000000 +011011110101 +000101000110 +001101011000 +100011100010 +101100010000 +001011001100 +001110010110 +000100000110 +100110111000 +110101110001 +101000001000 +100101000011 +101010101010 +101111001101 +110001100110 +011110101110 +110011100100 +011101101001 +010001111101 +110101000101 +111100001010 +101001001011 +101011101111 +110011101110 +101010011010 +001010011101 +111111111001 +011000010011 +111010001101 +001010110101 +011001000000 +010010110010 +100111001001 +010010000111 +100100110001 +000110010111 +111000000001 +100111111111 +100100011111 +011011101011 +111100010011 +001010010101 +100001110110 +000101010011 +110110011110 +110010110000 +110101001001 +110111101001 +110001101011 +101110111011 +101100010011 +111000101101 +100101101100 +111000101000 +101011011110 +011000100000 +111011001100 +010110110111 +001101001000 +011011000110 +100100101100 +011011101111 +100000010000 +110110101100 +111101101101 +101111011000 +100110011010 +110101010110 +110110010110 +000101000010 +010100100000 +100110011111 +100011100000 +111000000011 +011111010001 +111011110101 +110100001101 +000001001101 +010111010100 +110110011000 +111101101011 +101011110010 +111010001000 +001111110111 +110101000001 +101101101001 +110100001001 +110100111111 +111011000001 +001000110000 +111001100110 +000100111100 +010101010000 +000000000100 +001111110010 +011010001010 +000111011100 +001000000111 +110101010100 +010110010111 +100101110011 +000101000100 +100101011101 +010100100001 +011001100111 +010101001111 +110011100010 +010100001110 +000010101000 +001011101000 +001000011010 +001100101011 +011110111000 +110100000100 +101011001010 +111011010101 +010011101011 +001111111001 +000101010111 +011100100010 +111101011101 +011010111001 +101110101110 +101011111111 +011100011101 +011001011110 +000100011010 +010100101110 +010000001011 +100001011110 +000011101010 +010101100000 +001000010010 +001100011111 +100111110101 +011101100100 +101101110001 +000110010001 +001110000000 +000000011010 +101101010100 +111101110011 +010011001000 +000011000001 +110110000110 +011011101100 +010011111011 +001101010011 +111000001100 +111100010111 +111000100100 +000011111100 +000011111000 +000000101110 +001001000010 +101011011000 +010110111111 +100001000010 +111001010010 +010110000110 +010001111111 +110101001111 +100001100100 +100100011011 +100100100010 +110110001101 +101001011000 +101100100101 +111001000001 +100100000000 +101001011110 +100110111010 +111101011010 +010110011111 +000001010111 +101000011010 +000111011011 +101000001001 +010010000010 +111110100100 +000100110001 +001101111011 +000110110001 +101111111100 +111110110101 +000010011101 +100001100110 +001011000110 +111100000001 +100111101000 +001001101011 +011100111111 +110001010100 +110000011110 +100101111101 +101000111000 +010110001010 +000100100101 +001011011101 +100110100100 +000011011110 +011101111000 +001110101000 +011010101001 +010000100100 +010000100101 +111110011100 +110010000110 +000100111101 +100011101100 +111101111010 +110111111101 +010100000011 +111110010000 +101001011101 +000000000010 +100100001010 +010100011101 +011111110001 +110110101011 +001100101100 +101101101000 +100001000000 +011100010011 +001011111111 +010001110101 +000100000101 +000111010010 +000011101111 +010001100101 +000110010100 +011100100001 +111110110100 +011100010100 +011111100100 +010001001110 +011000011001 +010101100010 +100110000100 +011111001101 +001101001111 +100111000000 +001011110010 +110000101011 +010001100110 +111000011101 +100010110010 +010110010110 +000010110110 +100100101001 +011010010110 +111010101100 +111001110011 +010010000110 +010111111000 +111001010011 +001100100011 +111100111010 +111011111110 +000101110001 +000101111010 +101111100101 +000100001010 +011111010101 +100100011110 +011010010010 +101111100111 +110100100110 +001001010100 +111100000010 +000010011100 +010111011100 +111101001001 +111111110110 +000010111001 +110011111111 +001000101100 +111111010011 +101010110010 +110111000000 +011010100100 +100111010001 +100100110011 +001011111001 +111100100011 +111011011111 +110000000110 +100101011111 +100001011111 +010001100111 +000001110010 +001001110010 +010000110111 +110010011011 +101110111111 +100011110110 +100110010110 +001110101111 +111110010011 +111000011100 +101101011001 +100000101111 +011001000001 +110001000110 +101011101101 +011000110110 +001110001110 +001001111101 +111111111111 +101011101100 +000001011101 +111000010000 +001110010011 +101101000111 +001101011101 +001010110011 +011101110011 +001010101000 +000100001101 +010110000011 +000001010101 +101011110001 +111010010101 +001110100000 +000011100001 +110001110111 +011011110111 +110100101011 +110111011110 +001111011001 +001111000111 +111100010101 +110000011010 +111000101111 +011100001100 +101000000001 +000100110011 +101001111101 +100010101110 +010001010100 +110111100010 +001100100100 +010000011100 +111010111111 +101111101101 +101010011000 +001001011001 +100100001110 +100000110110 +100110001010 +010010011001 +010101101011 +010000001111 +101111101111 +101000111011 +111100111111 +101110001001 +001011000011 +110000100101 +011100100110 +000010100101 +011001110110 +111101000010 +010011111110 +011100111100 +101111000001 +010110111011 +011010110010 +001001100110 +011101100110 +001110000110 +111011111111 +000011010011 +000110001110 +101001001000 +110111010001 +000111111000 +111111101011 +000001110110 +101011010100 +001010110100 +011000101011 +001111010001 +110011010100 +100101111011 +100110010100 +000110000011 +111111000000 +001111100000 +000101101010 +101000011110 +011101010100 +101001011010 +101010100000 +111011111101 +100101001000 +010100111110 +110100010101 +000001011011 +110101000010 +101100000011 +100101000100 +011011101010 +111110010100 +000100101111 +011111101010 +011011000111 +011001100100 +101110111000 +111100001111 +000111110010 +000010010111 +001010001010 +010010001101 +110011101000 +000110111100 +110101101001 +101000000100 +110000001101 +100100111000 +101100000101 +110011001111 +010011001101 +100111000110 +001111111010 +000000000101 +000011000000 +111011101110 +111110101001 +110000101010 +110111101101 +010111110011 +000101110111 +010111010111 +011011111110 +101101000001 +101000101101 +101100100000 +100100000100 +001000010101 +110000001001 +010111111101 +100001101011 +110011110110 +100110101011 +011001111010 +101000001011 +011001101111 +101011100111 +001011010000 +111100111000 +111001001001 +010001011000 +101010110101 +001111001101 +000000111000 +010111001110 +110100010000 +111101111100 +000000001101 +110010001011 +110000111100 +101010000110 +101011011111 +101000011011 +001001110011 +001011111100 +100110001001 +101000011000 +011100110101 diff --git a/2021/solutions/day_03.py b/2021/solutions/day_03.py new file mode 100644 index 0000000..1335a3d --- /dev/null +++ b/2021/solutions/day_03.py @@ -0,0 +1,127 @@ +"""AOC Day 3""" + +from os import read +import pathlib +import time + +TEST_INPUT = """00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010""" + +def read_input(input_path: str) -> str: + """take input file path and return a str with the file's content""" + with open(input_path, 'r') as input_file: + input_data = input_file.read().strip() + return input_data + +def extract(input_data: str) -> list: + """take input data and return the appropriate data structure""" + return input_data.split('\n') + +def common_bits(lines: list) -> list: + """common_bits take a list of strings representing binary numbers and return a list of int one per digit. Each int can be 0, > 0 or < 0 if the number of 1s and 0s in that column are equal, more 1s or more 0s""" + col_num = len(lines[0]) + columns = [0]*col_num + for line in lines: + bits = list(map(int, line)) + for i, b in enumerate(bits): + if b == 0: + columns[i] -= 1 + elif b == 1: + columns[i] += 1 + return columns + +def common_bit(lines: list, column_number: int) -> int: + """common_bits take a list of strings representing binary numbers and a column number and return an int that can be 0, > 0 or < 0 if the number of 1s and 0s in that column are equal, more 1s or more 0s""" + cb = 0 + for line in lines: + bits = list(map(int, line)) + if bits[column_number] == 0: + cb -= 1 + elif cb == 1: + cb += 1 + return cb + +def part1(entries: list) -> int: + """part1 solver take the entries and return the part1 solution""" + gamma_rate = '' + epsilon_rate = '' + columns = common_bits(entries) + + for common_bit in columns: + if common_bit > 0: + gamma_rate += '1' + epsilon_rate += '0' + elif common_bit < 0: + gamma_rate += '0' + epsilon_rate += '1' + + return int(gamma_rate, 2)*int(epsilon_rate, 2) + +def part2(entries: list) -> int: + """part2 solver take the entries and return the part2 solution""" + og_rating = '' + co2s_rating = '' + + nums = entries.copy() + i = 0 + while len(nums) > 1: + cb = common_bit(nums, i) + new_nums = list() + for num in nums: + if cb >= 0 and num[i] == '1': + new_nums.append(num) + elif cb < 0 and num[i] == '0': + new_nums.append(num) + nums = new_nums.copy() + i += 1 + og_rating = nums[0] + + nums = entries.copy() + i = 0 + while len(nums) > 1: + cb = common_bit(nums, i) + new_nums = list() + for num in nums: + if cb >= 0 and num[i] == '0': + new_nums.append(num) + elif cb < 0 and num[i] == '1': + new_nums.append(num) + nums = new_nums.copy() + i += 1 + co2s_rating = nums[0] + + return int(og_rating, 2)*int(co2s_rating, 2) + +def test_input_day_03(): + """pytest testing function""" + entries = extract(TEST_INPUT) + assert part1(entries) == 198 + assert part2(entries) == 230 + +def test_bench_day_03(benchmark): + """pytest-benchmark function""" + benchmark(main) + +def main(): + """main function""" + input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem) + start_time = time.time() + input_data = read_input(input_path) + entries = extract(input_data) + print("Part 1: %d" % part1(entries)) + print("Part 2: %d" % part2(entries)) + end_time = time.time() + print("Execution time: %f" % (end_time-start_time)) + +if __name__ == "__main__": + main() \ No newline at end of file