this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Clean up day 1

estym 573c5aef 2f49c67a

+24 -47
+24 -47
src/days/day1.gleam
··· 1 - import gleam/dynamic 2 - import gleam/float 3 1 import gleam/int 4 2 import gleam/io 5 3 import gleam/list ··· 30 28 data 31 29 |> string.split("\n") 32 30 |> list.map(fn(l) { 33 - let chars = 34 - l 35 - |> string.to_graphemes() 36 - 37 - let direction_string = 38 - chars 39 - |> list.first 40 - |> result.unwrap("") 31 + let graphemes = string.to_graphemes(l) 41 32 42 - let direction = case direction_string { 43 - "L" -> Left 44 - "R" -> Right 33 + let direction = case list.first(graphemes) { 34 + Ok("L") -> Left 35 + Ok("R") -> Right 45 36 _ -> Left 46 37 } 47 38 48 39 let number = 49 - chars 40 + graphemes 50 41 |> list.drop(1) 51 42 |> string.join("") 52 43 |> int.parse ··· 59 50 fn part1(data: List(Action)) { 60 51 data 61 52 |> list.fold([50], fn(acc: List(Int), elem: Action) { 62 - let current = 63 - acc 64 - |> list.first 53 + let current = list.first(acc) |> result.unwrap(0) 54 + 55 + let new = 56 + case elem { 57 + Action(Left, x) -> current - x 58 + Action(Right, x) -> current + x 59 + } 60 + |> int.modulo(100) 65 61 |> result.unwrap(0) 66 62 67 - let new = case elem { 68 - Action(Left, x) -> { current - x } % 100 69 - Action(Right, x) -> { current + x } % 100 70 - } 71 - 72 - let new = case new < 0 { 73 - True -> new + 100 74 - False -> new 75 - } 76 - 77 - acc 78 - |> list.prepend(new) 63 + list.prepend(acc, new) 79 64 }) 80 65 |> list.count(fn(x) { x == 0 }) 81 66 } ··· 83 68 fn part2(data: List(Action)) { 84 69 data 85 70 |> list.fold([#(50, 0)], fn(acc: List(#(Int, Int)), elem: Action) { 86 - let #(current, _) = 87 - acc 88 - |> list.first 89 - |> result.unwrap(#(0, 0)) 71 + let #(current, _) = list.first(acc) |> result.unwrap(#(0, 0)) 90 72 91 73 let Action(dir, clicks) = elem 92 - 93 - let remaining = clicks % 100 94 - 95 74 let reached_zero = case dir { 96 - Left -> current - remaining <= 0 && current != 0 97 - Right -> current + remaining >= 100 && current != 0 75 + Left -> current - { clicks % 100 } <= 0 && current != 0 76 + Right -> current + { clicks % 100 } >= 100 && current != 0 98 77 } 99 78 100 79 let reached = ··· 104 83 False -> 0 105 84 } 106 85 107 - let new = case elem { 108 - Action(Left, x) -> { current - x } % 100 109 - Action(Right, x) -> { current + x } % 100 110 - } 111 - 112 - let new = case new < 0 { 113 - True -> new + 100 114 - False -> new 115 - } 86 + let new = 87 + case elem { 88 + Action(Left, x) -> current - x 89 + Action(Right, x) -> current + x 90 + } 91 + |> int.modulo(100) 92 + |> result.unwrap(0) 116 93 117 94 acc 118 95 |> list.prepend(#(new, reached))