{ "cells": [ { "cell_type": "markdown", "id": "297093bf-b2ad-4645-935b-b2adea775c55", "metadata": {}, "source": [ "### Part 1" ] }, { "cell_type": "code", "execution_count": 3, "id": "129613b8-f732-4f41-a745-edf2c979b213", "metadata": {}, "outputs": [], "source": [ "def item_priority(item: str) -> int:\n", " if item.isupper():\n", " return ord(item) - 38\n", " else:\n", " return ord(item) - 96\n", "\n", "def string_halves(string: str) -> [str, str]:\n", " length = int(len(string) / 2)\n", " first_part = string[:length]\n", " second_part = string[length:]\n", " return first_part, second_part" ] }, { "cell_type": "code", "execution_count": 14, "id": "069cad24-9cd2-4d1a-bfa5-f265fa578909", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7795" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open(\"aoc3_input.txt\", \"r\") as f:\n", " sum_priority = 0\n", " for rucksack in f:\n", " comp1, comp2 = map(set, string_halves(rucksack.strip()))\n", " intrsct = comp1.intersection(comp2)\n", " sum_priority += sum(map(item_priority, intrsct))\n", "\n", "sum_priority" ] }, { "cell_type": "markdown", "id": "86d642f4-2504-42c0-a703-2dc063810a2c", "metadata": {}, "source": [ "### Part 2" ] }, { "cell_type": "code", "execution_count": 19, "id": "921090d1-2d2b-4161-aad2-23af1b358645", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2703" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open(\"aoc3_input.txt\", \"r\") as f:\n", " sum_badge_priority = 0\n", " for r1 in f:\n", " r1 = set(r1.strip())\n", " r2, r3 = set(next(f).strip()), set(next(f).strip())\n", " sum_badge_priority += sum(map(item_priority, r1.intersection(r2).intersection(r3)))\n", "\n", "sum_badge_priority" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.7" } }, "nbformat": 4, "nbformat_minor": 5 }