AdventOfCode/Python/day6/solution.ipynb

152 lines
3.5 KiB
Text

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Get puzzle input from input file\n",
"\n",
"puzzle_input = open(\"input\").read()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def toggle(val: bool) -> bool:\n",
" return not val\n",
"\n",
"def turn_off(val: bool) -> bool:\n",
" return False\n",
"\n",
"def turn_on(val: bool) -> bool:\n",
" return True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Part 1 #"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"400410\n"
]
}
],
"source": [
"lights = [[False]*1000 for i in range(1000)]\n",
"for command in puzzle_input.split(\"\\n\"):\n",
" if re.search(\"toggle\", command):\n",
" method = toggle\n",
" elif re.search(\"on\", command):\n",
" method = turn_on\n",
" else:\n",
" method = turn_off\n",
" \n",
" points = re.search(r\"(\\d+,\\d+) through (\\d+,\\d+)\", command)\n",
" point_a = tuple(map(int, points.group(1).split(\",\")))\n",
" point_b = tuple(map(int, points.group(2).split(\",\")))\n",
" \n",
" for a in range(point_a[0], point_b[0]+1):\n",
" for b in range(point_a[1], point_b[1]+1):\n",
" lights[a][b] = method(lights[a][b])\n",
"\n",
"print(list(item for sublist in lights for item in sublist).count(True))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Part 2 #"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15343601\n"
]
}
],
"source": [
"def toggle(val: bool) -> bool:\n",
" return val+2\n",
"\n",
"def turn_off(val: bool) -> bool:\n",
" return max(val-1, 0)\n",
"\n",
"def turn_on(val: bool) -> bool:\n",
" return val+1\n",
"\n",
"lights = [[0]*1000 for i in range(1000)]\n",
"for command in puzzle_input.split(\"\\n\"):\n",
" if re.search(\"toggle\", command):\n",
" method = toggle\n",
" elif re.search(\"on\", command):\n",
" method = turn_on\n",
" else:\n",
" method = turn_off\n",
" \n",
" points = re.search(r\"(\\d+,\\d+) through (\\d+,\\d+)\", command)\n",
" point_a = tuple(map(int, points.group(1).split(\",\")))\n",
" point_b = tuple(map(int, points.group(2).split(\",\")))\n",
" \n",
" for a in range(point_a[0], point_b[0]+1):\n",
" for b in range(point_a[1], point_b[1]+1):\n",
" lights[a][b] = method(lights[a][b])\n",
"\n",
"print(sum(list(item for sublist in lights for item in sublist)))"
]
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}