my solutions to advent of code
aoc advent-of-code
1import gleam/int.{to_string} 2import gleam/io.{println} 3import gleam/list.{fold} 4import gleam/regexp.{check} 5import gleam/result.{unwrap} 6import gleam/string.{split, trim} 7import simplifile.{read} 8 9// regex feels like cheating idk 10 11pub fn is_nice_part_1(str: String) { 12 let assert Ok(has_three_vowels) = 13 regexp.from_string(".*?[aeiou].*?[aeiou].*?[aeiou].*?") 14 let assert Ok(has_doubled_letter) = regexp.from_string("([a-z])\\1") 15 let assert Ok(has_bad_string) = regexp.from_string("(ab|cd|pq|xy)") 16 17 { has_three_vowels |> check(str) } 18 && { has_doubled_letter |> check(str) } 19 && !{ has_bad_string |> check(str) } 20} 21 22pub fn is_nice_part_2(str: String) { 23 let assert Ok(has_doubled_pair) = regexp.from_string("([a-z])[a-z]\\1") 24 let assert Ok(has_letter1_letter2_letter1) = 25 regexp.from_string("([a-z][a-z]).*?\\1") 26 27 { has_doubled_pair |> check(str) } 28 && { has_letter1_letter2_letter1 |> check(str) } 29} 30 31pub fn main() { 32 let input = read(from: "../input.txt") |> unwrap("") |> trim |> split("\n") 33 34 println( 35 input 36 |> fold(0, fn(acc, str) { 37 case is_nice_part_1(str) { 38 True -> acc + 1 39 False -> acc 40 } 41 }) 42 |> to_string, 43 ) 44 45 println( 46 input 47 |> fold(0, fn(acc, str) { 48 case is_nice_part_2(str) { 49 True -> acc + 1 50 False -> acc 51 } 52 }) 53 |> to_string, 54 ) 55}