Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexandramartinez/adventofcode-2024
DataWeave scripts used in the adventofcode.com site for 2024.
https://github.com/alexandramartinez/adventofcode-2024
advent-of-code advent-of-code-2024 advent-of-code-2024-dataweave adventofcode adventofcode2024 challenge challenges dataweave dataweave-lang dataweave-language dataweave-playground programming programming-challenges programming-exercises programming-language programming-languages
Last synced: 26 days ago
JSON representation
DataWeave scripts used in the adventofcode.com site for 2024.
- Host: GitHub
- URL: https://github.com/alexandramartinez/adventofcode-2024
- Owner: alexandramartinez
- Created: 2024-12-01T13:58:28.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2024-12-01T14:48:47.000Z (about 1 month ago)
- Last Synced: 2024-12-01T15:19:23.403Z (about 1 month ago)
- Topics: advent-of-code, advent-of-code-2024, advent-of-code-2024-dataweave, adventofcode, adventofcode2024, challenge, challenges, dataweave, dataweave-lang, dataweave-language, dataweave-playground, programming, programming-challenges, programming-exercises, programming-language, programming-languages
- Language: DataWeave
- Homepage: https://adventofcode.com/2024
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Advent of Code 2024
DataWeave scripts used in the [adventofcode.com](https://adventofcode.com/) site for 2024.
> [!TIP]
> Check out [Ryan's private leaderboard](https://adventofcode.com/2024/leaderboard/private/view/1739830)!### Other Muleys doing DataWeave
- [Ryan Hoegg](https://github.com/rhoegg/adventofcode2024)
- [Pranav Davar](https://github.com/pranav-davar/advent-of-code-2024-dw)
- [Matthias Transier](https://github.com/mtransier/AdventOfCode2024)## 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
input payload application/csv separator=" ", header=false
output application/json
var a = payload.column_0 orderBy $
var b = payload.column_3 orderBy $
---
(0 to sizeOf(a)-1) map (abs(a[$] - b[$])) then sum($)
```### Part 2
Script
```dataweave
import countBy from dw::core::Arrays
var p = read(payload, "application/csv", {header:false,separator:" "})
var a = p.column_0
var b = p.column_3
---
a map ((item) ->
(b countBy ($==item)) * item
) then sum($)
```## 🔹 Day 2
### Part 1
Script
```dataweave
import every, countBy from dw::core::Arrays
var decreasing = "decreasing"
var increasing = "increasing"
---
((payload splitBy "\n") map (
(($ splitBy " ") reduce ((number, a=[]) ->
if (isEmpty(a)) a+{
prevNum: number,
operation: null,
isSafe: true
} else (a[-1].operation match {
case null -> a+{
prevNum: number,
operation: if (a[-1].prevNum-number > 0) decreasing else increasing,
isSafe: [1, 2, 3] contains abs(a[-1].prevNum - number)
}
case "$(decreasing)" -> a+{
prevNum: number,
operation: a[-1].operation,
isSafe: [1, 2, 3] contains (a[-1].prevNum - number)
}
else -> a+{
prevNum: number,
operation: a[-1].operation,
isSafe: [1, 2, 3] contains (number - a[-1].prevNum)
}
})
)).isSafe every $
)) countBy $
```### Part 2
Horrible code. But I did what I had to do :(
Script
```dataweave
import every, countBy from dw::core::Arrays
var decreasing = "decreasing"
var increasing = "increasing"
var newp = ((payload splitBy "\n") map (
(($ splitBy " ") reduce ((number, a=[]) ->
if (isEmpty(a)) a+{
prevNum: number,
operation: null,
isSafe: true
} else (a[-1].operation match {
case null -> a+{
prevNum: number,
operation: if (a[-1].prevNum-number > 0) decreasing else increasing,
isSafe: [1, 2, 3] contains abs(a[-1].prevNum - number)
}
case "$(decreasing)" -> a+{
prevNum: number,
operation: a[-1].operation,
isSafe: [1, 2, 3] contains (a[-1].prevNum - number)
}
else -> a+{
prevNum: number,
operation: a[-1].operation,
isSafe: [1, 2, 3] contains (number - a[-1].prevNum)
}
})
))
))
var safeOnes = newp filter ((item) -> item.isSafe every $)
var unsafeOnes = newp -- safeOnes
fun getScenarios(data) = data map ($ reduce ((number, a=[]) ->
if (isEmpty(a)) a+{
prevNum: number,
operation: null,
isSafe: true
} else (a[-1].operation match {
case null -> a+{
prevNum: number,
operation: if (a[-1].prevNum-number > 0) decreasing else increasing,
isSafe: [1, 2, 3] contains abs(a[-1].prevNum - number)
}
case "$(decreasing)" -> a+{
prevNum: number,
operation: a[-1].operation,
isSafe: [1, 2, 3] contains (a[-1].prevNum - number)
}
else -> a+{
prevNum: number,
operation: a[-1].operation,
isSafe: [1, 2, 3] contains (number - a[-1].prevNum)
}
})
))
---
unsafeOnes map ((firstArray) ->
getScenarios(firstArray.prevNum map ((number, numIndex) ->
firstArray.prevNum filter ((item, index) -> index != numIndex)
)) filter ((item) -> item.isSafe every $)
) filter (!isEmpty($))
then sizeOf($)+sizeOf(safeOnes)
```## 🔹 Day 3
### Part 1
Script
```dataweave
(flatten(payload scan /mul\(\d+,\d+\)/)) map do {
var nums = flatten($ scan /\d+/)
---
nums[0] * nums[1]
} then sum($)
```### Part 2
Script
```dataweave
(payload scan /(mul|don't|do)\(\d*,?\d*\)/) reduce ((op, a={r:0,"do":true}) ->
op[0][0 to 2] match {
case "mul" -> do {
var nums = flatten(op[0] scan /\d+/)
var newR = a.r + ((nums[0] default 0) * (nums[1] default 0))
---
{
r: if (a."do") newR else a.r,
"do": a."do"
}
}
case "don" -> { r: a.r, "do": false }
else -> { r: a.r, "do": true }
}
) then $.r
```## 🔹 Day 4
### Part 1
Script
```dataweave
var lines = payload splitBy "\n"
var XMAS = "XMAS"
fun getLetter(x:Number,y:Number) = if ((x<0) or (y<0)) "" else (lines[x][y] default "")
fun getStr(str:String,x:Number,y:Number) = if ((x<0) or (y<0)) "" else (str[x to y])
---
flatten
(lines map ((lineStr, lineidx) ->
(lineStr splitBy "") map ((letter, letteridx) ->
if (letter=="X") (
// right
(if (XMAS == getStr(lineStr,letteridx,letteridx+3)) 1 else 0)
// left
+ (if (XMAS == getStr(lineStr,letteridx,letteridx-3)) 1 else 0)
// down
+ (if (XMAS == (letter ++ getLetter(lineidx+1,letteridx) ++ getLetter(lineidx+2,letteridx) ++ getLetter(lineidx+3,letteridx))) 1 else 0)
// // up
+ (if (XMAS == (letter ++ getLetter(lineidx-1,letteridx) ++ getLetter(lineidx-2,letteridx) ++ getLetter(lineidx-3,letteridx))) 1 else 0)
// down-right
+ (if (XMAS == (letter ++ getLetter(lineidx+1,letteridx+1) ++ getLetter(lineidx+2,letteridx+2) ++ getLetter(lineidx+3,letteridx+3))) 1 else 0)
// down-left
+ (if (XMAS == (letter ++ getLetter(lineidx+1,letteridx-1) ++ getLetter(lineidx+2,letteridx-2) ++ getLetter(lineidx+3,letteridx-3))) 1 else 0)
// up-right
+ (if (XMAS == (letter ++ getLetter(lineidx-1,letteridx+1) ++ getLetter(lineidx-2,letteridx+2) ++ getLetter(lineidx-3,letteridx+3))) 1 else 0)
// up-left
+ (if (XMAS == (letter ++ getLetter(lineidx-1,letteridx-1) ++ getLetter(lineidx-2,letteridx-2) ++ getLetter(lineidx-3,letteridx-3))) 1 else 0)
)
else 0
)
)) then sum($)
```### Part 2
Script
```dataweave
var lines = payload splitBy "\n"
fun getLetter(x:Number,y:Number) = if ((x<0) or (y<0)) "" else (lines[x][y] default "")
var xmas = ["MAS", "SAM"]
---
flatten
(lines map ((lineStr, lineidx) ->
(lineStr splitBy "") map ((letter, letteridx) ->
if (letter=="A") do {
var topleft = getLetter(lineidx-1, letteridx-1)
var topright = getLetter(lineidx-1, letteridx+1)
var bottomleft = getLetter(lineidx+1, letteridx-1)
var bottomright = getLetter(lineidx+1, letteridx+1)
var cross1 = topleft ++ letter ++ bottomright
var cross2 = topright ++ letter ++ bottomleft
---
if ( (xmas contains cross1) and (xmas contains cross2) ) 1 else 0
}
else 0
)
)) then sum($)
```