Skip to content

alexandramartinez/adventofcode-2022

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

Advent of Code 2022

DataWeave scripts used in the adventofcode.com site for 2022.

Similar repos

Scripts

Day 1

This script was created thanks to Pavan Vara Prasad Mamidi during this live stream.

Script
%dw 2.0
output application/json
---
(payload splitBy "\n\n")
map ((item,index) -> do {
    var newItem = item splitBy "\n"
    ---
    sum(newItem) as Number
})
//then max($) //Part 1
//Part 2 below
orderBy $
then $[-1 to -3]
then sum($)

Day 2

This script was created during this live stream.

Script
%dw 2.0
output application/json
/** Part 1
var rules = {
    A: { //R
        X: 3 + 1, //R
        Y: 6 + 2, //P
        Z: 0 + 3 // S
    },
    B: { //P
        X: 0 + 1, //R
        Y: 3 + 2, //P
        Z: 6 + 3 // S
    },
    C: { //S
        X: 6 + 1, //R
        Y: 0 + 2, //P
        Z: 3 + 3 // S
    }
}*/

// Part 2
var rules = {
    A: { //R
        X: 0 + 3, //L S
        Y: 3 + 1, //D R
        Z: 6 + 2 // W P
    },
    B: { //P
        X: 0 + 1, //L R
        Y: 3 + 2, //D P
        Z: 6 + 3 // W S
    },
    C: { //S
        X: 0 + 2, //L P
        Y: 3 + 3, //D S
        Z: 6 + 1 // W R
    }
}
---
payload splitBy "\n"
reduce ((round, score=0) -> do {
    var arr = round splitBy " "
    var opponent = arr[0]
    var me = arr[-1]
    ---
    score + (rules[opponent][me] default 0)
})

Day 3

This script was created during the following live streams:

  1. Happy new year!! First stream of 2023 ✨ | Advent of Code day 3
  2. Advent of Code day 3 with DataWeave...FINALIZED!

Script
%dw 2.0
output application/json 
import divideBy, firstWith, indexOf from dw::core::Arrays
var values = "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" splitBy ""
fun findChars(first, second) = (
    (first map (firstItem) -> do {
        second firstWith ($ contains firstItem)
    }) 
    filter $ != null 
    distinctBy $
)
---
payload splitBy "\n"
divideBy 3 // PART 2 ONLY
map do {
    // PART 1
    /*
    var middle = sizeOf($) / 2
    var first = $[0 to middle-1] splitBy ""
    var second = $[middle to -1] splitBy ""
    ---
    findChars(first, second) 
    */
    // PART 2
    var first = $[0] splitBy ""
    var second = $[1] splitBy ""
    var third = $[2] splitBy ""
    ---
    findChars(first, second)
    then findChars($, third)
}
then flatten($)
then $ map do {
    values indexOf $
}
then sum($)

Day 4

This script was created during the following live stream:

  1. Advent of Code 2022 day 4 -- with DataWeave!

Script
%dw 2.0
output application/json 
import countBy, some from dw::core::Arrays
fun getRange(sections) = do {
    var arr = sections splitBy "-"
    var range = arr[0] to arr[1]
    ---
    // PART 1
    //" $(range joinBy " , ") "
    // PART 2
    range
}
---
payload splitBy "\n"
map do {
    var pairs = $ splitBy ","
    var first = getRange(pairs[0])
    var second = getRange(pairs[1])
    ---
    // PART 1
    //(first contains second) or (second contains first)
    // PART 2
    (first map (firstItem) -> (
        second some $ == firstItem
    )) some $
} 
countBy $

Day 5

This script was created during the following live streams:

  1. Still doing Advent of Code '22 day 5 with DataWeave!
  2. Advent of Code 2022 days 5.2 and 6! ✨

Script
%dw 2.0
output application/json 
import isUpperCase from dw::core::Strings
import take, drop from dw::core::Arrays
import update from dw::util::Values

var instructions = (payload splitBy "\n\n")[1] splitBy "\n"
    then $ map do {
        var i = flatten($ scan /\d+/)
        ---
        {
            crates: i[0] as Number,
            from: i[1]-1,
            to: i[2]-1
        }
    }
var crates = (payload splitBy "\n\n")[0] splitBy "\n"

fun getStuff(crates, result=[]) = do {
    var r = crates map (
            $[0 to 2] filter isUpperCase($)
        ) filter !isEmpty($)
        //then $[-1 to 0]
    var nc = crates map $[4 to -1]
    var newr = result + r
    ---
    if (nc[0] == null) newr
    else getStuff(nc,newr)
}

fun move(instructions, crates) = do {
    @Lazy
    var i = instructions[0]
    @Lazy
    var cratesToTake = crates[i.from] take i.crates
    @Lazy
    var newColumnFrom = crates[i.from] drop i.crates
    @Lazy
    // PART 1
    // var newColumnTo = cratesToTake[-1 to 0] ++ crates[i.to]
    // PART 2
    var newColumnTo = cratesToTake ++ crates[i.to]
    @Lazy
    var newCrates = crates update i.from with newColumnFrom
        then $ update i.to with newColumnTo
    ---
    if (isEmpty(instructions)) crates
    else move(instructions drop 1, newCrates)
}
---
move(instructions, getStuff(crates))
map ($[0]) 
joinBy ""

Day 6

This script was created during the following live stream:

  1. Advent of Code 2022 days 5.2 and 6! ✨

Script
%dw 2.0
import mapString, someCharacter from dw::core::Strings
output application/json
var numberOfMarkers = 14 // PART 1 = 4
fun findThing(str) = do {
    var toEvaluate = str[0 to numberOfMarkers-1] splitBy ""
    var repeated = sizeOf(toEvaluate distinctBy $) < numberOfMarkers
    ---
    if (repeated) findThing(str[1 to -1])
    else if (toEvaluate == null) -1
    else toEvaluate
}
---
findThing(payload) 
joinBy ""
then indexOf(payload, $) + numberOfMarkers

Day 7 - FAILED

Ended up taking Felix's solution :(

Here are the live streams to watch me fail 😂:

  1. Advent of Code 2022 day 7 😭 (Part 1) with DataWeave
  2. Advent of Code 2022 day 7 🧐 (Part 2) with DataWeave
  3. Advent of Code 2022 day 7 🥹 (Part 3) #fail with DataWeave

Day 8

This script was created during the following live streams:

  1. Advent of Code 2022 day 8 ✨ Part 1
  2. Advent of Code 2022 day 8.2 and 9.1 with DataWeave ✨

Note

The code might not work in the Playground due to a timeout, but it will work from Visual Studio Code.


Script
%dw 2.0
import countBy, every from dw::core::Arrays
import lines, everyCharacter from dw::core::Strings
output application/json
var linesarr = lines(payload)
fun countTrees(thisTree:String, nextTrees:String, count=0):Number = do {
    if (isEmpty(nextTrees)) count
    else if (nextTrees[0] < thisTree) countTrees(thisTree, nextTrees[1 to -1], count+1)
    else count+1
}
---
flatten
(linesarr map (line, y) -> do {
    var edge = sizeOf(line) - 1
    ---
    (line splitBy "") map (tree, x) -> do {
        // PART 1
        /*
        var edges:Boolean = (x == 0 or y == 0 or x == edge or y == edge)
        var left:Boolean = (line[0 to (x-1)] everyCharacter ($ < tree))
        var right:Boolean = (line[(x+1) to -1] everyCharacter ($ < tree))
        var top:Boolean = (linesarr[0 to y-1] every $[x] < tree)
        var bottom:Boolean = (linesarr[(y+1) to -1] every $[x] < tree)
        ---
        edges or left or right or top or bottom
        */

        // PART 2
        var left:Number = if (x==0) 1
            else tree countTrees line[(x-1) to 0]
        var right:Number = if (x==edge) 1
            else tree countTrees line[(x+1) to -1]
        var top:Number = if (y==0) 1
            else tree countTrees (linesarr[0 to y-1] reduce ((str, a='') -> str[x] ++ a))
        var bottom:Number = if (y==edge) 1
            else tree countTrees (linesarr[(y+1) to -1] reduce ((str, a='') -> a ++ str[x]))
        ---
        left * right * top * bottom

        // {
        //     linesarr: 0,
        //     line: line,
        //     y: y,
        //     tree: tree,
        //     x: x
        // }
    }
}) 
// PART 1
//countBy $
// PART 2
then max($)

Day 9

This script was created during the following live streams:

  1. Advent of Code 2022 day 8.2 and 9.1 with DataWeave ✨
  2. Advent of Code 2022 day 9 ✨ Part 2

Script
%dw 2.0
import lines, repeat from dw::core::Strings

output application/json
type Point = { // or knot
    x:Number,
    y:Number
}
type Direction = 'R' | 'L' | 'U' | 'D'
type Rope = Array<Point>
var sPoint:Point = {x:0,y:0}
var rope = (0 to 9) as Array reduce (item, acc=[]) -> acc + sPoint
fun isNear(point1:Point, point2:Point) = do {
    var xDistance = abs(point1.x - point2.x)
    var yDistance = abs(point1.y - point2.y)
    ---
    (xDistance <= 1) and (yDistance <= 1)
}
fun whichDirections(point1:Point, point2:Point): Array | String = do {
    var xDistance = point1.x - point2.x
    var yDistance = point1.y - point2.y
    var isDiagonal = point1.x != point2.x and point1.y != point2.y
    ---
    if (isDiagonal) ([] +
        (if (xDistance >= 1) 'R'
        else if (xDistance <= -1) 'L'
        else '')
        +
        (if (yDistance >= 1) 'U'
        else if (yDistance <= -1) 'D'
        else '')
    )
    else (
        if (xDistance > 1) 'R'
        else if (xDistance < -1) 'L'
        else if (yDistance > 1) 'U'
        else if (yDistance < -1) 'D'
        else ''
    )
}
fun move(point:Point, direction:Direction): Point = 
    direction match {
		case "D" -> {
			x: point.x,
			y: point.y - 1
		}
		case "U" -> {
			x: point.x,
			y: point.y + 1
		}
		case "L" -> {
			x: point.x - 1,
			y: point.y
		}
		case "R" -> {
			x: point.x + 1,
			y: point.y
		}
		else -> point
	}
fun moveRestOfRope(head:Point, rope:Rope, index=0) = do {
    var thisPoint:Point = rope[index]
    @Lazy
    var wd = log(whichDirections(head, thisPoint))
    @Lazy
    var newPoint = wd match {
        case is String -> thisPoint move wd // single move
        case is Array -> (thisPoint move wd[0]) move wd[1] // 2 moves - diagonal
        else -> thisPoint
    }
    ---
    if (isEmpty(thisPoint)) rope
    else if (head isNear thisPoint) rope
    else moveRestOfRope(
        newPoint,
        rope update {
            case [index] -> newPoint
        },
        index + 1
    )
}
fun getTailPoints(directions:String, rope=rope, tailPoints=[sPoint]) = do {
    var direction:Direction = directions[0]
    var head:Point = rope[0]
    @Lazy
    var newHead = log("head moves",head move direction)
    @Lazy
    var newRope = newHead >> moveRestOfRope(newHead, rope[1 to -1])
    ---
    if (isEmpty(directions)) tailPoints
    else getTailPoints(
        directions[1 to -1],
        newRope,
        tailPoints + newRope[-1]
    )
}
---
(lines(payload) map (line) -> do {
    var splits = line splitBy " "
    var d = splits[0]
    var howmany = splits[1]
    ---
    d repeat howmany
}) joinBy ""
then getTailPoints($) distinctBy $
then sizeOf($)

Other repos