aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/src/day13/solve.gleam
blob: a597074352fa66405a9c544249c0142c04b05164 (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
import adglent.{First, Second}
import gleam/io
import gleam/list
import gleam/string
import gleam/result

type SymmetryType {
  Horizontal(Int)
  Vertical(Int)
}

fn is_symmetric(xs: List(List(a)), errors: Int, index: Int) -> Result(Int, Nil) {
  case list.split(xs, index) {
    #(_, []) -> Error(Nil)
    #(ls, rs) -> {
      let zipped = list.zip(list.flatten(list.reverse(ls)), list.flatten(rs))
      let found_errors =
        zipped
        |> list.filter(fn(tup) { tup.1 != tup.0 })
        |> list.length
      case found_errors == errors {
        True -> Ok(index)
        False -> is_symmetric(xs, errors, index + 1)
      }
    }
  }
}

fn get_symmetry_type(xs: List(List(String)), errors: Int) {
  result.or(
    xs
    |> is_symmetric(errors, 1)
    |> result.map(Horizontal(_)),
    xs
    |> list.transpose()
    |> is_symmetric(errors, 1)
    |> result.map(Vertical(_)),
  )
}

fn summarize_notes(symmetries: List(SymmetryType)) {
  use acc, note <- list.fold(symmetries, 0)
  case note {
    Horizontal(n) -> 100 * n
    Vertical(n) -> n
  } + acc
}

fn solve(input: String, errors: Int) {
  input
  |> string.split("\n\n")
  |> list.map(fn(strs) {
    strs
    |> string.split("\n")
    |> list.map(string.to_graphemes)
    |> get_symmetry_type(errors)
  })
  |> result.values
  |> summarize_notes
  |> string.inspect
}

pub fn part1(input: String) {
  solve(input, 0)
}

pub fn part2(input: String) {
  solve(input, 1)
}

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