my solutions to advent of code
aoc advent-of-code
at main 1.7 kB view raw
1import gleam/int 2import gleam/io 3import gleam/list 4import gleam/result 5import gleam/string 6import simplifile as file 7 8pub type RotationState { 9 RotationState(turn: Int, zeroes: Int) 10} 11 12pub fn main() { 13 let assert Ok(input) = file.read(from: "../input.txt") 14 as "Input file not found" 15 let input = 16 input 17 |> string.trim 18 |> string.split("\n") 19 |> list.map(fn(l) { 20 case l { 21 "R" <> turn -> turn |> int.parse |> result.unwrap(0) 22 "L" <> turn -> -{ turn |> int.parse |> result.unwrap(0) } 23 _ -> panic as "bad input" 24 } 25 }) 26 27 let part1 = 28 input 29 |> list.fold(RotationState(50, 0), fn(acc, v) { 30 let new_number = 31 int.modulo(acc.turn + v, 100) 32 |> result.unwrap(0) 33 RotationState(new_number, case new_number { 34 0 -> acc.zeroes + 1 35 _ -> acc.zeroes 36 }) 37 }) 38 part1.zeroes 39 |> int.to_string 40 |> io.println 41 42 let part2 = 43 input 44 |> list.fold(RotationState(50, 0), fn(acc, v) { 45 let raw_new_number = acc.turn + v 46 let raw_zeroes = int.absolute_value(raw_new_number / 100) 47 let zeroes = 48 acc.zeroes 49 + case acc.turn != 0 && raw_new_number <= 0 { 50 // if it is below zero before being moduloed and the original number itself wasn't zero it means that it did touch zero but the division thing wouldn't count it, so we give this extra support. 51 // of course, there is no need to deal with a negative to positive situation because the acc.turn will never be negative!!! 52 True -> raw_zeroes + 1 53 False -> raw_zeroes 54 } 55 RotationState(int.modulo(raw_new_number, 100) |> result.unwrap(0), zeroes) 56 }) 57 part2.zeroes 58 |> int.to_string 59 |> io.println 60}