DayOneAndTwo

This commit is contained in:
2024-09-21 21:22:18 +02:00
parent 42f57c189e
commit acac859d2b
3 changed files with 296 additions and 7 deletions

View File

@ -61,14 +61,14 @@
"\n",
" if c is None:\n",
" break\n",
" sublist = []\n",
" front = []\n",
"\n",
" for i in c:\n",
" if i in numbers:\n",
" sublist.append(str(i)) \n",
" front.append(str(i)) \n",
" \n",
" if len(numbers) > 1:\n",
" Solution += int(sublist[0] + sublist[len(sublist)-1])\n",
" Solution += int(front[0] + front[len(front)-1])\n",
" \n",
" elif len(numbers) == 1:\n",
" Solution += int(numbers[0]*2)\n",
@ -82,6 +82,112 @@
"source": [
"The right answer is: 55447"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--- Part Two ---\n",
"\n",
"\n",
"Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid \"digits\".\n",
"\n",
"Equipped with this new information, you now need to find the real first and last digit on each line. For example:\n",
"\n",
"two1nine\n",
"eightwothree\n",
"abcone2threexyz\n",
"xtwone3four\n",
"4nineeightseven2\n",
"zoneight234\n",
"7pqrstsixteen\n",
"In this example, the calibration values are 29, 83, 13, 24, 42, 14, and 76. Adding these together produces 281.\n",
"\n",
"What is the sum of all of the calibration values?"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"54706\n"
]
}
],
"source": [
"def openafile(filename):\n",
" with open(filename, 'r') as file:\n",
" for line in file:\n",
" yield line.strip()\n",
"\n",
"def lookfornum(lines, letter, ir):\n",
" line = ''\n",
" for i in lines[:lines.index(letter)]:\n",
" line += i\n",
" if 'one'[::ir] in line:\n",
" return 1\n",
" elif 'two'[::ir] in line:\n",
" return 2\n",
" elif 'three'[::ir] in line:\n",
" return 3\n",
" elif 'four'[::ir] in line:\n",
" return 4\n",
" elif 'five'[::ir] in line:\n",
" return 5\n",
" elif 'six'[::ir] in line:\n",
" return 6\n",
" elif 'seven'[::ir] in line:\n",
" return 7\n",
" elif 'eight'[::ir] in line:\n",
" return 8\n",
" elif 'nine'[::ir] in line:\n",
" return 9\n",
" return -1\n",
"\n",
"line = openafile(\"calibration.txt\")\n",
"numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']\n",
"Solution = 0\n",
"\n",
"while True:\n",
" c = next(line, None)\n",
" if c is None:\n",
" break\n",
" \n",
" front = []\n",
" back = []\n",
"\n",
" for i in c:\n",
" if i in numbers:\n",
" found_num = lookfornum(c, i, 1)\n",
" if found_num > -1:\n",
" front.append(str(found_num))\n",
" break\n",
" front.append(str(i))\n",
" break\n",
"\n",
" for i in c[::-1]:\n",
" if i in numbers:\n",
" found_num = lookfornum(c[::-1], i, -1)\n",
" if found_num > -1:\n",
" back.append(str(found_num))\n",
" break\n",
" front.append(str(i))\n",
" break\n",
"\n",
" front.extend(back[::-1])\n",
"\n",
" if len(front) > 1:\n",
" Solution += int(front[0] + front[-1])\n",
" else:\n",
" Solution += int(front[0] * 2)\n",
"\n",
"print(Solution)\n"
]
}
],
"metadata": {