Skip to content

Commit

Permalink
Day 21
Browse files Browse the repository at this point in the history
  • Loading branch information
flint-hasset authored Dec 22, 2020
1 parent 516942b commit 2ce32dc
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 0 deletions.
213 changes: 213 additions & 0 deletions Advent of Code 2020 - 21.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import math"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"with open(r'C:\\Users\\stefa\\OneDrive\\Documents\\Python Scripts\\Problem 21 Input.txt') as f:\n",
" lines = f.read().split('\\n')\n",
"lines = lines[:-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 1"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"for x in range(len(lines)):\n",
" lines[x] = lines[x].replace('peanuts', 'peanuds')\n",
" lines[x] = lines[x].replace('shellfish', 'shellfist')\n"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"ing_list =[]\n",
"aller_list = []\n",
"\n",
"for line in lines:\n",
" \n",
" ingred = line.split(\" (contains \")[0]\n",
" aller = line.split(\" (contains \")[1][:-1]\n",
" for x in ingred.split(\" \"):\n",
" ing_list.append(x)\n",
" for y in aller.split(\", \"):\n",
" aller_list.append(y)\n",
"ing_list = list(set(ing_list))\n",
"aller_list = list(set(aller_list))\n"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'fish': 'vhkj',\n",
" 'soy': 'dfzqlk',\n",
" 'peanuds': 'tvdvzd',\n",
" 'dairy': 'smfz',\n",
" 'sesame': 'lcb',\n",
" 'shellfist': 'lrqqqsg',\n",
" 'nuts': 'qzlmr',\n",
" 'wheat': 'shp'}"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"aller_dict = {}\n",
"while len(aller_dict)<len(aller_list):\n",
" for aller in aller_list:\n",
" aller_line = [x for x in lines if aller in x]\n",
" ing_list = [x for x in aller_line[0].split(\" (contains \")[0].split(' ')]\n",
" for line in aller_line[1:]:\n",
" ings = []\n",
" ingred = line.split(\" (contains \")[0]\n",
" for x in ingred.split(\" \"):\n",
" if x not in aller_dict.values():\n",
" ings.append(x)\n",
" ing_list = set(ing_list)&set(ings)\n",
" if len(ing_list)==1:\n",
" aller_dict[aller] = list(ing_list)[0]\n",
" \n",
"aller_dict"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2302"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"total_ing = 0\n",
"for x in range(len(lines)):\n",
" line = lines[x].split(\" (contains \")[0]\n",
" for ing in line.split(\" \"):\n",
" if ing not in aller_dict.values():\n",
" total_ing += 1\n",
"total_ing"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values(['vhkj', 'dfzqlk', 'tvdvzd', 'smfz', 'lcb', 'lrqqqsg', 'qzlmr', 'shp'])"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"aller_dict.values()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 2"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'smfz,vhkj,qzlmr,tvdvzd,lcb,lrqqqsg,dfzqlk,shp,'"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"answer_list = ''\n",
"for i in sorted (aller_dict.keys()) : \n",
" answer_list = answer_list+(aller_dict.get(i))+',' \n",
"answer_list"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"[smfz,vhkj,qzlmr,tvdvzd,lcb,lrqqqsg,dfzqlk,shp]"
]
}
],
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit 2ce32dc

Please sign in to comment.