From 3bf0fcec573a1e944933460df0d50cba1026d10f Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Fri, 3 Dec 2021 12:08:16 +0100 Subject: [PATCH] Solve day3 part 1 --- 2021/day03_gamma.py | 52 ++ 2021/inputs/day03 | 1000 +++++++++++++++++++++++++++++++++++++++ 2021/inputs/day03-test1 | 12 + 2021/new_day | 14 + 2021/template.py | 28 ++ 5 files changed, 1106 insertions(+) create mode 100644 2021/day03_gamma.py create mode 100644 2021/inputs/day03 create mode 100644 2021/inputs/day03-test1 create mode 100755 2021/new_day create mode 100644 2021/template.py diff --git a/2021/day03_gamma.py b/2021/day03_gamma.py new file mode 100644 index 0000000..4b562e4 --- /dev/null +++ b/2021/day03_gamma.py @@ -0,0 +1,52 @@ +from collections import defaultdict + + +def main(filename: str, expected_part_1: int = None, expected_part_2: int = None): + print(f"\n+ Running on {filename}") + with open(filename) as f: + data = f.read().strip().split("\n") + + counter_part_1 = solve_part_1(data) + + print(f"1. Found {counter_part_1}") + if expected_part_1: + assert expected_part_1 == counter_part_1 + + counter_part_2 = solve_part_2(data) + print(f"2. Found {counter_part_2}") + if expected_part_2: + assert expected_part_2 == counter_part_2 + + +def solve_part_1(data): + count_ones = defaultdict(int) + total = len(data) + for binary in data: + for index, digit in enumerate(binary): + if digit == "1": + count_ones[index] += 1 + gamma = "" + epsilon = "" + for index in range(len(data[0])): + most_common = _most_common(count_ones, total, index) + gamma += most_common + epsilon += "0" if most_common == "1" else "1" + return int(gamma, 2) * int(epsilon, 2) + + +def _most_common(count_ones, total, index): + """ + Return 1 if the most common value is 1, otherwise 0. + """ + if count_ones[index] >= total / 2: + return "1" + return "0" + + +def solve_part_2(data): + return 0 + + +if __name__ == "__main__": + main("inputs/day03-test1", expected_part_1=198) + main("inputs/day03") diff --git a/2021/inputs/day03 b/2021/inputs/day03 new file mode 100644 index 0000000..721a3dd --- /dev/null +++ b/2021/inputs/day03 @@ -0,0 +1,1000 @@ +000111111001 +111011110110 +101111111000 +111100100110 +001111011000 +011100010110 +000101001001 +100001100101 +000011111100 +110000101000 +011110110101 +011000111100 +011000001011 +010111101101 +011111100010 +100110001111 +100111100100 +111110010100 +101001001110 +110101111110 +110110100000 +001010001110 +101100001100 +111001011000 +100101111111 +001001010100 +100111011000 +100101100000 +010001010101 +000111001100 +001100011110 +101100010101 +111011000011 +010000001110 +110000000110 +001010110000 +100011001111 +110100111101 +111111010100 +000101110111 +011110101111 +100110000110 +010011000000 +000110010000 +101101101011 +011101100011 +011111101001 +100101111011 +100010101010 +010000011011 +100110101011 +010110000010 +110101010000 +001000101100 +101000001000 +000110111110 +001000111001 +110000011001 +011101001010 +101011100011 +111010010010 +111100110111 +011110100001 +101111111111 +011100001001 +000011100100 +100010101001 +101101000100 +010110111110 +111001001011 +010110010001 +100110001110 +110101100111 +110010111110 +000011101010 +100110000101 +011100000111 +001100100011 +001001011101 +111111101001 +110110000000 +001101100000 +111101100110 +011110100011 +001011010111 +000001010100 +100111000110 +111000101111 +111010100011 +101101011011 +010110010011 +010110111111 +100011011000 +001010111101 +100100110010 +110001100000 +010111011111 +000011111111 +010110000011 +010100000001 +111110011100 +110110110001 +000110001010 +101100101110 +110010011111 +101101110011 +000110011101 +101101111010 +101110111011 +100000000011 +010110010111 +010001111001 +100001110011 +011011111011 +101101010011 +010100101111 +001001000000 +100110101000 +000101100010 +011110000001 +000011101011 +110111110101 +010110100100 +010001011101 +110101001010 +011010011011 +110001110011 +011001011011 +100101110010 +111110000101 +100001100011 +010100000011 +111101101001 +101111101001 +001111011101 +100011101111 +100100000001 +010101101111 +110000110001 +000000101011 +011000011110 +110111100010 +110010101110 +010110100111 +101011010000 +101011100010 +100010111000 +000000001110 +010010001001 +001011010010 +100011001101 +110110100001 +011110111000 +000110101010 +010010111011 +011111100101 +010101111011 +100011010100 +100000111010 +010011001101 +100010100111 +111111100010 +111100001111 +111010001101 +100011000001 +111001101011 +111110010011 +010100001010 +101001111100 +101010111110 +010110110010 +000011111101 +010011100000 +010001100111 +110111010100 +111101100000 +001101101111 +110110000101 +111110110101 +010011100101 +000011000101 +011010001010 +000111000011 +000011000110 +101010001000 +101011110111 +011010010011 +011111000001 +011101011010 +001011001001 +001100110010 +010011111001 +010101111000 +010000111101 +011100110000 +101100000011 +000111100110 +011001110110 +111000100001 +110010001010 +010000000011 +110110110111 +111110111100 +010001101000 +000001100011 +111001001101 +010111101011 +001110000010 +100011100010 +001011010000 +010000001011 +011100011010 +010100001001 +100110011111 +011110010011 +010100011101 +001111110100 +100001100111 +111110100010 +000111110001 +011100011001 +000101010101 +101000000100 +101010110001 +111101000101 +000110000010 +100101001010 +101001111101 +111010011001 +101100101111 +101010101001 +110101001101 +111000101010 +001101010111 +001111111011 +001101011101 +011101000101 +100100100010 +111100010011 +101111101010 +101011001100 +101101111011 +101110011000 +111100001001 +011111001010 +010110011010 +010111101111 +010101001101 +111011000101 +110010011110 +111101011001 +111101000100 +000010001101 +000010011110 +001111000001 +110110111011 +100001001011 +100111110110 +011110100010 +011001110100 +010001010010 +111111011000 +000110111011 +001100001010 +001110000101 +001110101100 +010100111001 +011101000111 +001001011001 +101111000110 +111011011011 +000010010100 +100101011101 +111110011011 +101000101111 +010110001111 +000010101100 +110011011001 +011001010100 +010010010011 +100000000010 +100110100101 +010011000110 +111010110111 +001100101000 +010110000100 +001011011110 +001001111101 +111001101010 +110101001111 +101111100000 +001100000110 +011111100110 +100000001000 +101010001001 +010101110011 +011011001111 +000000111000 +100100011011 +101100110001 +110111000000 +101100111000 +010010010101 +111011000001 +000000000101 +010111000001 +110000010101 +010010101111 +101001110010 +011111000010 +001011100001 +101111000010 +001110101101 +111110100101 +111011001101 +100011001100 +010011010110 +010001111000 +110010011011 +001100011000 +110011000000 +000000000001 +100111111011 +000111000110 +010110101110 +101110110100 +010100110110 +100011010110 +010100001101 +111110010110 +010000101011 +110100110101 +001101011000 +010101110101 +011100110111 +000100110111 +001110111001 +000010000010 +100110111001 +001011011010 +000000010111 +101011000001 +110110000111 +101110001101 +000000000111 +011000010100 +010111111101 +111011110011 +110100111111 +000001010111 +101000101101 +110100010110 +011101101000 +100000001011 +011000000111 +110100010011 +011100010010 +010110001001 +111101111001 +101001001010 +110000100011 +011001101001 +010101110100 +101000011000 +101111100111 +010110110100 +001000111110 +000101000001 +111011011110 +000101101111 +111000110001 +010101010100 +110001100001 +000100010110 +010011000100 +011011010111 +001100010000 +000110010111 +111111011001 +100010011011 +100100111101 +110100010010 +100001110111 +100000101011 +010001000100 +101110100101 +000111100001 +111001101111 +101111011110 +001111000011 +011011101100 +100001010000 +000110100110 +110111010110 +101100010110 +101110001001 +100011010101 +111111010001 +011111101000 +110111110001 +010111000011 +001000100101 +111101110010 +101111010000 +010001110101 +101011100000 +010101101110 +100010000100 +010100010011 +001011111010 +110000110110 +100001101010 +001001100101 +111111110101 +000011011100 +011111010000 +101110011110 +010011011101 +111111000111 +010000011101 +110011001001 +100011101110 +111100011011 +000110110100 +101000000110 +100101100001 +101100010000 +101101011110 +010001111110 +010101001010 +101011111111 +100001101110 +110000001000 +010010011011 +111011111011 +001101101001 +011001010001 +110110111110 +001111010001 +000011001101 +110111100100 +000111110100 +000100111101 +011101100111 +111001000101 +110001111011 +110000110010 +101000110000 +010100000000 +101011001110 +110111111000 +010010000011 +100001000111 +111111010110 +111000010011 +010101001111 +101001110001 +100011011010 +001001010101 +100111011001 +111011010111 +001010010100 +000101110011 +111000101011 +011111000000 +101001111010 +111000000100 +010001101101 +100000100010 +100001010100 +110000010011 +000000001101 +100000011101 +011010001100 +001101111111 +010011011001 +011011011101 +101001010001 +101111110111 +100010110011 +001000110010 +101111001000 +001011001100 +001110010011 +001010001101 +110101001001 +101100010001 +101001110110 +000000100000 +100011000110 +110101000100 +001011101010 +100000011000 +010010011000 +000001010011 +111110101010 +110100111010 +010010011110 +111111101110 +000010111110 +000010101110 +000110100000 +001011000100 +010111100011 +000111111011 +010100001000 +001100010111 +010111100101 +110010010001 +101000100001 +110111011111 +110111100101 +000110110000 +011110000100 +110001001110 +111110001001 +111100101010 +011011100011 +110001000000 +000000000100 +101011011110 +000000001010 +010110010000 +011011010110 +101001000101 +110101011001 +000010000101 +111011000111 +010111001110 +011100011000 +001111100001 +110101011011 +010110110001 +101010101101 +101100110000 +000011101101 +000001111111 +111101001110 +111000111110 +100010111001 +100010001011 +000100101001 +011001111110 +111001110001 +001100110101 +011010001001 +011000101101 +110100001010 +100001010101 +011000000001 +111001001000 +100111010001 +110000101101 +100101000010 +100000011001 +001100010110 +110100000100 +100110110010 +100100001001 +100010101100 +101010100110 +011000110000 +110001000110 +011010000000 +111111100000 +111001001010 +101001101110 +111000000101 +000000010010 +011011110001 +000011011110 +000110110101 +101100011011 +111111110011 +010100010101 +111001010110 +000001110010 +111001101000 +111110100000 +001001110001 +101111111011 +101110101010 +100100100100 +100110110001 +100010000010 +111101101101 +111001101110 +110010110011 +111110100111 +100001111010 +110011111011 +100100001010 +110010110111 +001110111100 +101011111110 +011000111001 +000011100111 +000111101000 +000001111011 +100101111001 +011100011100 +100100111010 +111000010101 +011101110001 +011010000010 +001111001010 +101001100001 +000011101001 +010110110011 +101000100010 +110100111001 +111110100001 +100010111100 +100100111011 +000011010010 +001100001001 +011101100110 +001111101111 +000110010010 +001100001111 +100110000100 +001111001111 +110110100010 +001111001000 +000010011100 +010101101100 +010110000101 +001101100111 +000010110100 +100011011100 +110010110100 +001001100100 +001010011011 +001000011101 +110000111100 +110001001010 +010101001000 +110011101010 +001011110111 +110100000101 +001101011110 +111100111000 +011111010100 +101001001100 +110011111000 +010010001000 +000000100010 +101010100111 +101010001110 +101000000001 +010011100111 +111000110101 +110011011111 +000001111000 +011000010111 +101001011111 +011011100000 +110000100001 +010111000110 +001001111111 +010111111000 +001111110010 +111101000111 +100101100011 +110001111010 +101000110110 +110000100000 +010110001100 +110101001000 +110100010000 +011110000110 +100110011000 +011001111100 +010111111110 +001001100001 +100110111010 +111001110000 +010010010110 +100111111110 +101111101101 +001000010001 +110001001001 +101110010011 +001000101010 +010001101111 +011101101010 +100000000101 +101000111000 +110111110100 +111111010011 +011101010100 +101001111111 +101101101000 +000011100010 +010100011001 +000111100010 +000100011110 +110101101110 +010010101010 +000010100001 +010101100101 +000101110000 +110101111011 +000100011010 +110110110101 +000101111000 +111100100111 +011011111110 +110111001011 +111001110100 +010001011000 +111001010100 +110001111111 +111100010101 +110110001000 +001010010011 +110011110010 +000000100110 +100001111001 +010110100101 +100010001100 +001000010011 +001001000100 +110110011010 +111001111110 +101110001011 +010000010010 +101001101101 +001000010000 +110011000011 +001111101110 +110110111000 +000101010000 +001100111101 +000000100101 +010111010100 +101101101111 +110010001000 +101001011011 +011111110000 +001101111011 +010110001110 +011000001101 +001000011111 +010011101000 +111110111111 +011110000101 +001011001110 +110001001101 +100011111111 +110100101110 +000111010101 +101001000010 +000011001010 +001111110101 +110100111000 +111010000010 +001011001101 +110111011110 +101100001000 +111011101111 +101001011101 +100001100100 +010111000100 +101111010100 +001111001101 +101110110001 +011100110100 +001101100101 +110100011000 +110110110100 +100010011111 +011001001101 +000000110001 +011010111000 +110111111101 +100100010011 +010011101101 +100100110001 +110010011001 +100110011011 +101111100110 +100000010011 +010111011100 +001011000101 +110100110010 +000111001110 +000011011101 +000001011010 +010010100101 +101100100110 +100001101111 +101011100110 +001100001000 +001101000010 +111111111011 +110111010101 +111000010100 +110011101101 +100110010111 +001110010100 +011010110001 +010011110010 +011101000000 +101001000111 +000010101011 +110100000111 +011101000010 +011000111010 +110101100000 +110100011011 +111111100101 +000100100001 +111100001010 +101110111000 +011000010010 +100001101101 +001111101001 +111101111100 +110100010101 +100011111101 +101000101010 +101010110100 +111101100011 +000000000010 +011001111111 +010101011000 +001000111111 +111010101010 +100001111100 +001101001000 +111100000101 +000100011101 +010010110110 +010100010111 +110101010101 +000011001111 +010010110000 +110110111001 +010000100000 +000000011000 +110011011100 +010110001010 +100001001111 +100010011101 +111111111101 +101100011000 +010101000101 +111000101101 +101000011010 +101101000001 +111001100001 +010001010100 +100101110011 +010000111111 +000110000001 +011011000000 +111011001100 +111001100011 +011101001011 +001000110000 +010010011111 +101110001000 +011101011011 +110010001101 +000000111011 +001001001111 +010100000010 +001000111000 +111110000110 +000010111111 +010001100110 +011100011111 +011101001000 +110100000000 +110111111100 +010010011010 +011111000110 +001000100111 +101011111000 +001101110110 +100010010101 +110000100100 +001101110100 +111111010111 +110011001110 +101110100111 +111111000010 +101110010010 +011000000010 +001110011001 +100110010100 +100111111010 +110110100100 +010101001011 +110101011101 +001100000011 +101110000000 +111100011010 +010011111110 +000110101111 +010011001100 +101011011111 +001101101010 +010100110000 +010011111010 +111110000001 +101001101011 +000000110111 +010000000110 +011101011001 +100001011111 +011110111001 +110111110000 +011100001000 +110000110000 +001101111001 +111011101010 +010000111010 +111001000000 +011011010000 +000001101001 +111011001011 +010111111011 +101000010111 +001011010011 +101100011111 +011111011000 +011111100001 +101100001010 +100111101000 +111110110110 +011001100011 +001111100000 +111011001110 +100000001101 +100010100001 +000010100100 +111001101001 +010100111101 +000011001110 +101111001100 +111010110010 +100000100111 +000011100101 +000100000010 +100010011001 +010001100011 +000011110010 +010111011001 +011001010000 +011011110011 +111000100011 +011110011101 +000000100011 +001011100100 +001111001100 +101100110100 +101100110101 +001010101111 +011000110010 +100010100011 +000010001100 +101101010010 +001011011000 +100010100010 +010010100010 +001111101100 +001000110111 +011111000111 +011110111101 +011010001011 +111101001100 +101010110110 +010101110010 +011001111011 +011101110010 +001110001111 +110111000101 +100011110011 +001010010000 +000101001100 +110011100110 +000101101010 +010111100110 +111001111010 +001000000100 +110111011001 +110001011001 +100101110001 +001010001001 +100100111001 +001100000010 +000001011000 +001010100110 +000101110101 +010101111010 +100100110000 +110011001010 +011001110101 diff --git a/2021/inputs/day03-test1 b/2021/inputs/day03-test1 new file mode 100644 index 0000000..665fd57 --- /dev/null +++ b/2021/inputs/day03-test1 @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 \ No newline at end of file diff --git a/2021/new_day b/2021/new_day new file mode 100755 index 0000000..9e83367 --- /dev/null +++ b/2021/new_day @@ -0,0 +1,14 @@ +#!/bin/bash +set -euxo pipefail + +if [[ $# -ne 2 ]]; then + >&2 echo "Usage: $0 " + exit 2 +fi + +filename=day$1_$2.py +cp template.py $filename +sed -e "s/dayXX/day$1/g" -i "" ./$filename +mkdir -p inputs +touch inputs/day$1 +touch inputs/day$1-test1 diff --git a/2021/template.py b/2021/template.py new file mode 100644 index 0000000..dafbc7b --- /dev/null +++ b/2021/template.py @@ -0,0 +1,28 @@ +def main(filename: str, expected_part_1: int = None, expected_part_2: int = None): + print(f"\n+ Running on {filename}") + with open(filename) as f: + data = f.read().strip().split("\n") + + counter_part_1 = solve_part_1(data) + + print(f"1. Found {counter_part_1}") + if expected_part_1: + assert expected_part_1 == counter_part_1 + + counter_part_2 = solve_part_2(data) + print(f"2. Found {counter_part_2}") + if expected_part_2: + assert expected_part_2 == counter_part_2 + + +def solve_part_1(data): + return 0 + + +def solve_part_2(data): + return 0 + + +if __name__ == "__main__": + main("inputs/dayXX-test1") + main("inputs/dayXX")