{"id":18793231,"url":"https://github.com/alexandramartinez/adventofcode-2022","last_synced_at":"2026-03-19T06:25:05.994Z","repository":{"id":152755641,"uuid":"573064594","full_name":"alexandramartinez/adventofcode-2022","owner":"alexandramartinez","description":"DataWeave scripts used in the adventofcode.com site for 2022.","archived":false,"fork":false,"pushed_at":"2024-11-12T22:01:18.000Z","size":94,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-21T15:54:25.308Z","etag":null,"topics":["advent-of-code","advent-of-code-2022","adventofcode","challenge","challenges","dataweave","dataweave-lang","dataweave-language","dataweave-playground","programming","programming-challenges","programming-exercises","programming-language","programming-languages"],"latest_commit_sha":null,"homepage":"https://adventofcode.com/2022","language":"DataWeave","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexandramartinez.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-12-01T16:11:29.000Z","updated_at":"2024-11-12T22:01:21.000Z","dependencies_parsed_at":"2023-12-05T16:42:09.877Z","dependency_job_id":"7f6895cd-92d1-4491-b483-d286d598ee59","html_url":"https://github.com/alexandramartinez/adventofcode-2022","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alexandramartinez/adventofcode-2022","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexandramartinez%2Fadventofcode-2022","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexandramartinez%2Fadventofcode-2022/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexandramartinez%2Fadventofcode-2022/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexandramartinez%2Fadventofcode-2022/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexandramartinez","download_url":"https://codeload.github.com/alexandramartinez/adventofcode-2022/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexandramartinez%2Fadventofcode-2022/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260294453,"owners_count":22987622,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["advent-of-code","advent-of-code-2022","adventofcode","challenge","challenges","dataweave","dataweave-lang","dataweave-language","dataweave-playground","programming","programming-challenges","programming-exercises","programming-language","programming-languages"],"created_at":"2024-11-07T21:24:04.794Z","updated_at":"2026-03-02T14:39:16.622Z","avatar_url":"https://github.com/alexandramartinez.png","language":"DataWeave","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Advent of Code 2022\n\nDataWeave scripts used in the [adventofcode.com](https://adventofcode.com/) site for 2022.\n\n## Similar repos\n\n[![](https://github-readme-stats.vercel.app/api/pin/?username=alexandramartinez\u0026repo=adventofcode-2023\u0026theme=neon)](https://github.com/alexandramartinez/adventofcode-2023)\n[![](https://github-readme-stats.vercel.app/api/pin/?username=alexandramartinez\u0026repo=adventofcode-2015\u0026theme=neon)](https://github.com/alexandramartinez/adventofcode-2015)\n\n## Scripts\n\n### Day 1\n\nThis script was created thanks to [Pavan Vara Prasad Mamidi](https://www.linkedin.com/in/pavan-mamidi/) during [this live stream](https://www.twitch.tv/videos/1667481264).\n\n\u003ca href=\"https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo\u0026repo=alexandramartinez%2Fadventofcode-2022\u0026path=scripts%2Fday1\"\u003e\u003cimg width=\"300\" src=\"/images/dwplayground-button.png\"\u003e\u003ca\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eScript\u003c/summary\u003e\n\n```dataweave\n%dw 2.0\noutput application/json\n---\n(payload splitBy \"\\n\\n\")\nmap ((item,index) -\u003e do {\n    var newItem = item splitBy \"\\n\"\n    ---\n    sum(newItem) as Number\n})\n//then max($) //Part 1\n//Part 2 below\norderBy $\nthen $[-1 to -3]\nthen sum($)\n```\n\u003c/details\u003e\n\n### Day 2\n\nThis script was created during [this live stream](https://www.twitch.tv/videos/1673658599).\n\n\u003ca href=\"https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo\u0026repo=alexandramartinez%2Fadventofcode-2022\u0026path=scripts%2Fday2\"\u003e\u003cimg width=\"300\" src=\"/images/dwplayground-button.png\"\u003e\u003ca\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eScript\u003c/summary\u003e\n\n```dataweave\n%dw 2.0\noutput application/json\n/** Part 1\nvar rules = {\n    A: { //R\n        X: 3 + 1, //R\n        Y: 6 + 2, //P\n        Z: 0 + 3 // S\n    },\n    B: { //P\n        X: 0 + 1, //R\n        Y: 3 + 2, //P\n        Z: 6 + 3 // S\n    },\n    C: { //S\n        X: 6 + 1, //R\n        Y: 0 + 2, //P\n        Z: 3 + 3 // S\n    }\n}*/\n\n// Part 2\nvar rules = {\n    A: { //R\n        X: 0 + 3, //L S\n        Y: 3 + 1, //D R\n        Z: 6 + 2 // W P\n    },\n    B: { //P\n        X: 0 + 1, //L R\n        Y: 3 + 2, //D P\n        Z: 6 + 3 // W S\n    },\n    C: { //S\n        X: 0 + 2, //L P\n        Y: 3 + 3, //D S\n        Z: 6 + 1 // W R\n    }\n}\n---\npayload splitBy \"\\n\"\nreduce ((round, score=0) -\u003e do {\n    var arr = round splitBy \" \"\n    var opponent = arr[0]\n    var me = arr[-1]\n    ---\n    score + (rules[opponent][me] default 0)\n})\n```\n\u003c/details\u003e\n\n### Day 3\n\nThis script was created during the following live streams:\n1. [Happy new year!! First stream of 2023 ✨ | Advent of Code day 3](https://www.twitch.tv/videos/1710480386)\n2. [Advent of Code day 3 with DataWeave...FINALIZED!](https://youtu.be/pQNRoY7hJKM)\n\n\u003ca href=\"https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo\u0026repo=alexandramartinez%2Fadventofcode-2022\u0026path=scripts%2Fday3\"\u003e\u003cimg width=\"300\" src=\"/images/dwplayground-button.png\"\u003e\u003ca\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eScript\u003c/summary\u003e\n\n```dataweave\n%dw 2.0\noutput application/json \nimport divideBy, firstWith, indexOf from dw::core::Arrays\nvar values = \"-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\" splitBy \"\"\nfun findChars(first, second) = (\n    (first map (firstItem) -\u003e do {\n        second firstWith ($ contains firstItem)\n    }) \n    filter $ != null \n    distinctBy $\n)\n---\npayload splitBy \"\\n\"\ndivideBy 3 // PART 2 ONLY\nmap do {\n    // PART 1\n    /*\n    var middle = sizeOf($) / 2\n    var first = $[0 to middle-1] splitBy \"\"\n    var second = $[middle to -1] splitBy \"\"\n    ---\n    findChars(first, second) \n    */\n    // PART 2\n    var first = $[0] splitBy \"\"\n    var second = $[1] splitBy \"\"\n    var third = $[2] splitBy \"\"\n    ---\n    findChars(first, second)\n    then findChars($, third)\n}\nthen flatten($)\nthen $ map do {\n    values indexOf $\n}\nthen sum($)\n```\n\u003c/details\u003e\n\n### Day 4\n\nThis script was created during the following live stream:\n1. [Advent of Code 2022 day 4 -- with DataWeave!](https://youtu.be/MTM5J3Qn_aM)\n\n\u003ca href=\"https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo\u0026repo=alexandramartinez%2Fadventofcode-2022\u0026path=scripts%2Fday4\"\u003e\u003cimg width=\"300\" src=\"/images/dwplayground-button.png\"\u003e\u003ca\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eScript\u003c/summary\u003e\n\n```dataweave\n%dw 2.0\noutput application/json \nimport countBy, some from dw::core::Arrays\nfun getRange(sections) = do {\n    var arr = sections splitBy \"-\"\n    var range = arr[0] to arr[1]\n    ---\n    // PART 1\n    //\" $(range joinBy \" , \") \"\n    // PART 2\n    range\n}\n---\npayload splitBy \"\\n\"\nmap do {\n    var pairs = $ splitBy \",\"\n    var first = getRange(pairs[0])\n    var second = getRange(pairs[1])\n    ---\n    // PART 1\n    //(first contains second) or (second contains first)\n    // PART 2\n    (first map (firstItem) -\u003e (\n        second some $ == firstItem\n    )) some $\n} \ncountBy $\n```\n\u003c/details\u003e\n\n### Day 5\n\nThis script was created during the following live streams:\n1. [Still doing Advent of Code '22 day 5 with DataWeave!](https://www.twitch.tv/videos/1712147286)\n2. [Advent of Code 2022 days 5.2 and 6! ✨](https://youtu.be/bWfUth6pvpI)\n\n\u003ca href=\"https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo\u0026repo=alexandramartinez%2Fadventofcode-2022\u0026path=scripts%2Fday5\"\u003e\u003cimg width=\"300\" src=\"/images/dwplayground-button.png\"\u003e\u003ca\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eScript\u003c/summary\u003e\n\n```dataweave\n%dw 2.0\noutput application/json \nimport isUpperCase from dw::core::Strings\nimport take, drop from dw::core::Arrays\nimport update from dw::util::Values\n\nvar instructions = (payload splitBy \"\\n\\n\")[1] splitBy \"\\n\"\n    then $ map do {\n        var i = flatten($ scan /\\d+/)\n        ---\n        {\n            crates: i[0] as Number,\n            from: i[1]-1,\n            to: i[2]-1\n        }\n    }\nvar crates = (payload splitBy \"\\n\\n\")[0] splitBy \"\\n\"\n\nfun getStuff(crates, result=[]) = do {\n    var r = crates map (\n            $[0 to 2] filter isUpperCase($)\n        ) filter !isEmpty($)\n        //then $[-1 to 0]\n    var nc = crates map $[4 to -1]\n    var newr = result + r\n    ---\n    if (nc[0] == null) newr\n    else getStuff(nc,newr)\n}\n\nfun move(instructions, crates) = do {\n    @Lazy\n    var i = instructions[0]\n    @Lazy\n    var cratesToTake = crates[i.from] take i.crates\n    @Lazy\n    var newColumnFrom = crates[i.from] drop i.crates\n    @Lazy\n    // PART 1\n    // var newColumnTo = cratesToTake[-1 to 0] ++ crates[i.to]\n    // PART 2\n    var newColumnTo = cratesToTake ++ crates[i.to]\n    @Lazy\n    var newCrates = crates update i.from with newColumnFrom\n        then $ update i.to with newColumnTo\n    ---\n    if (isEmpty(instructions)) crates\n    else move(instructions drop 1, newCrates)\n}\n---\nmove(instructions, getStuff(crates))\nmap ($[0]) \njoinBy \"\"\n```\n\u003c/details\u003e\n\n### Day 6\n\nThis script was created during the following live stream:\n1. [Advent of Code 2022 days 5.2 and 6! ✨](https://youtu.be/bWfUth6pvpI)\n\n\u003ca href=\"https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo\u0026repo=alexandramartinez%2Fadventofcode-2022\u0026path=scripts%2Fday6\"\u003e\u003cimg width=\"300\" src=\"/images/dwplayground-button.png\"\u003e\u003ca\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eScript\u003c/summary\u003e\n\n```dataweave\n%dw 2.0\nimport mapString, someCharacter from dw::core::Strings\noutput application/json\nvar numberOfMarkers = 14 // PART 1 = 4\nfun findThing(str) = do {\n    var toEvaluate = str[0 to numberOfMarkers-1] splitBy \"\"\n    var repeated = sizeOf(toEvaluate distinctBy $) \u003c numberOfMarkers\n    ---\n    if (repeated) findThing(str[1 to -1])\n    else if (toEvaluate == null) -1\n    else toEvaluate\n}\n---\nfindThing(payload) \njoinBy \"\"\nthen indexOf(payload, $) + numberOfMarkers\n```\n\u003c/details\u003e\n\n### Day 7 - FAILED\n\nEnded up taking [Felix's solution](https://github.com/Shadow-Devil/aoc2022) :(\n\nHere are the live streams to watch me fail 😂:\n1. [Advent of Code 2022 day 7 😭 (Part 1) with DataWeave](https://youtu.be/FpG6s2deJrg)\n2. [Advent of Code 2022 day 7 🧐 (Part 2) with DataWeave](https://youtu.be/n3902h-sAF0)\n3. [Advent of Code 2022 day 7 🥹 (Part 3) #fail with DataWeave](https://youtu.be/TPLgA1iSV7I)\n\n### Day 8\n\nThis script was created during the following live streams:\n1. [Advent of Code 2022 day 8 ✨ Part 1](https://youtu.be/FpqoYBBsodU)\n2. [Advent of Code 2022 day 8.2 and 9.1 with DataWeave ✨](https://youtu.be/ADiU4LjvX3U)\n\n\u003e [!NOTE]\n\u003e The code might not work in the Playground due to a timeout, but it will work from Visual Studio Code.\n\u003c/br\u003e\n\n\u003ca href=\"https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo\u0026repo=alexandramartinez%2Fadventofcode-2022\u0026path=scripts%2Fday8\"\u003e\u003cimg width=\"300\" src=\"/images/dwplayground-button.png\"\u003e\u003ca\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eScript\u003c/summary\u003e\n\n```dataweave\n%dw 2.0\nimport countBy, every from dw::core::Arrays\nimport lines, everyCharacter from dw::core::Strings\noutput application/json\nvar linesarr = lines(payload)\nfun countTrees(thisTree:String, nextTrees:String, count=0):Number = do {\n    if (isEmpty(nextTrees)) count\n    else if (nextTrees[0] \u003c thisTree) countTrees(thisTree, nextTrees[1 to -1], count+1)\n    else count+1\n}\n---\nflatten\n(linesarr map (line, y) -\u003e do {\n    var edge = sizeOf(line) - 1\n    ---\n    (line splitBy \"\") map (tree, x) -\u003e do {\n        // PART 1\n        /*\n        var edges:Boolean = (x == 0 or y == 0 or x == edge or y == edge)\n        var left:Boolean = (line[0 to (x-1)] everyCharacter ($ \u003c tree))\n        var right:Boolean = (line[(x+1) to -1] everyCharacter ($ \u003c tree))\n        var top:Boolean = (linesarr[0 to y-1] every $[x] \u003c tree)\n        var bottom:Boolean = (linesarr[(y+1) to -1] every $[x] \u003c tree)\n        ---\n        edges or left or right or top or bottom\n        */\n\n        // PART 2\n        var left:Number = if (x==0) 1\n            else tree countTrees line[(x-1) to 0]\n        var right:Number = if (x==edge) 1\n            else tree countTrees line[(x+1) to -1]\n        var top:Number = if (y==0) 1\n            else tree countTrees (linesarr[0 to y-1] reduce ((str, a='') -\u003e str[x] ++ a))\n        var bottom:Number = if (y==edge) 1\n            else tree countTrees (linesarr[(y+1) to -1] reduce ((str, a='') -\u003e a ++ str[x]))\n        ---\n        left * right * top * bottom\n\n        // {\n        //     linesarr: 0,\n        //     line: line,\n        //     y: y,\n        //     tree: tree,\n        //     x: x\n        // }\n    }\n}) \n// PART 1\n//countBy $\n// PART 2\nthen max($)\n```\n\u003c/details\u003e\n\n### Day 9\n\nThis script was created during the following live streams:\n1. [Advent of Code 2022 day 8.2 and 9.1 with DataWeave ✨](https://youtu.be/ADiU4LjvX3U)\n2. [Advent of Code 2022 day 9 ✨ Part 2](https://youtu.be/UDGiwoIXn8Y)\n\n\u003ca href=\"https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo\u0026repo=alexandramartinez%2Fadventofcode-2022\u0026path=scripts%2Fday9\"\u003e\u003cimg width=\"300\" src=\"/images/dwplayground-button.png\"\u003e\u003ca\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eScript\u003c/summary\u003e\n\n```dataweave\n%dw 2.0\nimport lines, repeat from dw::core::Strings\n\noutput application/json\ntype Point = { // or knot\n    x:Number,\n    y:Number\n}\ntype Direction = 'R' | 'L' | 'U' | 'D'\ntype Rope = Array\u003cPoint\u003e\nvar sPoint:Point = {x:0,y:0}\nvar rope = (0 to 9) as Array reduce (item, acc=[]) -\u003e acc + sPoint\nfun isNear(point1:Point, point2:Point) = do {\n    var xDistance = abs(point1.x - point2.x)\n    var yDistance = abs(point1.y - point2.y)\n    ---\n    (xDistance \u003c= 1) and (yDistance \u003c= 1)\n}\nfun whichDirections(point1:Point, point2:Point): Array | String = do {\n    var xDistance = point1.x - point2.x\n    var yDistance = point1.y - point2.y\n    var isDiagonal = point1.x != point2.x and point1.y != point2.y\n    ---\n    if (isDiagonal) ([] +\n        (if (xDistance \u003e= 1) 'R'\n        else if (xDistance \u003c= -1) 'L'\n        else '')\n        +\n        (if (yDistance \u003e= 1) 'U'\n        else if (yDistance \u003c= -1) 'D'\n        else '')\n    )\n    else (\n        if (xDistance \u003e 1) 'R'\n        else if (xDistance \u003c -1) 'L'\n        else if (yDistance \u003e 1) 'U'\n        else if (yDistance \u003c -1) 'D'\n        else ''\n    )\n}\nfun move(point:Point, direction:Direction): Point = \n    direction match {\n\t\tcase \"D\" -\u003e {\n\t\t\tx: point.x,\n\t\t\ty: point.y - 1\n\t\t}\n\t\tcase \"U\" -\u003e {\n\t\t\tx: point.x,\n\t\t\ty: point.y + 1\n\t\t}\n\t\tcase \"L\" -\u003e {\n\t\t\tx: point.x - 1,\n\t\t\ty: point.y\n\t\t}\n\t\tcase \"R\" -\u003e {\n\t\t\tx: point.x + 1,\n\t\t\ty: point.y\n\t\t}\n\t\telse -\u003e point\n\t}\nfun moveRestOfRope(head:Point, rope:Rope, index=0) = do {\n    var thisPoint:Point = rope[index]\n    @Lazy\n    var wd = log(whichDirections(head, thisPoint))\n    @Lazy\n    var newPoint = wd match {\n        case is String -\u003e thisPoint move wd // single move\n        case is Array -\u003e (thisPoint move wd[0]) move wd[1] // 2 moves - diagonal\n        else -\u003e thisPoint\n    }\n    ---\n    if (isEmpty(thisPoint)) rope\n    else if (head isNear thisPoint) rope\n    else moveRestOfRope(\n        newPoint,\n        rope update {\n            case [index] -\u003e newPoint\n        },\n        index + 1\n    )\n}\nfun getTailPoints(directions:String, rope=rope, tailPoints=[sPoint]) = do {\n    var direction:Direction = directions[0]\n    var head:Point = rope[0]\n    @Lazy\n    var newHead = log(\"head moves\",head move direction)\n    @Lazy\n    var newRope = newHead \u003e\u003e moveRestOfRope(newHead, rope[1 to -1])\n    ---\n    if (isEmpty(directions)) tailPoints\n    else getTailPoints(\n        directions[1 to -1],\n        newRope,\n        tailPoints + newRope[-1]\n    )\n}\n---\n(lines(payload) map (line) -\u003e do {\n    var splits = line splitBy \" \"\n    var d = splits[0]\n    var howmany = splits[1]\n    ---\n    d repeat howmany\n}) joinBy \"\"\nthen getTailPoints($) distinctBy $\nthen sizeOf($)\n```\n\u003c/details\u003e\n\n## Other repos\n\n- Clayton Flesher's [AdventOfCode2022](https://github.com/claytonflesher/AdventOfCode2022/tree/main/src/main/resources/dwl)\n- Ryan Hoegg's [adventofcode2022](https://github.com/rhoegg/adventofcode2022)\n- Felix Schnabel's [aoc2022](https://github.com/Shadow-Devil/aoc2022)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexandramartinez%2Fadventofcode-2022","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexandramartinez%2Fadventofcode-2022","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexandramartinez%2Fadventofcode-2022/lists"}