fun formatting
This commit is contained in:
parent
57a08d0353
commit
8ee119b41a
1 changed files with 34 additions and 46 deletions
|
|
@ -9,23 +9,6 @@ main =
|
|||
Stdout.line! "Part 1: $(Num.toStr (run data 1))"
|
||||
Stdout.line! "Part 2: $(Num.toStr (run data 2))"
|
||||
|
||||
isDigit = \char ->
|
||||
Str.fromUtf8 [char]
|
||||
|> Result.withDefault "0"
|
||||
|> Str.toU32
|
||||
|> Result.isOk
|
||||
|
||||
toDigit = \u8s ->
|
||||
Str.fromUtf8 u8s
|
||||
|> Result.withDefault "0"
|
||||
|> Str.toU32
|
||||
|> Result.withDefault 0
|
||||
|
||||
fromVal = \val ->
|
||||
when val is
|
||||
Val x -> x
|
||||
Null -> 0
|
||||
|
||||
run = \code, part ->
|
||||
Str.walkUtf8 code { digits: [], chars: [], curNum: [], firstNum: Null, enabled: Bool.true } \state, elem ->
|
||||
isEnabled = state.enabled || part == 1
|
||||
|
|
@ -50,24 +33,29 @@ run = \code, part ->
|
|||
'l' if state.chars == ['m', 'u'] -> { state & chars: List.append state.chars 'l' }
|
||||
'(' if state.chars == ['m', 'u', 'l'] -> { state & chars: List.append state.chars '(' }
|
||||
|
||||
d if isDigit d && state.chars == ['m', 'u', 'l', '('] -> { state & curNum: [d], chars: List.append state.chars d }
|
||||
d if isDigit d && isDigit previousChar -> { state & curNum: List.append state.curNum d, chars: List.append state.chars d }
|
||||
d if isDigit d && previousChar == ',' -> { state & curNum: [d], chars: List.append state.chars d }
|
||||
d if state.chars == ['m', 'u', 'l', '('] && isDigit d -> { state & chars: List.append state.chars d, curNum: [d] }
|
||||
d if isDigit d && isDigit previousChar -> { state & chars: List.append state.chars d, curNum: List.append state.curNum d }
|
||||
d if isDigit d && previousChar == ',' -> { state & chars: List.append state.chars d, curNum: [d] }
|
||||
|
||||
',' if isDigit previousChar && state.firstNum == Null -> {
|
||||
state &
|
||||
chars: List.append state.chars ',',
|
||||
curNum: [],
|
||||
firstNum: Val (toDigit state.curNum)
|
||||
}
|
||||
')' if isDigit previousChar && state.firstNum != Null -> {
|
||||
state &
|
||||
chars: List.append state.chars ')',
|
||||
curNum: [],
|
||||
firstNum: Null,
|
||||
digits: List.append state.digits (fromVal state.firstNum, toDigit state.curNum)
|
||||
}
|
||||
',' if isDigit previousChar && state.firstNum == Null -> { state & chars: List.append state.chars ',', curNum: [], firstNum: Val (toDigit state.curNum) }
|
||||
')' if isDigit previousChar && state.firstNum != Null -> { state & curNum: [], digits: List.append state.digits (fromVal state.firstNum, toDigit state.curNum) }
|
||||
_ -> { state & chars: [], curNum: [], firstNum: Null } # didn't match, blank it out
|
||||
|
||||
|> .digits
|
||||
|> List.walk 0 \sum, (l, r) -> sum + l * r
|
||||
|
||||
isDigit = \char ->
|
||||
Str.fromUtf8 [char]
|
||||
|> Result.withDefault "0"
|
||||
|> Str.toU32
|
||||
|> Result.isOk
|
||||
|
||||
toDigit = \u8s ->
|
||||
Str.fromUtf8 u8s
|
||||
|> Result.withDefault "0"
|
||||
|> Str.toU32
|
||||
|> Result.withDefault 0
|
||||
|
||||
fromVal = \val ->
|
||||
when val is
|
||||
Val x -> x
|
||||
Null -> 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue