# Advent of Code, Day 2 # # Follow a robot across a repeating roygbv waypoint cycle. Each # instruction jumps to the very next waypoint of that colour, so the # distance is (target - position) mod 6, or 6 when the colour repeats. # # roc run day2.roc # # Note: Roc 0.1.0's default app platform exposes only Echo.line! (no file # I/O), so the puzzle input is embedded below as a single-line string. # The puzzle input: one program of 1042 instructions. input = "gvvggogbvogbbrrvgvrbgbbggrvorvoobovggoogorgvbvbrorgbvorogggbvvogogogbobvgrvbrbbgbbgorvvogbbogbgbbrgooobbgrgbvrggvvbovorbrvggrrvgbrogbbvbovbbrbrrggbvbvgorbvobbobbbrbrgvbggvgbobbgrvrbbrrobbgobobgrroooogbgbbgbgbvobogrorovogrbbvgggrgbggbrrrbvgrrgoggrgbrbgrbgrbvgbogorogvobovoggrvrroobbrorgrggogorbroggvrvrrrgbooogbrbbrbvrrgggbboovgrrbggvgrbboboorvggborboobvvgbgbgrbgrrbroobboobvrrgbgggrogborgrrgogoobrrrovgbbvobooggvggrgobogbbrrbbvobovggrgrovrgrrgrvboobvggovbggboorovbovgbbgoobbvbgorovgrrrvbbrovbbgbbgbbgogrbrbgbbgrogobbggbgoboogbbgggbbrbgvvrvbrrgbroorgbbgoorgbbbggbogbgovrgobbbvrgorrrggobooogbgbrbbbrobbovvbgooogbovbvbbroogbrvvbgrorbbovbrggboggrbbogvrrbbggggbrggggggboogrrovogbrbvbvbrrgogrgoobgbbooobrvrrvggovvggborogvbbroggbbbgrbogrrbobggbggrbovrbvbrrgrrrgobrgbgbobororbgbgvbbbvrrrorgobobovbogrobbrbgogvbobgovrgbgggvggrggbroogvvrvggobobrovrogrorvrborrgoggbrggbrggbgrgbbgbbggoggroogrbvbooorrbobvgbbogovgorvvgggvobovrbrgobvrrobbgbrrbrrrobbgrvrgorbbrbvbvobbogrbgbgrooobvbobgbgboggbrgggbbvbggoogrgrovogrggvbrrrrobrovgboorggrrrbvorgborvbgvvroggbrrrr" # Waypoint index of each colour, by its byte value. The annotation # widens the fold: the running total (thousands) would not fit in a byte. idx : U8 -> U64 idx = |b| { match b { 114 => 0 # r 111 => 1 # o 121 => 2 # y 103 => 3 # g 98 => 4 # b 118 => 5 # v _ => 0 } } main! = |_| { bytes = Str.to_utf8(input) final = List.fold(bytes, { pos: 0, total: 0 }, |state, b| { target = idx(b) # add 6 before the modulo so the subtraction never goes negative raw = (target + 6 - state.pos) % 6 dist = if raw == 0 { 6 } else { raw } { pos: target, total: state.total + dist } }) Echo.line!("answer: ${final.total.to_str()}\n") Ok({}) }