advent-of-code

advent of code (partial solutions)
Log | Files | Refs | README

commit ec87d8e08bc4f6b4d78d74bc9420741b2e2b0847
parent ef5b714259c2819e7ddaf599a60c94b1104c4497
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Thu,  2 Dec 2021 21:58:34 -0800

Add day 3

Diffstat:
A2021/aoc03-1.c | 39+++++++++++++++++++++++++++++++++++++++
A2021/aoc03-2.py | 26++++++++++++++++++++++++++
2 files changed, 65 insertions(+), 0 deletions(-)

diff --git a/2021/aoc03-1.c b/2021/aoc03-1.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <string.h> + +#define LENGTH 12 + +int main() { + char line[LENGTH]; + int counts[LENGTH]; // counts 1s + int total = 0; + for(int c=0; c<LENGTH; c++ ){ + counts[c] = 0; + } + while(scanf("%s", &line) == 1) { + for(int c=0; c<LENGTH; c++ ){ + if (line[c] == '1') { + counts[c]++; + } + } + total++; + } + unsigned int bin = 0; + unsigned int binverse = 0; + for(int c=0; c<LENGTH; c++ ){ + int r = counts[c]; + bin <<= 1; + binverse <<= 1; + if (r > (total - r)) { // 1 most common + bin++; + } else { + binverse++; + } + } + printf("%d ", bin * binverse); + + + printf("\n%d\n", 1); + printf("%d\n", 2); + return 0; +} diff --git a/2021/aoc03-2.py b/2021/aoc03-2.py @@ -0,0 +1,26 @@ +import sys +from collections import Counter + +lines = sys.stdin.read().split("\n")[:-1] +linesb = lines.copy() +for i in range(len(lines[0])): + c = Counter(line[i] for line in lines) + if c['0'] == c['1'] or c['0'] < c['1']: + d = "1" + else: + d = "0" + lines = [line for line in lines if line[i] == d] + if len(lines) == 1: + a = int(lines[0], 2) + +for i in range(len(linesb[0])): + c = Counter(line[i] for line in linesb) + if c['0'] == c['1'] or c['0'] < c['1']: + d = "0" + else: + d = "1" + linesb = [line for line in linesb if line[i] == d] + if len(linesb) == 1: + b = int(linesb[0], 2) + print(a, b, a * b) + break