Skip to content

Commit

Permalink
Update examples to use new release
Browse files Browse the repository at this point in the history
  • Loading branch information
mulias committed Jan 2, 2024
1 parent c1671cf commit b790209
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 55 deletions.
4 changes: 3 additions & 1 deletion examples/aoc_2022/day_05/main.roc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ app "Advent2022Day05"
packages {
cli: "https://github.com/roc-lang/basic-cli/releases/download/0.6.2/c7T4Hp8bAdWz3r9ZrhboBzibCjJag8d0IP_ljb42yVc.tar.br",
parser: "https://github.com/lukewilliamboswell/roc-parser/releases/download/0.2.0/dJQSsSmorujhiPNIvJKlQoI92RFIG_JQwUfIxZsCSwE.tar.br",
array2d: "https://github.com/mulias/roc-array2d/releases/download/v0.1.0/ssMT0bDIv-qE7d_yNUyCByGQHvpNkQJZsGUS6xEFsIY.tar.br",
array2d: "https://github.com/mulias/roc-array2d/releases/download/v0.2.0/pmAttjSPjyubNa8XiVH9D3vsDMqHahq_yz81N_tt_UU.tar.br",
}
imports [
cli.Stdout,
cli.Task,
parser.Core.{ Parser, between, sepBy1, chompWhile, keep, skip, const, map, oneOf, buildPrimitiveParser, parsePartial, fail },
parser.String.{ RawStr, parseStr, string, codeunit, digits, anyCodeunit },
array2d.Array2D.{ Array2D },
array2d.Shape2D,
array2d.Index2D,
"example.txt" as exampleInput : Str,
]
provides [main] to cli
Expand Down
52 changes: 28 additions & 24 deletions examples/aoc_2022/day_08/main.roc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ app "Advent2022Day08"
packages {
cli: "https://github.com/roc-lang/basic-cli/releases/download/0.6.2/c7T4Hp8bAdWz3r9ZrhboBzibCjJag8d0IP_ljb42yVc.tar.br",
parser: "https://github.com/lukewilliamboswell/roc-parser/releases/download/0.2.0/dJQSsSmorujhiPNIvJKlQoI92RFIG_JQwUfIxZsCSwE.tar.br",
array2d: "https://github.com/mulias/roc-array2d/releases/download/v0.1.0/ssMT0bDIv-qE7d_yNUyCByGQHvpNkQJZsGUS6xEFsIY.tar.br",
array2d: "https://github.com/mulias/roc-array2d/releases/download/v0.2.0/pmAttjSPjyubNa8XiVH9D3vsDMqHahq_yz81N_tt_UU.tar.br",
}
imports [
cli.Stdout,
cli.Task,
parser.Core.{ Parser, map, between, chompWhile, sepBy1, oneOrMore },
parser.String.{ RawStr, parseStr, string, digit },
array2d.Array2D.{ Array2D },
array2d.Shape2D,
array2d.Index2D.{ Index2D },
"example.txt" as exampleInput : Str,
]
provides [main] to cli
Expand All @@ -37,12 +39,14 @@ HeightMap : Array2D Nat

VisibilityMap : Array2D Bool

TreeSightLines : { index : Array2D.Index, left : Nat, right : Nat, up : Nat, down : Nat }
TreeSightLines : { index : Index2D, left : Nat, right : Nat, up : Nat, down : Nat }

SightLinesMap : Array2D TreeSightLines

visibleTrees : HeightMap -> VisibilityMap
visibleTrees = \heightMap ->
mapShape = Array2D.shape heightMap

visibilityMap = Array2D.map heightMap \_elem -> Bool.false

startState = { visibilityMap, maxHeight: 0 }
Expand All @@ -58,7 +62,7 @@ visibleTrees = \heightMap ->
startState
{ direction: Forwards, orientation: Rows }
\state, treeHeight, index ->
if Array2D.isRowStart index || treeHeight > state.maxHeight then
if Index2D.isRowStart index || treeHeight > state.maxHeight then
setTreeVisible state treeHeight index
else
state
Expand All @@ -69,7 +73,7 @@ visibleTrees = \heightMap ->
withLeft
{ direction: Backwards, orientation: Rows }
\state, treeHeight, index ->
if Array2D.isRowEnd heightMap index || treeHeight > state.maxHeight then
if Index2D.isRowEnd index mapShape || treeHeight > state.maxHeight then
setTreeVisible state treeHeight index
else
state
Expand All @@ -80,7 +84,7 @@ visibleTrees = \heightMap ->
withRight
{ direction: Forwards, orientation: Cols }
\state, treeHeight, index ->
if Array2D.isColStart index || treeHeight > state.maxHeight then
if Index2D.isColStart index || treeHeight > state.maxHeight then
setTreeVisible state treeHeight index
else
state
Expand All @@ -91,7 +95,7 @@ visibleTrees = \heightMap ->
withTop
{ direction: Backwards, orientation: Cols }
\state, treeHeight, index ->
if Array2D.isColEnd heightMap index || treeHeight > state.maxHeight then
if Index2D.isColEnd index mapShape || treeHeight > state.maxHeight then
setTreeVisible state treeHeight index
else
state
Expand Down Expand Up @@ -124,7 +128,7 @@ treeSightLines = \heightMap ->
0
{ direction: Backwards, orientation: Rows, start: treeIndex }
\count, otherTree, otherIndex ->
if Array2D.isRowStart otherIndex then
if Index2D.isRowStart otherIndex then
Break count
else
visibleCount count otherTree otherIndex
Expand All @@ -135,7 +139,7 @@ treeSightLines = \heightMap ->
0
{ direction: Forwards, orientation: Rows, start: treeIndex }
\count, otherTree, otherIndex ->
if Array2D.isRowEnd heightMap otherIndex then
if Index2D.isRowEnd otherIndex (Array2D.shape heightMap) then
Break count
else
visibleCount count otherTree otherIndex
Expand All @@ -146,7 +150,7 @@ treeSightLines = \heightMap ->
0
{ direction: Backwards, orientation: Cols, start: treeIndex }
\count, otherTree, otherIndex ->
if Array2D.isColStart otherIndex then
if Index2D.isColStart otherIndex then
Break count
else
visibleCount count otherTree otherIndex
Expand All @@ -157,7 +161,7 @@ treeSightLines = \heightMap ->
0
{ direction: Forwards, orientation: Cols, start: treeIndex }
\count, otherTree, otherIndex ->
if Array2D.isColEnd heightMap otherIndex then
if Index2D.isColEnd otherIndex (Array2D.shape heightMap) then
Break count
else
visibleCount count otherTree otherIndex
Expand All @@ -168,10 +172,10 @@ mostScenicTree : SightLinesMap -> TreeSightLines
mostScenicTree = \trees ->
startState =
trees
|> Array2D.get { x: 0, y: 0 }
|> Array2D.get { row: 0, col: 0 }
|> orCrash "Unexpected empty array"

Array2D.walk trees startState { direction: Forwards, orientation: Rows, start: { x: 0, y: 0 } } \state, nextTree, _index ->
Array2D.walk trees startState { direction: Forwards } \state, nextTree, _index ->
if scenicScore state < scenicScore nextTree then
nextTree
else
Expand All @@ -182,24 +186,24 @@ scenicScore = \{ left, right, up, down } -> left * right * up * down

displayScenicTreeMap : HeightMap, TreeSightLines -> Str
displayScenicTreeMap = \heightMap, { index, right, left, up, down } ->
rightSegment = List.range { start: After index.y, end: Length right }
leftSegment = List.range { start: At (index.y - left), end: Length left }
upSegment = List.range { start: At (index.x - up), end: Length up }
downSegment = List.range { start: After index.x, end: Length down }
rightSegment = List.range { start: After index.col, end: Length right }
leftSegment = List.range { start: At (index.col - left), end: Length left }
upSegment = List.range { start: At (index.row - up), end: Length up }
downSegment = List.range { start: After index.row, end: Length down }

scenicTreeMap = Array2D.map heightMap Num.toStr

withRight = List.walk rightSegment scenicTreeMap \mapState, y ->
Array2D.set mapState { x: index.x, y } "."
withRight = List.walk rightSegment scenicTreeMap \mapState, col ->
Array2D.set mapState { row: index.row, col } "."

withLeft = List.walk leftSegment withRight \mapState, y ->
Array2D.set mapState { x: index.x, y } "."
withLeft = List.walk leftSegment withRight \mapState, col ->
Array2D.set mapState { row: index.row, col } "."

withUp = List.walk upSegment withLeft \mapState, x ->
Array2D.set mapState { x, y: index.y } "."
withUp = List.walk upSegment withLeft \mapState, row ->
Array2D.set mapState { row, col: index.col } "."

withAll = List.walk downSegment withUp \mapState, x ->
Array2D.set mapState { x, y: index.y } "."
withAll = List.walk downSegment withUp \mapState, row ->
Array2D.set mapState { row, col: index.col } "."

joinArrayWith withAll "" "\n"

Expand Down
62 changes: 32 additions & 30 deletions examples/aoc_2022/day_14/main.roc
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
app "Advent2022Day14"
packages {
cli: "https://github.com/roc-lang/basic-cli/releases/download/0.6.2/c7T4Hp8bAdWz3r9ZrhboBzibCjJag8d0IP_ljb42yVc.tar.br",
array2d: "https://github.com/mulias/roc-array2d/releases/download/v0.1.0/ssMT0bDIv-qE7d_yNUyCByGQHvpNkQJZsGUS6xEFsIY.tar.br",
array2d: "https://github.com/mulias/roc-array2d/releases/download/v0.2.0/pmAttjSPjyubNa8XiVH9D3vsDMqHahq_yz81N_tt_UU.tar.br",
}
imports [
cli.Stdout,
cli.Task,
array2d.Array2D.{ Array2D },
array2d.Shape2D,
array2d.Index2D.{ Index2D },
"example.txt" as exampleInput : Str,
]
provides [main] to cli

# Offset makes it easier to display the results
offsetY = 300
offsetCols = 300

caveDimensions = { dimX: 170, dimY: 700 - offsetY }
caveDimensions = { rows: 170, cols: 700 - offsetCols }

sandSource = { x: 0, y: 500 - offsetY }
sandSource = { row: 0, col: 500 - offsetCols }
exampleCave = initCave exampleInput

expect exampleCave |> addMaxSand |> countSand == 24
Expand All @@ -39,15 +41,15 @@ main =

Cave : Array2D [Air, Rock, Sand]

Path : List Array2D.Index
Path : List Index2D

addMaxSand : Cave -> Cave
addMaxSand = \cave ->
when addSand cave sandSource is
Ok sandyCave -> addMaxSand sandyCave
Err _ -> cave

addSand : Cave, Array2D.Index -> Result Cave [SandFellIntoTheInfiniteAbyss, Blocked]
addSand : Cave, Index2D -> Result Cave [SandFellIntoTheInfiniteAbyss, Blocked]
addSand = \cave, pos ->
when Array2D.get cave pos is
Err OutOfBounds -> Err SandFellIntoTheInfiniteAbyss
Expand All @@ -63,9 +65,9 @@ elseIfBlocked = \result, thunk ->
Err Blocked -> thunk {}
_ -> result

down = \{ x, y } -> { x: x + 1, y }
downLeft = \{ x, y } -> { x: x + 1, y: y - 1 }
downRight = \{ x, y } -> { x: x + 1, y: y + 1 }
down = \{ row, col } -> { row: row + 1, col }
downLeft = \{ row, col } -> { row: row + 1, col: col - 1 }
downRight = \{ row, col } -> { row: row + 1, col: col + 1 }

countSand : Cave -> Nat
countSand = \cave -> Array2D.countIf cave \material -> material == Sand
Expand All @@ -85,14 +87,14 @@ toPaths = \input ->
|> Str.split " -> "
|> List.map toPoint

toPoint : Str -> Array2D.Index
toPoint : Str -> Index2D
toPoint = \input ->
when Str.split input "," is
[strY, strX] ->
x = strX |> Str.toNat |> orCrash "Expected a number"
y = strY |> Str.toNat |> orCrash "Expected a number"
[strCol, strRow] ->
row = strRow |> Str.toNat |> orCrash "Expected a number"
col = strCol |> Str.toNat |> orCrash "Expected a number"

{ x, y: y - offsetY }
{ row, col: col - offsetCols }

_ -> crash "Expected a point: \(input)"

Expand All @@ -106,36 +108,36 @@ setRockPath = \cave, path ->

_ -> cave

addRockLineSegment : Cave, Array2D.Index, Array2D.Index -> Cave
addRockLineSegment : Cave, Index2D, Index2D -> Cave
addRockLineSegment = \cave, point1, point2 ->
if point1.x == point2.x && point1.y != point2.y then
if point1.row == point2.row && point1.col != point2.col then
# vertical line
x = point1.x
startY = Num.min point1.y point2.y
endY = Num.max point1.y point2.y
row = point1.row
startCol = Num.min point1.col point2.col
endCol = Num.max point1.col point2.col

{ start: At startY, end: At endY }
{ start: At startCol, end: At endCol }
|> List.range
|> List.walk cave \state, y -> Array2D.set state { x, y } Rock
else if point1.x != point2.x && point1.y == point2.y then
|> List.walk cave \state, col -> Array2D.set state { row, col } Rock
else if point1.row != point2.row && point1.col == point2.col then
# horizontal line
y = point1.y
startX = Num.min point1.x point2.x
endX = Num.max point1.x point2.x
col = point1.col
startRow = Num.min point1.row point2.row
endRow = Num.max point1.row point2.row

{ start: At startX, end: At endX }
{ start: At startRow, end: At endRow }
|> List.range
|> List.walk cave \state, x -> Array2D.set state { x, y } Rock
|> List.walk cave \state, row -> Array2D.set state { row, col } Rock
else
crash "Diagonal lines not supported"

addFloor : Cave -> Cave
addFloor = \cave ->
cave
|> Array2D.findLastIndex \material -> material == Rock
|> Result.map \{ x: floorLevel, y: _ } ->
point1 = { x: floorLevel + 2, y: 0 }
point2 = { x: floorLevel + 2, y: cave |> Array2D.shape |> .dimY }
|> Result.map \{ row: floorLevel, col: _ } ->
point1 = { row: floorLevel + 2, col: 0 }
point2 = { row: floorLevel + 2, col: cave |> Array2D.shape |> .cols }
addRockLineSegment cave point1 point2
|> orCrash "Error finding floor level"

Expand Down

0 comments on commit b790209

Please sign in to comment.