~aperezdc/aoc2022

99338f80e6b051ee0b022ddafac07bf0e9c67235 — Adrian Perez de Castro 2 years ago 79dfcbb
Day 03, first part
2 files changed, 34 insertions(+), 0 deletions(-)

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

def find_common_item(line):
    assert len(line) % 2 == 0
    nitems = len(line) // 2
    common = frozenset(line[:nitems]) & frozenset(line[nitems:])
    assert len(common) == 1
    for item in common:
        return item

def item_priority(item):
    item = ord(item)
    if ord('a') <= item <= ord('z'):
        return item - ord('a') + 1
    elif ord('A') <= item <= ord('Z'):
        return item - ord('A') + 27
    else:
        assert False, "unreachable"

import sys

print(sum((item_priority(item) for item in (find_common_item(line.strip()) for
                                            line in sys.stdin.readlines()))))

A day03/day03.test => day03/day03.test +6 -0
@@ 0,0 1,6 @@
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw