Skip to content

Commit

Permalink
Update AoC 2021
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewilliamboswell committed Aug 30, 2023
1 parent 6fed9b6 commit e47a6b4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
2 changes: 1 addition & 1 deletion aoc-2021/01/main.roc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
app "aoc"
packages {
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.3.2/tE4xS_zLdmmxmHwHih9kHWQ7fsXtJr7W7h3425-eZFk.tar.br",
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.5.0/Cufzl36_SnJ4QbOoEmiJ5dIpUxBvdB3NEySvuH82Wio.tar.br",
}
imports [
pf.Stdout,
Expand Down
8 changes: 4 additions & 4 deletions aoc-2021/02/main.roc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
app "aoc"
packages {
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.3.2/tE4xS_zLdmmxmHwHih9kHWQ7fsXtJr7W7h3425-eZFk.tar.br",
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.5.0/Cufzl36_SnJ4QbOoEmiJ5dIpUxBvdB3NEySvuH82Wio.tar.br",
}
imports [
pf.Stdout,
Expand All @@ -25,10 +25,10 @@ maybeMove : Str, Str -> Result U64 [InvalidNumStr, NotFound]
maybeMove = \line, direction ->
line
|> Str.replaceFirst direction ""
|> Result.map Str.trim
|> Result.try Str.toU64
|> Str.trim
|> Str.toU64

expect Str.replaceFirst "forward 12" "forward" "" == Ok " 12"
expect Str.replaceFirst "forward 12" "forward" "" == " 12"
expect maybeMove "forward 12" "forward" == Ok 12

parseInput : Str -> List [Fd U64, Up U64, Dn U64]
Expand Down
2 changes: 1 addition & 1 deletion aoc-2021/03/main.roc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
app "aoc"
packages {
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.3.2/tE4xS_zLdmmxmHwHih9kHWQ7fsXtJr7W7h3425-eZFk.tar.br",
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.5.0/Cufzl36_SnJ4QbOoEmiJ5dIpUxBvdB3NEySvuH82Wio.tar.br",
}
imports [
pf.Stdout,
Expand Down
2 changes: 1 addition & 1 deletion aoc-2021/04/main.roc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
app "aoc"
packages {
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.3.2/tE4xS_zLdmmxmHwHih9kHWQ7fsXtJr7W7h3425-eZFk.tar.br",
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.5.0/Cufzl36_SnJ4QbOoEmiJ5dIpUxBvdB3NEySvuH82Wio.tar.br",
}
imports [
BingoBoard,
Expand Down
55 changes: 40 additions & 15 deletions aoc-2022/12/main.roc
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ main =
cols: 0,
}

part1SampleAnswer = part1 (Str.toUtf8 sampleInput) initialState
part1FileAnswer = part1 fileInput initialState
# part1SampleAnswer = part1 (Str.toUtf8 sampleInput) initialState
# part1FileAnswer = part1 fileInput initialState
part2SampleAnswer = part2 (Str.toUtf8 sampleInput) initialState

{} <- Stdout.line part1SampleAnswer |> Task.await
{} <- Stdout.line part1FileAnswer |> Task.await
# {} <- Stdout.line part1SampleAnswer |> Task.await
# {} <- Stdout.line part1FileAnswer |> Task.await
{} <- Stdout.line part2SampleAnswer |> Task.await

Task.succeed {}

Expand All @@ -63,29 +65,52 @@ part1 = \inputBytes, initialState ->
# Dijkstra's starting at Start
final = mainHelp state state.start

# Get the shortest path
shortest = shortestHelp final.previous final.heights final.end []

shortestSteps =
shortest
|> List.len
|> Num.toStr

"Part 1 - Shortest steps is: \(shortestSteps)"

part2 = \inputBytes, initialState ->

# Parse the map state
state = parseMap inputBytes initialState 0 0

# Dijkstra's starting at Start
final = mainHelp state state.start

# Debug the previous state
# xxx =
# Dict.toList final.previous
# |> List.map \(f, t) -> {f, t }
# dbg xxx

# Get the shortest path
shortest = shortestHelp final.previous final.end []
shortest = shortestHelp final.previous final.heights final.end []

dbg shortest

shortestSteps =
shortest
|> List.len
|> List.walkUntil 0 \count, height ->
if height == 0 then
Break (count + 1)
else
Continue (count + 1)
|> Num.toStr

"Part 1 - Shortest steps is: \(shortestSteps)"


shortestHelp : Dict Node Node, Node, List U8 -> List U8
shortestHelp = \previousNodes, current, steps ->
when Dict.get previousNodes current is
Err _ -> steps
Ok previous ->
shortestHelp previousNodes previous (List.append steps 'x')
"Part 2 - Shortest steps is: \(shortestSteps)"

shortestHelp : Dict Node Node, Dict Node U8, Node, List U8 -> List U8
shortestHelp = \previousNodes, heights, current, steps ->
when (Dict.get previousNodes current, Dict.get heights current) is
(Ok previous, Ok height) ->
shortestHelp previousNodes heights previous (List.append steps height)
(_,_) -> steps

mainHelp : State, Node -> State
mainHelp = \state, currentNode ->
Expand Down

0 comments on commit e47a6b4

Please sign in to comment.