aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/src/day8/solve.gleam
blob: dee24a4081bafab646054abdba669fe27fe7df22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import adglent.{First, Second}
import gleam/bool
import gleam/dict.{type Dict}
import gleam/io
import gleam/iterator.{Next}
import gleam/list
import gleam/option.{Some}
import gleam/string
import gleam/regex.{type Match, Match}
import gleam_community/maths/arithmetics

type Paths {
  Paths(to_left: String, to_right: String)
}

type Maze =
  Dict(String, Paths)

fn parse(input: String) {
  let [directions_str, maze_str] = string.split(input, "\n\n")

  let directions =
    directions_str
    |> string.to_graphemes()
    |> iterator.from_list
    |> iterator.cycle

  let assert Ok(re) = regex.from_string("(...) = \\((...), (...)\\)")
  let maze =
    maze_str
    |> string.split("\n")
    |> list.map(fn(str) {
      let assert [Match(submatches: [Some(name), Some(left), Some(right)], ..)] =
        regex.scan(re, str)
      #(name, Paths(left, right))
    })
    |> dict.from_list

  #(directions, maze)
}

fn to_next_step(current, stop_at, count, directions, maze: Maze) {
  use <- bool.guard(string.ends_with(current, stop_at), count)
  let assert Next(next_direction, rest_directions) = iterator.step(directions)
  let assert Ok(paths) = dict.get(maze, current)
  case next_direction {
    "L" -> paths.to_left
    "R" -> paths.to_right
  }
  |> to_next_step(stop_at, count + 1, rest_directions, maze)
}

pub fn part1(input: String) {
  let #(directions, maze) = parse(input)

  to_next_step("AAA", "ZZZ", 0, directions, maze)
  |> string.inspect
}

pub fn part2(input: String) {
  let #(directions, maze) = parse(input)

  maze
  |> dict.keys
  |> list.filter(string.ends_with(_, "A"))
  |> list.fold(
    1,
    fn(acc, name) {
      to_next_step(name, "Z", 0, directions, maze)
      |> arithmetics.lcm(acc)
    },
  )
  |> string.inspect
}

pub fn main() {
  let assert Ok(part) = adglent.get_part()
  let assert Ok(input) = adglent.get_input("8")
  case part {
    First ->
      part1(input)
      |> adglent.inspect
      |> io.println
    Second ->
      part2(input)
      |> adglent.inspect
      |> io.println
  }
}