Tweaks on 10 an 11

This commit is contained in:
Tabledevil
2024-12-17 21:47:48 +01:00
parent af2ea4b6dc
commit 733be845de
2 changed files with 141 additions and 463 deletions

View File

@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 52, "execution_count": 11,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -13,7 +13,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 53, "execution_count": 12,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -28,449 +28,46 @@
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": 54,
"metadata": {}, "metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 11),\n",
" (0, 18),\n",
" (0, 32),\n",
" (0, 34),\n",
" (0, 41),\n",
" (0, 45),\n",
" (0, 49),\n",
" (1, 2),\n",
" (1, 13),\n",
" (1, 27),\n",
" (1, 52),\n",
" (2, 0),\n",
" (2, 5),\n",
" (2, 17),\n",
" (2, 22),\n",
" (2, 26),\n",
" (2, 37),\n",
" (2, 40),\n",
" (3, 9),\n",
" (3, 21),\n",
" (3, 30),\n",
" (3, 46),\n",
" (3, 53),\n",
" (3, 55),\n",
" (3, 57),\n",
" (4, 1),\n",
" (4, 16),\n",
" (4, 36),\n",
" (4, 42),\n",
" (4, 44),\n",
" (4, 48),\n",
" (5, 2),\n",
" (5, 12),\n",
" (5, 15),\n",
" (5, 25),\n",
" (5, 33),\n",
" (5, 36),\n",
" (5, 43),\n",
" (6, 9),\n",
" (6, 10),\n",
" (6, 15),\n",
" (6, 37),\n",
" (7, 4),\n",
" (7, 11),\n",
" (7, 25),\n",
" (7, 28),\n",
" (7, 59),\n",
" (8, 18),\n",
" (8, 20),\n",
" (8, 39),\n",
" (8, 46),\n",
" (9, 27),\n",
" (9, 44),\n",
" (9, 47),\n",
" (9, 50),\n",
" (9, 52),\n",
" (9, 59),\n",
" (10, 3),\n",
" (10, 14),\n",
" (10, 31),\n",
" (10, 32),\n",
" (10, 36),\n",
" (11, 1),\n",
" (11, 6),\n",
" (11, 14),\n",
" (11, 42),\n",
" (11, 55),\n",
" (12, 1),\n",
" (12, 31),\n",
" (12, 35),\n",
" (12, 59),\n",
" (13, 8),\n",
" (13, 10),\n",
" (13, 12),\n",
" (13, 17),\n",
" (13, 24),\n",
" (13, 28),\n",
" (13, 34),\n",
" (14, 21),\n",
" (14, 27),\n",
" (14, 28),\n",
" (14, 39),\n",
" (14, 52),\n",
" (14, 57),\n",
" (15, 5),\n",
" (15, 29),\n",
" (15, 57),\n",
" (16, 0),\n",
" (16, 25),\n",
" (16, 33),\n",
" (16, 38),\n",
" (16, 41),\n",
" (16, 48),\n",
" (16, 55),\n",
" (17, 7),\n",
" (17, 16),\n",
" (17, 25),\n",
" (17, 53),\n",
" (18, 1),\n",
" (18, 9),\n",
" (18, 36),\n",
" (18, 41),\n",
" (19, 12),\n",
" (19, 16),\n",
" (19, 20),\n",
" (19, 22),\n",
" (19, 28),\n",
" (19, 42),\n",
" (19, 47),\n",
" (19, 51),\n",
" (20, 2),\n",
" (20, 10),\n",
" (20, 24),\n",
" (20, 33),\n",
" (20, 45),\n",
" (20, 47),\n",
" (21, 14),\n",
" (21, 23),\n",
" (21, 29),\n",
" (21, 33),\n",
" (21, 42),\n",
" (21, 52),\n",
" (22, 2),\n",
" (22, 44),\n",
" (22, 55),\n",
" (23, 14),\n",
" (23, 34),\n",
" (23, 35),\n",
" (23, 55),\n",
" (24, 4),\n",
" (24, 7),\n",
" (24, 13),\n",
" (24, 24),\n",
" (24, 25),\n",
" (24, 27),\n",
" (24, 38),\n",
" (24, 42),\n",
" (24, 45),\n",
" (25, 16),\n",
" (25, 20),\n",
" (25, 22),\n",
" (25, 31),\n",
" (25, 36),\n",
" (25, 53),\n",
" (26, 5),\n",
" (26, 11),\n",
" (26, 20),\n",
" (26, 24),\n",
" (26, 50),\n",
" (26, 53),\n",
" (27, 2),\n",
" (27, 4),\n",
" (27, 16),\n",
" (27, 37),\n",
" (27, 47),\n",
" (27, 49),\n",
" (27, 58),\n",
" (28, 7),\n",
" (28, 33),\n",
" (28, 39),\n",
" (28, 45),\n",
" (28, 56),\n",
" (29, 10),\n",
" (29, 26),\n",
" (29, 32),\n",
" (29, 50),\n",
" (29, 54),\n",
" (30, 4),\n",
" (30, 22),\n",
" (30, 37),\n",
" (31, 17),\n",
" (31, 25),\n",
" (31, 26),\n",
" (31, 32),\n",
" (31, 37),\n",
" (31, 40),\n",
" (31, 47),\n",
" (32, 5),\n",
" (32, 13),\n",
" (32, 24),\n",
" (32, 36),\n",
" (32, 41),\n",
" (32, 46),\n",
" (32, 53),\n",
" (32, 59),\n",
" (33, 10),\n",
" (33, 23),\n",
" (33, 28),\n",
" (33, 34),\n",
" (33, 38),\n",
" (33, 42),\n",
" (33, 50),\n",
" (34, 28),\n",
" (34, 51),\n",
" (35, 4),\n",
" (35, 16),\n",
" (35, 46),\n",
" (35, 58),\n",
" (36, 4),\n",
" (36, 20),\n",
" (36, 55),\n",
" (37, 0),\n",
" (37, 1),\n",
" (37, 16),\n",
" (37, 18),\n",
" (37, 26),\n",
" (37, 34),\n",
" (37, 44),\n",
" (37, 56),\n",
" (38, 4),\n",
" (38, 13),\n",
" (38, 22),\n",
" (38, 32),\n",
" (38, 40),\n",
" (38, 45),\n",
" (39, 12),\n",
" (39, 17),\n",
" (39, 27),\n",
" (39, 30),\n",
" (39, 41),\n",
" (39, 43),\n",
" (39, 58),\n",
" (40, 1),\n",
" (40, 11),\n",
" (40, 22),\n",
" (40, 33),\n",
" (40, 37),\n",
" (40, 50),\n",
" (40, 53),\n",
" (41, 0),\n",
" (41, 5),\n",
" (41, 27),\n",
" (41, 45),\n",
" (41, 53),\n",
" (41, 55),\n",
" (42, 2),\n",
" (42, 9),\n",
" (42, 20),\n",
" (42, 21),\n",
" (42, 24),\n",
" (42, 36),\n",
" (42, 38),\n",
" (42, 42),\n",
" (43, 2),\n",
" (43, 6),\n",
" (43, 8),\n",
" (43, 25),\n",
" (44, 22),\n",
" (44, 23),\n",
" (44, 26),\n",
" (44, 28),\n",
" (44, 34),\n",
" (44, 37),\n",
" (44, 39),\n",
" (44, 42),\n",
" (44, 46),\n",
" (44, 47),\n",
" (44, 49),\n",
" (44, 59),\n",
" (45, 9),\n",
" (45, 10),\n",
" (45, 17),\n",
" (45, 31),\n",
" (45, 59),\n",
" (46, 18),\n",
" (46, 31),\n",
" (46, 33),\n",
" (46, 54),\n",
" (47, 1),\n",
" (47, 5),\n",
" (47, 10),\n",
" (48, 19),\n",
" (48, 23),\n",
" (48, 25),\n",
" (48, 35),\n",
" (48, 38),\n",
" (48, 51),\n",
" (49, 7),\n",
" (49, 20),\n",
" (49, 35),\n",
" (49, 50),\n",
" (50, 10),\n",
" (50, 20),\n",
" (50, 26),\n",
" (50, 42),\n",
" (50, 45),\n",
" (50, 55),\n",
" (51, 4),\n",
" (51, 6),\n",
" (51, 7),\n",
" (51, 10),\n",
" (51, 15),\n",
" (51, 19),\n",
" (51, 51),\n",
" (51, 58),\n",
" (52, 28),\n",
" (52, 32),\n",
" (52, 42),\n",
" (53, 15),\n",
" (53, 45),\n",
" (53, 59),\n",
" (54, 1),\n",
" (54, 7),\n",
" (54, 13),\n",
" (54, 19),\n",
" (54, 23),\n",
" (54, 27),\n",
" (54, 28),\n",
" (54, 35),\n",
" (54, 38),\n",
" (54, 43),\n",
" (54, 48),\n",
" (54, 51),\n",
" (55, 4),\n",
" (55, 26),\n",
" (55, 28),\n",
" (55, 57),\n",
" (56, 7),\n",
" (56, 11),\n",
" (56, 38),\n",
" (57, 14),\n",
" (57, 17),\n",
" (57, 18),\n",
" (57, 36),\n",
" (57, 47),\n",
" (58, 11),\n",
" (58, 29),\n",
" (58, 31),\n",
" (58, 40),\n",
" (58, 45),\n",
" (58, 56),\n",
" (59, 3),\n",
" (59, 28),\n",
" (59, 37),\n",
" (59, 43),\n",
" (59, 50)]"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"find_zeros(data)" "Recursive Function to to check each direction and recursively call itself until it reaches the end of the path. If it reaches the end of the path, it returns the current position as a solution. If it doesn't reach the end of the path, it continues to check the next direction. The function takes in the current x and y coordinates, the data, and the current stage of the path as arguments. It yields each solution as it finds them.\n"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 55, "execution_count": 13,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"['987672345210988321089487678943210101985430123012901212349876',\n",
" '890581876347809451276596521058923432876543276529874301236765',\n",
" '081490932156918760345603430667898943109801983434565321245125',\n",
" '112387893045329654565012348766087652234712123403125430104034',\n",
" '201236794532134503456983289632128141045623010512036765295543',\n",
" '310145687621032012987674108543459031012654107696543894387654',\n",
" '498456546001221093474565432432569122101965298787645976578761',\n",
" '567307632120332387565478901201678233212874345676534589469450',\n",
" '569218986787454456010785832178876544589012321201225676354321',\n",
" '478123678796561234521896541069987235678987430390310101256910',\n",
" '312054569657320899634567892454100124017986543485436543267871',\n",
" '203765012348210798749656876363211345623477012576567654106565',\n",
" '104894343239345687658775965278901210786568943689658987265430',\n",
" '985787210101012210343189034107349809894329876756567456892121',\n",
" '276321895696523421221076121001256756765018549843498305743012',\n",
" '123450734789434322834565432890162125321567678732565214654012',\n",
" '043545643298545618965436901781878034450410589721056543204323',\n",
" '652634102187656709878927801652989876567323434874347890116454',\n",
" '701783214074327812567213452343567865018430128965236921327985',\n",
" '879898543265018903450102169603498984329543012340121023438876',\n",
" '930569856104345676543221078712321075655432121050123214589565',\n",
" '321478987645430189876330789610165034746583043269874303678456',\n",
" '430326786554321234565445698543278129837895650178965452102365',\n",
" '545415898545210765676324582344569100121298782348766567801476',\n",
" '696504305676905894889213001053213256780367091059654321945987',\n",
" '787413218789876103990102128967104345091452187768345670236896',\n",
" '898310129650145212850167034878545432187873677851210980167345',\n",
" '790101212341232101763258949889436782106924578960102398798201',\n",
" '889212303216987232654345656776521093345014467017681432120132',\n",
" '974322454307896342363214780345810894214323454178590541098743',\n",
" '865401965416787651654303291236989765201412963289487672347652',\n",
" '765432876545619650189432100387870652106507875670343781656761',\n",
" '765410987814308761276541087498761543017890124501289890967890',\n",
" '898321678903216654365650196501252102120987033215670767856981',\n",
" '456912565214327612984787767102343256761856144256781056743234',\n",
" '367803454323438703673298898234358543892349856105892343212101',\n",
" '219804456554549654560143567895569212876548761234987454101234',\n",
" '008512367567632103067654410766978103975432810125676655670543',\n",
" '127601898458903452198103328957860198786901912089985765781632',\n",
" '234534567321012560198712367046043245697850105678789854398701',\n",
" '103421673450523871237601452132154032106543214543210710239678',\n",
" '011210982567658964321546543012965124321432343058988720106549',\n",
" '320125671098576545690037012129876765010541012167349634218932',\n",
" '430134040123489836789128903456745895432672123453234545347451',\n",
" '549232132123498721654100190109832106701089098500104456956320',\n",
" '678943433001567890193289283254108987892128987612245437875410',\n",
" '217654456712346521089374374363201076985434376543336521056587',\n",
" '103450349809454434678765465478912345876548985965447899867898',\n",
" '212761212778765894501250104567656210230123476876534038769876',\n",
" '429843203989987765410343215698567340145696545123410125612345',\n",
" '343456117801256784320354210785458756968787034014567234501654',\n",
" '652107006901343098901235341812329647879674123456798012612703',\n",
" '567898215414332187612356756901410138984565012343898543563812',\n",
" '238987312323278076543349867812567221345694321032187623438910',\n",
" '109813408954109189801210789003498210210782107153098018542123',\n",
" '216701567867898276764325650101567367823473498544567129656034',\n",
" '345652101210567345123434543234989456910565567639875678798945',\n",
" '456543458325430410089545696545678321045876543320564549567876',\n",
" '434567869450321523679656787230109878236905452011254235650165',\n",
" '321018978761234334578765698101234569107812301232340123543234']"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"data" "def recursive_check(x,y,data,stage):\n",
" # foreach direction check if within bounds\n",
" for x,y in [(x,y) for x,y in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)] if x>=0 and x < len(data) and y>=0 and y < len(data[0])]:\n",
" # if in bounds and on correct hight\n",
" if int(data[x][y]) == stage:\n",
" # if on last stage return solution\n",
" if stage == 9:\n",
" yield (x,y) \n",
" else: \n",
" # return all solutions from next stage\n",
" for solution in recursive_check(x,y,data,stage+1):\n",
" yield solution\n",
" else: \n",
" continue"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"10a" "### Solution for 10 a\n",
"\n",
"counting the number of unique peaks for each zero (start)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 58, "execution_count": 14,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@@ -482,39 +79,26 @@
} }
], ],
"source": [ "source": [
"def recursive_check(x,y,data,stage):\n",
" # foreach direction check if within bounds\n",
" for x,y in [(x,y) for x,y in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)] if x>=0 and x < len(data) and y>=0 and y < len(data[0])]:\n",
" if int(data[x][y]) == stage:\n",
" if stage == 9:\n",
" yield (x,y)\n",
" else:\n",
" for solution in recursive_check(x,y,data,stage+1):\n",
" yield solution\n",
" else: \n",
" continue\n",
"\n",
"\n",
"summ = 0\n", "summ = 0\n",
"for zero in find_zeros(data):\n", "for zero in find_zeros(data):\n",
" peaks = set(recursive_check(zero[0],zero[1],data,1))\n", " peaks = set(recursive_check(zero[0],zero[1],data,1))\n",
" summ += len(peaks) \n", " summ += len(peaks) \n",
"\n", "\n",
"print(summ)\n", "print(summ)\n"
" \n",
" \n"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# 10b" "### Solution for 10 b\n",
"\n",
"counting the number of unique paths to a peak (9) for each zero (start)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 59, "execution_count": 15,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@@ -526,33 +110,18 @@
} }
], ],
"source": [ "source": [
"def recursive_check(x,y,data,stage):\n",
" # foreach direction check if within bounds\n",
" for x,y in [(x,y) for x,y in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)] if x>=0 and x < len(data) and y>=0 and y < len(data[0])]:\n",
" if int(data[x][y]) == stage:\n",
" if stage == 9:\n",
" yield (x,y)\n",
" else:\n",
" for solution in recursive_check(x,y,data,stage+1):\n",
" yield solution\n",
" else: \n",
" continue\n",
"\n",
"\n",
"summ = 0\n", "summ = 0\n",
"for zero in find_zeros(data):\n", "for zero in find_zeros(data):\n",
" peaks = list(recursive_check(zero[0],zero[1],data,1))\n", " peaks = list(recursive_check(zero[0],zero[1],data,1))\n",
" summ += len(peaks) \n", " summ += len(peaks) \n",
"\n", "\n",
"print(summ)\n", "print(summ)"
" \n",
" \n"
] ]
} }
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "pdf", "display_name": "advent",
"language": "python", "language": "python",
"name": "python3" "name": "python3"
}, },
@@ -566,7 +135,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.12.7" "version": "3.13.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@@ -0,0 +1,109 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"#input = r'''0 1 10 99 999'''\n",
"#input = r'''125 17'''\n",
"input = r'''475449 2599064 213 0 2 65 5755 51149'''\n",
"\n",
"stones = [int(x) for x in input.split()]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"from functools import lru_cache\n",
"\n",
"@lru_cache(maxsize=None)\n",
"def step(stone,steps):\n",
" if steps == 1:\n",
" if stone == 0:\n",
" return 1\n",
" elif (tx_stone := str(stone)) and len(tx_stone) %2 == 0:\n",
" return 2\n",
" else:\n",
" return 1\n",
" else:\n",
" if stone == 0:\n",
" return step(1,steps-1)\n",
" elif (tx_stone := str(stone)) and len(tx_stone) %2 == 0:\n",
" half = len(tx_stone)//2\n",
" left = int(tx_stone[:half])\n",
" right = int(tx_stone[half:])\n",
" x_left = step(left,steps-1)\n",
" x_right = step(right,steps-1)\n",
" return x_left + x_right\n",
" else:\n",
" return(step(stone*2024,steps-1))\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"193269"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum([step(stone,25) for stone in stones])"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"228449040027793"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum([step(stone,75) for stone in stones])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "advent",
"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.13.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}