-
Notifications
You must be signed in to change notification settings - Fork 2
/
5.clj
43 lines (38 loc) · 7 KB
/
5.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(ns advent-of-code.2017.5
(:require [clojure.string :as cs]))
(def raw-input "1\n2\n-1\n-2\n1\n-3\n-1\n1\n0\n-4\n-8\n-7\n-2\n0\n-2\n-11\n1\n-2\n0\n0\n-11\n-17\n-18\n-1\n-12\n-21\n-15\n-24\n-8\n-5\n0\n-17\n-8\n-5\n-24\n-16\n-16\n-21\n-5\n-7\n-13\n-11\n-2\n-27\n-29\n-38\n-2\n2\n-27\n-10\n-9\n-32\n-3\n-1\n-6\n-50\n-21\n-47\n-47\n-16\n-48\n-19\n-53\n-25\n-57\n-42\n-64\n-21\n-59\n-3\n-51\n-66\n-44\n-42\n-45\n-6\n-18\n-28\n-18\n-48\n-21\n-15\n-4\n-10\n-49\n-72\n-56\n-47\n-41\n-74\n-38\n-60\n-28\n-10\n-32\n-1\n-9\n-40\n-10\n-6\n-58\n-92\n-8\n-94\n-99\n-93\n-33\n-31\n-84\n-28\n-39\n-105\n-23\n-76\n-35\n-71\n-100\n-102\n-29\n-86\n-70\n-30\n-8\n0\n-109\n1\n-22\n-24\n-92\n-21\n-103\n-127\n-67\n0\n-68\n-31\n-71\n-111\n-26\n-123\n-39\n-116\n-15\n-86\n-85\n-137\n-127\n-134\n-145\n-29\n-123\n-19\n-43\n-152\n-122\n-148\n-129\n-97\n-39\n-28\n-49\n-93\n-110\n-103\n-130\n1\n-114\n-146\n-99\n-128\n-118\n-32\n-48\n-115\n-155\n-26\n-37\n-65\n-48\n-71\n-6\n-137\n-178\n-111\n-139\n-127\n-160\n-172\n-98\n-38\n-156\n-11\n-62\n-187\n-53\n2\n-117\n-3\n-31\n-143\n-41\n-47\n-169\n-162\n-158\n-12\n-69\n-114\n-180\n-155\n-125\n-64\n-176\n-184\n-202\n-116\n-74\n-98\n-205\n-84\n-152\n-54\n-102\n-165\n-138\n-140\n-180\n-96\n-98\n-109\n-81\n-199\n-137\n-56\n-74\n-179\n-175\n-114\n-124\n-15\n-234\n-219\n-51\n-41\n-144\n-134\n-161\n-59\n-128\n-71\n-22\n-165\n-222\n-70\n-65\n-51\n-43\n-86\n-198\n-238\n-119\n-31\n-195\n-87\n-102\n-30\n-73\n-76\n-153\n-238\n-8\n-73\n-63\n-148\n-42\n-16\n-228\n-243\n-235\n-160\n-107\n-235\n-29\n-188\n-202\n-42\n-215\n-159\n-134\n-172\n-263\n-188\n-124\n-34\n-206\n-15\n-138\n-184\n-20\n-32\n-271\n-103\n-203\n-129\n-177\n-69\n-107\n-265\n-68\n-299\n-161\n-148\n-182\n0\n-207\n-106\n-68\n-92\n-53\n-52\n-288\n-3\n-211\n-143\n-204\n-126\n-152\n-106\n-232\n-153\n-234\n-62\n-124\n-131\n-42\n-297\n-332\n-188\n-115\n-100\n-173\n-52\n-115\n-296\n-301\n-312\n-292\n-2\n-321\n-178\n-174\n-244\n-309\n-161\n-346\n-251\n-157\n-325\n-292\n-159\n-95\n0\n-124\n-69\n-324\n-223\n-89\n-359\n-242\n-239\n1\n-39\n-204\n-287\n-142\n-123\n-363\n-218\n-197\n-136\n-20\n-304\n-281\n-83\n-7\n-129\n-315\n-76\n-349\n-141\n-318\n-369\n-346\n-161\n-141\n-110\n-279\n-5\n-86\n-348\n-59\n-255\n-266\n-355\n-110\n-14\n-339\n-109\n-44\n-38\n-10\n-164\n-214\n-265\n-412\n-72\n-413\n-271\n-343\n-124\n-352\n-304\n-124\n-381\n-258\n-8\n-235\n-288\n-27\n-296\n-179\n-392\n-336\n-255\n-114\n-15\n-407\n-296\n-29\n-352\n-419\n-190\n-308\n-2\n-430\n-157\n-379\n-220\n-179\n-77\n-337\n-61\n-48\n-64\n-197\n-408\n-284\n-84\n-409\n-243\n-316\n-77\n-77\n-428\n-432\n-182\n-437\n-254\n-50\n-260\n-301\n-28\n-33\n-335\n-348\n-240\n-287\n-436\n-225\n-221\n-198\n-190\n-50\n-87\n-161\n-408\n0\n-14\n-225\n-105\n-188\n-290\n-349\n-57\n-45\n-20\n-384\n-36\n-264\n-359\n-52\n-21\n-328\n-194\n-432\n-113\n-475\n-391\n-86\n-407\n-18\n-435\n-206\n-317\n-254\n-369\n-373\n-127\n-405\n-309\n-154\n-480\n-271\n-71\n-306\n-381\n-252\n-253\n-420\n-40\n-349\n-403\n-44\n-256\n-33\n-429\n-10\n-461\n-405\n-216\n-329\n-201\n-498\n-392\n-149\n-419\n-85\n-408\n-248\n-88\n-322\n-438\n-381\n-100\n-445\n-412\n-215\n-220\n-83\n-436\n-411\n-555\n-372\n-232\n-309\n-151\n-214\n-219\n-268\n-123\n-90\n-241\n-508\n-134\n-74\n-296\n-505\n-240\n-161\n-477\n-63\n-118\n-293\n-69\n-197\n-88\n-520\n-170\n-37\n-114\n-234\n-36\n-225\n-116\n-36\n-195\n-363\n-75\n-137\n-7\n-506\n-124\n-556\n-15\n-327\n-74\n-367\n-505\n-29\n-296\n-281\n-180\n-420\n-119\n-449\n-502\n-204\n-294\n-484\n-515\n-74\n-337\n-256\n-479\n-471\n-27\n-614\n-354\n-369\n-607\n-244\n-578\n-195\n-215\n-407\n-552\n-247\n-514\n-434\n-291\n-521\n-99\n-598\n-292\n-400\n-594\n-381\n-602\n-260\n-79\n-441\n-444\n-146\n-451\n-502\n-215\n-81\n-577\n-652\n-507\n-264\n-588\n-431\n-401\n-103\n-282\n-125\n-259\n-615\n-321\n-271\n-84\n-84\n-323\n-650\n-79\n-289\n-522\n-129\n-343\n-441\n-186\n-561\n-244\n-186\n-296\n-272\n-258\n-308\n-390\n-677\n-367\n-186\n-604\n-104\n-481\n-394\n-31\n-663\n-493\n-608\n-142\n-86\n-356\n-581\n-131\n-11\n-92\n-258\n-552\n-176\n-244\n-208\n-564\n-9\n-558\n-256\n-439\n-460\n-641\n-457\n-715\n-328\n-291\n-172\n-380\n-406\n0\n-123\n-286\n-301\n-375\n-358\n-607\n-599\n-670\n-94\n-143\n-65\n-201\n-486\n-394\n-405\n-671\n-673\n-564\n-137\n-200\n-148\n-644\n-589\n-643\n-155\n-714\n-602\n-54\n-746\n-403\n-520\n-446\n-646\n-680\n-474\n-431\n-762\n-712\n-554\n-187\n-242\n-242\n-595\n-66\n-610\n-378\n-430\n-595\n-485\n-467\n-434\n-663\n-375\n-81\n-503\n-688\n-651\n-17\n-10\n-184\n-361\n-165\n-785\n-61\n-211\n-140\n-740\n-126\n-549\n-222\n-611\n-557\n-786\n-525\n-431\n-111\n-287\n-131\n-574\n-212\n-733\n-223\n-734\n-275\n-524\n-295\n-541\n-240\n-162\n-750\n-350\n-486\n-672\n-579\n-410\n-737\n-544\n-728\n-516\n-163\n-227\n-249\n-177\n-522\n-363\n-190\n-613\n-148\n-810\n-593\n-702\n-545\n-187\n-27\n-332\n-611\n-510\n-214\n-56\n-219\n-696\n-593\n-720\n-479\n-155\n-278\n-517\n-691\n-314\n-638\n-748\n-232\n-737\n-46\n-138\n-192\n-631\n-224\n-691\n-628\n-613\n-324\n-185\n-365\n-259\n-219\n-462\n-290\n-783\n-710\n-444\n-271\n-117\n-469\n-609\n-105\n-602\n-465\n-260\n-323\n-544\n-493\n-458\n-261\n-102\n-198\n-221\n-321\n-694\n-614\n-147\n-511\n-592\n-335\n-738\n-198\n-274\n-780\n-598\n-281\n-686\n-25\n-682\n-827\n-491\n-312\n-540\n-304\n-293\n2\n-238\n-614\n-22\n-380\n-194\n-167\n-167\n-569\n-170\n-184\n-104\n-327\n-401\n-654\n-926\n-571\n-181\n-809\n-552\n-767\n-579\n-823\n-620\n-660\n-853\n-448\n-720\n-872\n-898\n-45\n-154\n-409\n-399\n-950\n-393\n-782\n-376\n-65\n-644\n-654\n-523\n-24\n-767\n-419\n-183\n-143\n-98\n-792\n-485\n-923\n-360\n-173\n-879\n-847\n-732\n-962\n-643\n-392\n-117\n-4\n-932\n-253\n-298\n-381\n-339\n-796\n-274\n-79\n-586\n-567\n-425\n-541\n-329\n-800\n-878\n-519\n-111\n-224\n-304\n-560\n-183\n-604\n-952\n-229\n2\n-115\n-748\n-262\n-54\n-533\n-139\n-785\n-583\n-634\n-164\n-836\n-77\n-578\n-593\n-561\n-596\n-611\n-440\n-27\n-848\n-998\n-56\n-947\n-740\n-737\n-612\n-655\n-845\n-812\n-925\n-197\n-236\n-37\n-753\n-747\n-286\n-641\n-43\n-348\n-33\n-713\n-610\n-777\n-899\n-1005\n-264\n-193\n-928\n-193\n-412\n-213\n-228\n-1012\n-920\n-702\n-420\n-496\n-1019\n-386\n-645\n-804\n-795\n-12\n-810\n-117\n-454\n-266\n-1059\n-321\n-674\n-647")
(def instructions (mapv #(Integer/parseInt %) (cs/split-lines raw-input)))
(def instructions-len (count instructions))
;; solve part one
(loop [instrs instructions
pos 0
count 0]
(if-not (< -1 pos instructions-len)
count
(recur (update instrs pos inc)
(+ pos (nth instrs pos))
(inc count))))
;; solve part two
(loop [instrs instructions
pos 0
count 0]
(if-not (< -1 pos instructions-len)
count
(let [offset (nth instrs pos)]
(recur (update instrs pos
(if (<= 3 offset) dec inc))
(+ pos offset)
(inc count)))))
;; or several times faster using transient instructions vector
(loop [instrs (transient instructions)
pos 0
count 0]
(if-not (< -1 pos instructions-len)
count
(let [offset (nth instrs pos)]
(recur (assoc! instrs pos
(if (<= 3 offset)
(dec offset)
(inc offset)))
(+ pos offset)
(inc count)))))