aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/src/day18/solve.gleam
diff options
context:
space:
mode:
authorJ.J <thechairman@thechairman.info>2024-05-30 21:49:58 -0400
committerJ.J <thechairman@thechairman.info>2024-05-30 21:49:58 -0400
commit231c2b688d1e6cf0846d46e883da30e042a9c6cf (patch)
tree98a6d3a461fe190b38b2cf33a708a1d01703fa70 /aoc2023/src/day18/solve.gleam
parentfe088aa5778dcdbaab4dd8d4a7395a91c444b45c (diff)
parenta2c2b728ec6051323ed937f54816089cd2ae9d20 (diff)
downloadgleam_aoc-231c2b688d1e6cf0846d46e883da30e042a9c6cf.tar.gz
gleam_aoc-231c2b688d1e6cf0846d46e883da30e042a9c6cf.zip
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
Diffstat (limited to 'aoc2023/src/day18/solve.gleam')
-rw-r--r--aoc2023/src/day18/solve.gleam113
1 files changed, 0 insertions, 113 deletions
diff --git a/aoc2023/src/day18/solve.gleam b/aoc2023/src/day18/solve.gleam
deleted file mode 100644
index 2c000f9..0000000
--- a/aoc2023/src/day18/solve.gleam
+++ /dev/null
@@ -1,113 +0,0 @@
-import adglent.{First, Second}
-import gleam/io
-import gleam/int
-import gleam/list
-import gleam/option.{Some}
-import gleam/regex.{type Match, Match}
-import gleam/string
-
-type Coord {
- Coord(x: Int, y: Int)
-}
-
-type Direction {
- Up
- Right
- Down
- Left
-}
-
-type Dig {
- Dig(dir: Direction, dist: Int)
-}
-
-fn to_direction(c: String) {
- case c {
- "R" | "0" -> Right
- "D" | "1" -> Down
- "L" | "2" -> Left
- "U" | "3" -> Up
- _ -> panic
- }
-}
-
-fn parse_front(line: String) {
- let assert Ok(re) = regex.from_string("(.) (.*) \\(.*\\)")
- let assert [Match(submatches: [Some(dir), Some(dist)], ..)] =
- regex.scan(with: re, content: line)
- let assert Ok(n) = int.parse(dist)
- Dig(to_direction(dir), n)
-}
-
-fn parse_hex(line: String) {
- let assert Ok(re) = regex.from_string("\\(#(.....)(.)\\)")
- let assert [Match(submatches: [Some(dist), Some(dir)], ..)] =
- regex.scan(with: re, content: line)
- let assert Ok(n) = int.base_parse(dist, 16)
- Dig(to_direction(dir), n)
-}
-
-fn go(current: Coord, dig: Dig) {
- case dig {
- Dig(Up, n) -> Coord(current.x, current.y + n)
- Dig(Right, n) -> Coord(current.x + n, current.y)
- Dig(Down, n) -> Coord(current.x, current.y - n)
- Dig(Left, n) -> Coord(current.x - n, current.y)
- }
-}
-
-fn double_triangle(c1: Coord, c2: Coord) {
- { c1.x * c2.y } - { c2.x * c1.y }
-}
-
-fn start_dig(digs: List(Dig)) {
- do_next_dig(digs, Coord(0, 0), 0, 0)
-}
-
-fn do_next_dig(
- digs: List(Dig),
- current: Coord,
- area: Int,
- perimeter: Int,
-) -> Int {
- case digs {
- [] -> int.absolute_value(area) / 2 + { perimeter / 2 } + 1
- [dig, ..rest] -> {
- let next = go(current, dig)
- let area = area + double_triangle(current, next)
- let perimeter = perimeter + dig.dist
- do_next_dig(rest, next, area, perimeter)
- }
- }
-}
-
-fn solve_with(input, f) {
- input
- |> string.split("\n")
- |> list.map(f)
- |> start_dig
- |> string.inspect
-}
-
-pub fn part1(input: String) {
- solve_with(input, parse_front)
-}
-
-pub fn part2(input: String) {
- solve_with(input, parse_hex)
-}
-
-pub fn main() {
- let assert Ok(part) = adglent.get_part()
- let assert Ok(input) = adglent.get_input("18")
- case part {
- First ->
- part1(input)
- |> adglent.inspect
- |> io.println
- Second ->
- part2(input)
- |> adglent.inspect
- |> io.println
- }
-}