Skip to content

Commit 44cd8c6

Browse files
Add day 13.
1 parent 165682a commit 44cd8c6

File tree

1 file changed

+80
-3
lines changed

1 file changed

+80
-3
lines changed

2022/advent_of_code_2022.ipynb

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,10 @@
268268
"\n",
269269
" stacks_str.reverse()\n",
270270
" for line in stacks_str:\n",
271-
" for stack_id in range(len(line)):\n",
272-
" if(line[stack_id] == '.'):\n",
271+
" for stack_id,item in enumerate(line):\n",
272+
" if(item == '.'):\n",
273273
" continue\n",
274-
" stacks[stack_id].append(line[stack_id])\n",
274+
" stacks[stack_id].append(item)\n",
275275
" return stacks\n",
276276
" \n",
277277
" def parse_operations(operations_str):\n",
@@ -935,6 +935,83 @@
935935
" num_steps = len(shortest_path)-1\n",
936936
"num_steps"
937937
]
938+
},
939+
{
940+
"attachments": {},
941+
"cell_type": "markdown",
942+
"metadata": {},
943+
"source": [
944+
"## Day 13: Distress Signal\n",
945+
"\n",
946+
"What is the sum of the indices of those pairs?"
947+
]
948+
},
949+
{
950+
"cell_type": "code",
951+
"execution_count": null,
952+
"metadata": {},
953+
"outputs": [],
954+
"source": [
955+
"def to_list(value):\n",
956+
" return value if type(value) == list else [value]\n",
957+
"\n",
958+
"def check_list_order(left, right):\n",
959+
" if not left and not right:\n",
960+
" return 0\n",
961+
" if not left:\n",
962+
" return -1\n",
963+
" if not right:\n",
964+
" return 1\n",
965+
" if check_order(left[0], right[0]):\n",
966+
" return check_order(left[0], right[0])\n",
967+
" return check_order(left[1:], right[1:])\n",
968+
"\n",
969+
"def check_order(left, right):\n",
970+
" if (type(left), type(right)) == (int, int):\n",
971+
" return left - right\n",
972+
" return check_list_order(to_list(left), to_list(right))\n",
973+
"\n",
974+
"lines = open('13_input.txt', 'r').read().split('\\n\\n')\n",
975+
"pairs = [[eval(pair_str) for pair_str in line.split()] for line in lines]\n",
976+
"\n",
977+
"idx_of_pairs_in_right_order = []\n",
978+
"for pair_idx, (left, right) in enumerate(pairs, start=1):\n",
979+
" if check_order(left, right) < 0:\n",
980+
" idx_of_pairs_in_right_order.append(pair_idx)\n",
981+
"\n",
982+
"sum(idx_of_pairs_in_right_order)"
983+
]
984+
},
985+
{
986+
"attachments": {},
987+
"cell_type": "markdown",
988+
"metadata": {},
989+
"source": [
990+
"What is the decoder key for the distress signal?"
991+
]
992+
},
993+
{
994+
"cell_type": "code",
995+
"execution_count": null,
996+
"metadata": {},
997+
"outputs": [],
998+
"source": [
999+
"from functools import cmp_to_key, reduce\n",
1000+
"from operator import mul\n",
1001+
"\n",
1002+
"def flatten(list_of_lists):\n",
1003+
" '''Flatten one level of nesting in a list of lists.'''\n",
1004+
" return [item for sublist in list_of_lists for item in sublist]\n",
1005+
"\n",
1006+
"divider_packets = [[[2]], [[6]]]\n",
1007+
"\n",
1008+
"pairs = flatten(pairs)\n",
1009+
"pairs.extend(divider_packets)\n",
1010+
"pairs = sorted(pairs, key=cmp_to_key(check_order))\n",
1011+
"\n",
1012+
"divider_packet_idx = [pairs.index(packet)+1 for packet in divider_packets]\n",
1013+
"reduce(mul, divider_packet_idx)"
1014+
]
9381015
}
9391016
],
9401017
"metadata": {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy