{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "with open('input', 'r') as file:\n", " content = file.readlines()\n", "\n", "lines = [line.strip().split(':') for line in content]\n", "lines = [(int(x),[int(val) for val in y.strip().split(' ')]) for x,y in lines]\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import itertools\n", "\n", "# Input lists\n", "values = [1, 2, 3,5,6]\n", "\n", "def test(values, expected):\n", " operators = ['+', '*']\n", " # Generate and calculate combinations directly\n", " for signs in itertools.product(operators, repeat=len(values)-1):\n", " # Start with the first number\n", " result = values[0]\n", " # Apply each operator with the subsequent number\n", " for num, sign in zip(values[1:], signs):\n", " if sign == '+':\n", " result += num\n", " elif sign == '*':\n", " result *= num\n", " if result == expected:\n", " return True\n", " return False\n", "\n", "test(values,51)\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1260333054159" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "total = 0\n", "for result, values in lines:\n", " if test(values,result):\n", " total += result\n", "\n", "total" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Part 2" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import re\n", "\n", "splitter = re.compile(r'(\\+|\\*|\\|)')\n", "\n", "\n", "\n", "\n", "def evaluate_expression_no_precedence(expression,expected):\n", " # Handle the case where the expression contains only a number\n", " if expression.isdigit():\n", " return int(expression) == expected\n", "\n", " # Split the expression into numbers and operators using regex\n", " parts = splitter.split(expression)\n", " parts = [int(parts[i]) if i % 2 == 0 else parts[i] for i in range(len(parts))] # Convert numbers to int\n", "\n", " # Evaluate left-to-right\n", " result = parts[0] # Start with the first number\n", " for i in range(1, len(parts), 2): # Step through operators and numbers\n", " if result > expected:\n", " return False\n", " operator = parts[i]\n", " number = parts[i + 1]\n", " if operator == '+':\n", " result += number\n", " elif operator == '*':\n", " result *= number\n", " elif operator == '|':\n", " result = int(f\"{result}{number}\")\n", "\n", " return result == expected\n", "\n", "evaluate_expression_no_precedence(\"17|8+14\",192)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Input lists\n", "def test2(values, expected,debug=False):\n", " operators = ['+', '*', '|']\n", " for signs in itertools.product(operators, repeat=len(values)-1):\n", " expression = f\"{values[0]}\"\n", " for num, sign in zip(values[1:], signs):\n", " expression += f\"{sign}{num}\"\n", " if evaluate_expression_no_precedence(expression,expected):\n", " return True\n", " \n", " \n", " return False\n", "\n", "test2([17, 8, 14],192,True)\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "162042343638683\n" ] } ], "source": [ "\n", "total = 0\n", "for result, values in lines:\n", " if test2(values,result):\n", " total += result\n", "\n", "print(total)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 2 }