aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

commit e8d93980f59f62d6e372bdb2f66fe32095f91a52 (patch)
parent eb940bc2eb4dcba8b63838d9cb36ccc52f13c58f
Author: Alex Karle <alex@alexkarle.com>
Date:   Fri,  3 Dec 2021 00:40:32 -0500

day3: Add python implementation

This was much *much* rougher 20m ago when I finished... I was printing
out the binary strings and plugging them into an online calculator to
beat my friends to the punchline (little did I know int(,2) was so
easy!).

I went back and cleaned it up a bit, but I'm SURE it can be done
better.... I'm just tired :)

Diffstat:
A3/a.py | 28++++++++++++++++++++++++++++
A3/b.py | 45+++++++++++++++++++++++++++++++++++++++++++++
A3/input | 1000+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 1073 insertions(+), 0 deletions(-)

diff --git a/3/a.py b/3/a.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +import sys + +# First get the counts of bit by position +counts = [] +first = True +for l in sys.stdin: + i = 0 + for c in l.strip(): + if first: + counts.append([0, 0]) + counts[i][int(c)] += 1 + i += 1 + + first = False + +# Then build up the binary string summaries +gamma = "" +eps = "" +for c in counts: + if c[0] > c[1]: + gamma += "0" + eps += "1" + else: + gamma += "1" + eps += "0" + +print(int(gamma, 2) * int(eps, 2)) diff --git a/3/b.py b/3/b.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +import sys + +def search(nums, maximum): + # idx to consider + i = 0 + while len(nums) > 1: + # count just that digit in the string (rest is garbage) + counts = [0, 0] + for n in nums: + counts[int(n[i])] += 1 + + # filter the list down to most or least common + def addsome(c): + for n in nums: + if n[i] == c: + new.append(n) + + new = [] + if maximum: + if counts[0] > counts[1]: + addsome('0') + else: + addsome('1') + else: + if counts[0] <= counts[1]: + addsome('0') + else: + addsome('1') + + nums = new + i+=1 + + return nums[0] + +# We need multiple iterations on this one -- save to mem +if __name__ == '__main__': + nums = [] + for l in sys.stdin: + nums.append(l.strip()) + + oxygen = int(search(nums, maximum=True), 2) + co2 = int(search(nums, maximum=False), 2) + + print(oxygen * co2) diff --git a/3/input b/3/input @@ -0,0 +1,1000 @@ +011001101000 +010101111100 +000000111101 +001001001010 +010011000001 +111111001110 +001011101111 +110011010110 +100011111110 +101110000111 +000011011100 +001111111011 +011101001111 +111001010110 +011100011110 +001011000010 +100010111011 +000101010011 +000001101100 +010001011110 +100010101000 +111101011101 +110000100001 +011011010101 +110101100110 +001101101010 +111010110011 +011111111000 +010001010001 +000001010101 +001001011011 +101111110011 +100011010001 +111010010111 +011010011010 +101111011011 +101010111100 +001111100110 +010100110111 +010110101100 +000111110101 +111110101110 +111010000000 +011111111010 +000101011100 +000001010110 +101000100101 +100001011001 +011101110010 +110111100100 +110011001011 +000010001111 +000100010101 +110111001100 +010001100000 +101111110101 +111000011011 +000100001001 +101101011101 +001001011110 +101100111010 +011011000010 +110010010111 +101001010101 +000000101110 +000011011111 +010001000011 +011011001010 +101101111011 +100010001010 +001001011100 +010011110001 +000001111101 +100001000101 +011101011010 +000010101110 +001010101010 +101010011011 +000111001011 +000011101000 +110100101001 +000111111010 +100111100101 +111010001111 +111100110001 +011111101010 +110101000011 +001111101100 +101000100110 +100101100011 +000011010011 +010101011111 +010000110111 +110011001001 +101011011101 +001011100001 +000001100101 +001001001101 +001101001111 +001110110001 +010110101110 +001100110110 +001000001111 +101101001001 +011111011010 +111111101110 +111001100000 +100101011001 +010101100101 +100000110100 +011001001000 +111100110011 +111011110001 +110011111010 +001101001010 +111110011111 +110101100000 +111001000101 +010001111110 +111011010100 +110011010111 +100100110111 +111111011010 +011110101111 +001011110010 +010111011010 +011101101101 +011010101110 +011000110001 +010110101010 +011110010000 +111111011100 +111011000101 +100110101110 +010101100010 +101100011110 +000010110101 +010010011110 +110000000110 +011111010101 +000110110011 +101011000010 +000011000111 +101011010000 +101101101001 +010000101010 +101001001001 +111010110110 +111000111101 +000001010111 +101110001100 +110010110001 +100000110001 +101100101011 +100000111001 +010110100111 +101011001111 +101010111001 +010010110000 +001110010011 +010101101110 +011000010100 +100111111000 +011110100011 +110100100111 +101110101000 +101010101111 +101001000000 +111010000110 +101001000100 +010001100111 +011011010001 +000101010101 +010110000011 +100000101110 +110010001000 +111000100001 +001001101001 +110010001001 +100001111011 +101010010100 +110011100101 +010101100001 +011101010100 +110100101111 +110100001111 +011000101101 +001011101100 +010010010101 +011011111010 +010101000001 +100101100000 +101001111101 +010000000100 +110000110100 +000000001101 +100100111101 +111100110010 +001011001111 +101100001011 +001111010001 +101100011001 +100110110001 +010111000011 +011010110101 +100111100011 +000111110011 +010101111101 +010010000000 +011000011011 +001100011001 +001011001001 +101110101101 +101111110111 +101011100100 +000100110100 +110000101111 +011001111101 +000000000111 +111001001010 +110100001100 +000001001110 +111001101011 +100011011111 +110000001000 +110101011011 +001010000101 +001011111001 +011011111100 +010100110101 +110101111001 +010110010101 +010010001000 +001001110111 +011111111111 +100011100010 +000110001101 +011111010000 +010111110010 +110010100011 +101011110101 +110011011110 +010000000101 +011101100100 +110010011100 +011101101100 +000010001001 +000011101101 +000101100011 +010111111000 +010111100111 +010101000101 +110111100010 +110001101011 +111010100000 +101010101011 +011101010101 +000100110111 +000111100111 +000100111111 +111100001001 +010000100001 +110101011111 +100010000100 +000011011000 +111011110000 +101000011100 +110001001010 +001000010101 +110110000101 +100000011110 +001101111010 +010111001100 +111011111100 +100101001000 +010111101001 +110010010000 +110011010000 +000010100111 +100101111101 +110101100010 +110100011110 +101111010110 +110001001111 +011101000111 +001011001101 +110001000010 +011110100110 +000101100110 +000110111110 +001010111010 +100110000111 +001000010110 +100001110100 +000111000110 +101100011100 +110100110110 +100011010111 +000101110001 +001010010110 +101000000101 +100101101000 +000111111110 +010010010111 +110010001010 +001100110111 +110110110101 +011111011101 +110101100111 +100100100111 +011011000000 +111101001100 +011101000011 +011101001010 +101101101101 +100100110010 +001000000010 +010010001010 +111110111001 +000111010001 +011100111111 +011111011001 +110101000000 +011110000010 +001111010110 +010011111111 +110101111011 +101110111111 +001001100001 +011100111100 +110111001110 +011000000111 +001100010111 +001011010010 +010010001011 +101110000011 +100000101000 +100010100100 +000111011101 +001000111011 +010010101010 +111011010011 +110100010001 +011110001110 +111010110001 +010000010111 +010110101101 +111101100001 +110011100110 +000101111110 +110010101010 +000001010000 +001101111101 +011011110100 +110001010101 +011001000001 +001101101100 +100011011011 +101011100010 +011011100101 +100110110101 +110111001011 +000100101011 +001101010100 +010000011100 +101100111011 +011001011100 +011011010100 +011100110001 +010010110011 +101000110011 +111101010001 +111000000100 +101101100111 +001011111000 +010100000111 +011110101101 +111011101100 +110101110001 +001110010000 +010010000111 +101001111011 +100100111100 +000101101101 +101111110100 +100000101111 +011001110000 +011110011001 +000101010110 +000000000101 +110010001100 +000111100100 +010010011010 +001111110100 +011010001010 +101100101111 +011001001011 +100011111101 +101100000001 +100100101010 +000000000100 +111001110101 +000010011110 +110110100100 +111010010001 +011101010010 +011100101111 +010110000010 +010000110101 +110101010011 +001110011110 +111010011010 +000110101000 +001111000001 +010110110000 +001100110001 +110011011111 +000100111011 +110001000111 +011010111101 +000010100110 +010100101001 +010001000010 +000101110101 +101100110100 +111000110010 +111011110010 +010111001111 +100000101101 +101100001010 +100100001001 +111111111101 +001010101001 +000011011010 +101011010010 +111001000110 +111100010011 +111010011100 +100000111100 +011100001101 +110001101000 +101111011001 +001100001000 +101110010111 +011111100000 +001011100000 +101011101010 +110010101111 +001110000101 +001111011011 +011110101000 +111000001000 +010100111010 +010010111001 +110000110001 +110011000101 +001110000100 +010100101101 +101010110100 +100001010100 +100011010011 +100000010010 +011101010111 +001000010111 +001100111100 +000110100011 +101111101010 +010001010000 +001100100110 +111001111110 +101000001001 +000010110010 +110111101011 +101011000011 +001000111111 +100011001101 +011010010101 +110011100011 +110010011010 +110001010100 +100010111101 +101110000110 +010111001011 +110011011100 +110001100011 +000100011010 +111011100010 +011110110011 +011111100010 +000100001101 +100010010000 +110000000011 +101001011000 +101100110101 +001010101011 +011001011010 +001010001001 +001111011100 +111100010001 +100011010110 +001000110111 +011011010011 +001100000000 +100001111100 +000011110101 +100010000111 +000010110011 +000010111110 +011110000101 +010000001011 +001111110010 +001110001001 +011101111111 +010110110001 +110000000111 +010101011101 +010111001001 +100100010010 +101001101110 +100001001010 +010001011101 +100111010101 +100000101011 +000100101001 +110111001001 +100100000001 +010111010010 +111101111011 +011010010010 +111101101000 +100110111101 +010010001001 +100111011000 +110101111111 +111010101110 +010011110010 +101001100100 +010000101111 +010011001111 +110010011111 +101110100111 +011010100101 +111111001001 +010101100110 +101001001101 +001000101010 +010001001100 +101001101000 +110110100000 +011101000101 +100101010101 +101110111011 +010010101001 +110100011000 +110101111010 +110000100000 +010011101101 +111011001101 +100100011101 +110100010111 +011100110100 +010111011001 +011101010001 +101000000010 +100111111001 +100100001000 +001000001000 +001000000100 +011110110010 +010101010011 +101101101100 +110111111001 +010000010011 +100010100001 +000110101111 +101010101101 +100101111100 +000111010110 +001101101111 +110010000011 +110100001110 +011111111101 +111011010101 +001010100010 +100010100110 +101101110111 +010110011011 +111001110110 +011110101001 +110011100111 +000100111101 +101100011000 +101101001010 +011111100111 +010000011000 +100001100110 +000000101101 +000111010010 +110000001100 +111111101010 +001000100010 +111111111011 +001011100111 +000101110010 +010111110011 +010000011001 +111001111101 +110100001101 +010001101000 +110000101011 +011011000110 +110010111011 +100111101010 +011001101011 +001101010011 +011000100101 +110111101111 +001000100001 +010010111101 +101101101011 +011111011100 +111111010110 +001110000000 +101011110010 +011101011111 +001001101101 +011101110000 +011100011101 +111101001101 +010111010101 +010111101010 +001000101101 +100000000011 +111110101111 +000111101111 +001110010101 +101111001101 +111100110000 +110010100000 +011101110100 +100011001111 +010110001001 +111100101100 +101001110100 +011010010111 +011000100100 +001100110100 +001001111000 +001000000111 +110100000111 +011000001011 +001011000000 +110110110111 +000010111011 +001001000011 +000011100111 +110011001111 +010000111101 +001110100010 +000011110001 +011001111011 +110111000001 +111111000001 +011001100110 +110010101100 +111101010100 +010010000101 +000011011011 +110110010001 +010111111010 +010000010101 +110000011010 +110001101110 +000110101011 +101111000111 +001101100000 +010100000001 +111110001001 +000011110000 +111100111001 +000001001011 +011000010110 +000001000100 +011100111000 +001011111110 +011100000000 +000101001011 +111111110110 +111010011000 +001000111101 +011001110010 +001101011100 +100110001111 +110110111010 +001110101100 +010000101100 +010011000011 +100101110010 +000000111010 +000001111001 +111101011000 +101000011001 +111110000101 +101111111011 +100110111110 +000110010110 +011011101110 +100000010111 +110111001000 +000010101010 +001010011011 +101010101010 +101100000000 +000101101011 +010110100110 +110010010100 +001010110101 +001110000001 +010001101010 +111111110100 +001101110110 +111101010111 +101011101100 +110001101010 +100111010100 +101101111001 +001011100110 +010010001111 +100101110110 +110011000010 +001111010100 +000101011011 +110000000001 +011011001001 +001001011111 +000111011111 +100001001100 +111011010010 +010101011001 +000001110100 +000010100101 +110110010010 +100010111110 +011110111110 +000001101000 +001011000110 +000010001010 +101101010100 +011011010000 +000011111000 +101000001110 +011110101011 +110100111000 +000011000010 +111111010001 +011101110001 +100000111101 +110100101101 +110000100101 +011001110110 +110001010010 +010010110110 +011110001000 +111010110100 +101111110010 +110000110111 +111101110100 +110111101110 +100110010111 +001001100011 +001110111000 +101110100100 +010000101101 +010111100101 +011001100010 +011100000101 +101001111001 +100011001010 +100010000010 +100101000010 +111111100101 +100001101110 +101001010110 +100000100011 +100000011000 +000110000001 +000000011011 +011000110101 +011010111011 +111110000001 +001001110100 +001011011110 +110111010110 +011101110101 +101111000101 +100001010010 +000001111000 +001110010100 +111111101100 +001011010001 +111001111001 +100101101010 +111000000010 +010101001010 +001010000010 +101011000110 +110011001010 +001000011000 +001010010011 +110000111101 +001001100000 +111101100111 +011001001100 +001100111011 +100001010101 +111010100011 +111000011111 +001000000000 +101110011001 +000000000011 +111011000111 +011010100011 +110000110000 +000011001001 +011000010001 +011110000011 +011101000000 +010100110011 +010110010001 +111001101111 +110111110100 +110111110110 +100111001011 +111100110110 +101010001110 +100110000010 +011011100001 +001101010010 +010001110011 +010011110000 +110010100111 +001101110100 +101010101100 +110000111000 +011101110110 +101011110001 +110011000000 +010000011011 +001011010000 +010111010001 +010100010101 +110101000100 +010011101011 +010110111001 +101111000011 +011011100000 +000010100010 +001100110000 +110000111001 +100100100000 +001101000010 +100000010011 +100010011110 +110101010000 +000100000101 +000111000001 +000001111011 +100111011101 +110111110111 +011101111000 +001001000110 +100110010011 +100011100111 +010011010000 +010011010111 +001101100101 +010000010110 +000101010001 +011111010100 +010010111100 +001010101110 +110000001011 +011001010101 +000001111110 +010111111011 +011110010010 +001001101100 +101100101000 +100000001110 +110001001110 +011110111011 +001000110011 +111100110111 +001101100111 +000110110000 +000010011011 +100110111010 +111110001100 +000000101100 +110000111100 +100110111100 +110111111100 +110000111010 +100001100100 +100010011010 +011011100110 +110100010110 +001001010101 +000110101110 +101001100000 +100010110011 +111000001101 +101110100110 +100010111100 +000101110110 +000011011001 +000001010100 +101100111001 +011101111101 +011110011000 +111010001011 +010101110101 +100001100101 +100110100010 +100110011000 +001010100110 +011001010111 +111111001010 +111111001000 +111110100101 +000101000011 +110101101111 +110100010010 +011100000010 +001000010100 +101100000111 +001010111111 +101001010011 +101011001000 +001100000011 +010010101000 +101101111101 +101001010010 +100101111011 +101110101110 +100000100100 +000000000010 +011101100011 +110000110110 +101000110000 +111011111010 +010111000110 +001111110110 +100111110110 +100110011101 +000000001000 +101110111010 +000101011000 +001011100011 +011101100111 +000011000011 +000101011001 +110101101101 +101000001111 +111000010101 +000011100010 +011010111001 +001100100101 +010111111101 +010101111011 +000101001100 +101010000111 +111100111111 +100000000111 +110111110011 +000110110111 +010011011111 +100110010100 +000100111110 +001000111001 +011111000001 +001010100101 +101010001100 +011100001011 +000101001111 +010010101111 +101000111011 +001101000001 +100100100001 +100010101111 +100111111110 +011011111001 +101000000001 +111001101101 +101011010101 +111111100110 +001010010100 +011101111001 +001100100000 +101001110110