|
268 | 268 | "\n",
|
269 | 269 | " stacks_str.reverse()\n",
|
270 | 270 | " 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", |
273 | 273 | " continue\n",
|
274 |
| - " stacks[stack_id].append(line[stack_id])\n", |
| 274 | + " stacks[stack_id].append(item)\n", |
275 | 275 | " return stacks\n",
|
276 | 276 | " \n",
|
277 | 277 | " def parse_operations(operations_str):\n",
|
|
935 | 935 | " num_steps = len(shortest_path)-1\n",
|
936 | 936 | "num_steps"
|
937 | 937 | ]
|
| 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 | + ] |
938 | 1015 | }
|
939 | 1016 | ],
|
940 | 1017 | "metadata": {
|
|
0 commit comments