Skip to content

Instantly share code, notes, and snippets.

@jrjames83
Created December 10, 2018 19:32
Show Gist options
  • Save jrjames83/bfdf6c7b57b4b5fec9ead580ebfffc7e to your computer and use it in GitHub Desktop.
Save jrjames83/bfdf6c7b57b4b5fec9ead580ebfffc7e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4920\n"
]
}
],
"source": [
"from collections import Counter\n",
"\n",
"twos = 0\n",
"threes = 0\n",
"\n",
"with open('../aocday2.txt') as f:\n",
" for line in f.readlines():\n",
" row_counter = Counter(list(line.strip()))\n",
" row_counter = {k:v for k,v in row_counter.items() if v > 1}\n",
" if 2 in row_counter.values(): twos +=1\n",
" if 3 in row_counter.values(): threes +=1\n",
" \n",
"print(twos * threes)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# p2 \n",
"\n",
"# The IDs abcde and axcye are close, \n",
"# but they differ by two characters (the second and fourth). \n",
"# However, the IDs fghij and fguij differ by exactly one character, \n",
"# the third (h and u). Those must be the correct boxes.\n",
"\n",
"# Find rows which differ by a single character\n",
"\n",
"\n",
"# Use a Class to read in the file and return the data rows\n",
"class AOCDay2Reader:\n",
" def __init__(self, filepath):\n",
" self.filepath = filepath\n",
"\n",
" @property\n",
" def data(self):\n",
" with open(self.filepath, 'r') as f:\n",
" # Just a hunch\n",
" return sorted([x.strip() for x in f.readlines()])\n",
" \n",
" def __len__(self):\n",
" return len(self.data)\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"250\n",
"['aonbsmjyquwwapeczikchtvdxl', 'conbsmjyqtwrapeczikggtvdxl', 'conbsmjyquwrapeczikahtvdxz', 'conbsxjxquwrapeczikghtvdxl', 'donbsmjyquqrapeczikghtadxl']\n"
]
},
{
"data": {
"text/plain": [
"Counter({26: 250})"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r = AOCDay2Reader('../aocday2.txt')\n",
"print(len(r)) # __len__\n",
"print(r.data[:5]) # quick check\n",
"Counter([len(x) for x in r.data]) # all 26 chars long"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"22759it [00:00, 113771.65it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"fonbwmjyquwtapeyzikghtvdxl\n",
"fonbwmjzquwtapeyzikghtvdxl\n",
"7\n",
"fonbwmjquwtapeyzikghtvdxl\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"from itertools import combinations\n",
"import editdistance # https://github.com/aflc/editdistance\n",
"from tqdm import tqdm\n",
"\n",
"pairs = combinations(r.data, 2)\n",
"\n",
"for p1, p2 in tqdm(pairs):\n",
" if editdistance.eval(p1, p2) == 1:\n",
" break\n",
" \n",
"# Common Letters between p1 and p2 and once they differ, find the index of the diff to remove it later\n",
"mirrors = list(zip(p1, p2))\n",
"for index, el in enumerate(mirrors):\n",
" if el[0] != el[1]:\n",
" remove_index = index\n",
" \n",
"print(p1)\n",
"print(p2)\n",
"print(remove_index)\n",
"print(\"\".join([p1[:remove_index],p1[remove_index + 1:]]))# Solution, slicing around the remove_index"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment