Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alexandramartinez/adventofcode-2015

DataWeave scripts used in the adventofcode.com site for 2015.
https://github.com/alexandramartinez/adventofcode-2015

advent-of-code adventofcode challenge challenges dataweave dataweave-lang dataweave-language dataweave-playground programming programming-challenges programming-exercises programming-language programming-languages

Last synced: about 1 month ago
JSON representation

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

Awesome Lists containing this project

README

        

# Advent of Code 2015

DataWeave scripts used in the [adventofcode.com](https://adventofcode.com/) site for 2015.

## Similar repos

[![](https://github-readme-stats.vercel.app/api/pin/?username=alexandramartinez&repo=adventofcode-2023&theme=neon)](https://github.com/alexandramartinez/adventofcode-2023)
[![](https://github-readme-stats.vercel.app/api/pin/?username=alexandramartinez&repo=adventofcode-2022&theme=neon)](https://github.com/alexandramartinez/adventofcode-2022)

## 🔹 Day 1

### Part 1

Script

```dataweave
%dw 2.0
---
payload
replace "(" with "1,"
replace ")" with "-1,"
splitBy ","
map $ as Number
then sum($)
```

### Part 2

Script

```dataweave
%dw 2.0
fun getResult(data, index=0, sum=0) =
if (!isEmpty(data)) do {
var newSum = data[0] as Number + sum
var currentIndex = index+1
---
if (newSum == -1)
currentIndex
else
getResult(data[1 to -1], currentIndex, newSum)
}
else index
---
payload
replace "(" with "1,"
replace ")" with "-1,"
splitBy ","
then getResult($)
```

## 🔹 Day 2

### Part 1

Script

```dataweave
%dw 2.0
fun getSingleResult(data) = do {
var d = data splitBy "x" map $ as Number orderBy $
var slack = d[0] * d[1]
---
(2*d[0]*d[1]) + (2*d[1]*d[2]) + (2*d[2]*d[0]) + slack
}
---
sum(payload splitBy "\n" map getSingleResult($))
```

### Part 2

Script

```dataweave
%dw 2.0
fun getSingleResult(data) = do {
var d = data splitBy "x" map $ as Number orderBy $
var ribbon = d[0] + d[0] + d[1] + d[1]
---
d[0]*d[1]*d[2] + ribbon
}
---
sum(payload splitBy "\n" map getSingleResult($))
```

## 🔹 Day 3

### Part 1

Script

```dataweave
%dw 2.0
type House = { x:Number, y:Number }
var west = "<" // left
var east = ">" // right
var north = "^" // up
var south = "v" // down
var start:House = { x:0, y:0 }
fun move(direction:String, house:House):House =
direction match {
case d if d == west -> { x: house.x-1, y: house.y }
case d if d == east -> { x: house.x+1, y: house.y }
case d if d == north -> { x: house.x, y: house.y+1 }
case d if d == south -> { x: house.x, y: house.y-1 }
else -> house
}
---
payload splitBy "" reduce ((direction, houses=[start]) ->
houses + move(direction, houses[-1])
) distinctBy $
then sizeOf ($)
```

### Part 2

Script

```dataweave
%dw 2.0
type House = { x:Number, y:Number }
var west = "<" // left
var east = ">" // right
var north = "^" // up
var south = "v" // down
var start:House = { x:0, y:0 }
fun move(direction:String, house:House):House =
direction match {
case d if d == west -> { x: house.x-1, y: house.y }
case d if d == east -> { x: house.x+1, y: house.y }
case d if d == north -> { x: house.x, y: house.y+1 }
case d if d == south -> { x: house.x, y: house.y-1 }
else -> house
}
fun getHouses(directions:Array):Array =
directions reduce ((direction, houses=[start]) ->
houses + move(direction, houses[-1])
)
var p = payload splitBy ""
var svrs = [(p filter (($$ mod 2) == 0)), (p filter (($$ mod 2) != 0))]
var santa = getHouses(svrs[0])
var robosanta = getHouses(svrs[1])
---
santa ++ robosanta
distinctBy $
then sizeOf ($)
```

## 🔹 Day 4

> [!NOTE]
> None of these scripts will run from the DW Playground. Part 1 can be run from vscode but Part 2 needs to run from the CLI.

### Part 1

Script

```dataweave
%dw 2.0
import MD5 from dw::Crypto
var secretKey = "yzbqklnj"
fun getCorrectHash(number:Number) =
if (MD5((secretKey ++ number as String) as Binary) startsWith "00000")
number
else getCorrectHash(number+1)
---
getCorrectHash(0)
```

### Part 2

> [!NOTE]
> I had to run this from the [DataWeave CLI](https://github.com/mulesoft-labs/data-weave-cli) because vscode was timing out. After you install it, you can run it with `dw run -f MyModule.dwl`

Script

```dataweave
%dw 2.0
import MD5 from dw::Crypto
var secretKey = "yzbqklnj"
fun getCorrectHash(number:Number) =
if (MD5((secretKey ++ number as String) as Binary) startsWith "000000")
number
else getCorrectHash(number+1)
---
getCorrectHash(0)
```

## 🔹 Day 5

### Part 1

Script

```dataweave
%dw 2.0
import countBy from dw::core::Arrays
---
payload splitBy "\n" map do {
var vowels = sizeOf(flatten($ scan /[aeiou]/))
var doubleLetters = sizeOf(flatten($ scan /(.)\1/))
var notThese = sizeOf(flatten($ scan /ab|cd|pq|xy/))
---
(notThese == 0) and (doubleLetters >= 2) and (vowels >= 3)
} countBy $
```

### Part 2

Script

```dataweave
%dw 2.0
import countMatches from dw::core::Strings
import countBy, some from dw::core::Arrays
---
payload splitBy "\n" map ((line) -> do {
var letterPairs = (line splitBy "" map ((letter, letterIndex) ->
line[letterIndex to letterIndex+1]
))[0 to -2] map (line countMatches $) some ($>=2)
var otherThing = sizeOf(flatten(line scan /(.).\1/)) >= 1
---
letterPairs and otherThing
}) countBy $
```

## 🔹 Day 6

### Part 1

Ok, I tried several approaches here and I didn't want to just discard the ones that didn't work, so I'm keeping them here.

- **[Attempt 1 - tailrec](/scripts/day6/part1/attempt1.dwl)** - For some reason this script was not processed as Tail Recursive??? I'm pretty sure it is. But ohwell. Had to discard the whole thing bc the payload is 300 lines (more than 255) so I was getting the `StackOverflow` error.
- **[Attempt 2 - reduce + mapleafvalues](/scripts/day6/part1/attempt2.dwl)** - This one works perfectly fine when testing with a smaller matrix. But since the challenge is a 0,999 matrix, this solution times out.

Script

```dataweave

```