~aperezdc/aoc2022

b6fb35a9a484abfc57597d72ea48da152fb7e83b — Adrian Perez de Castro 2 years ago d496cd3
Day 3, second part
2 files changed, 30 insertions(+), 0 deletions(-)

M .gitignore
A day03/day03b.py
M .gitignore => .gitignore +1 -0
@@ 1,2 1,3 @@
*.o
*.in
__pycache__/

A day03/day03b.py => day03/day03b.py +29 -0
@@ 0,0 1,29 @@
#! /usr/bin/env python3
# vim:fenc=utf-8
#
# Copyright © 2022 Adrian Perez de Castro <aperez@igalia.com>
#
# Distributed under terms of the MIT license.

from common import item_priority
import sys

def common_group_item(a, b, c):
    common = frozenset(a) & frozenset(b) & frozenset(c)
    assert len(common) == 1
    for item in common:
        return item

def line_groups(lines):
    lines = iter(lines)
    while True:
        try:
            yield (next(lines).strip(),
                   next(lines).strip(),
                   next(lines).strip())
        except StopIteration:
            return

print(sum((item_priority(item)
           for item in (common_group_item(a, b, c)
                        for a, b, c in line_groups(sys.stdin.readlines())))))