diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-12-23 12:47:11 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-12-23 12:47:11 +0100 |
commit | 5aa24ac572ab5451e187f9accfa6833634c58215 (patch) | |
tree | 45b9daab20e182dcc5f65ec0271727fb847f6e7d | |
parent | 1b8b85dd5f0aead5fb97ee9d8aa99d877c6a79e7 (diff) | |
download | gleam_aoc2020-5aa24ac572ab5451e187f9accfa6833634c58215.tar.gz gleam_aoc2020-5aa24ac572ab5451e187f9accfa6833634c58215.zip |
Solve part 1 of day 24
-rw-r--r-- | aoc-2020-gleam/src/days/day24.gleam | 51 | ||||
-rw-r--r-- | aoc-2020-gleam/src/ext/setx.gleam | 16 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/hex.gleam | 21 |
3 files changed, 84 insertions, 4 deletions
diff --git a/aoc-2020-gleam/src/days/day24.gleam b/aoc-2020-gleam/src/days/day24.gleam new file mode 100644 index 0000000..66bedb8 --- /dev/null +++ b/aoc-2020-gleam/src/days/day24.gleam @@ -0,0 +1,51 @@ +import gleam/io +import gleam/set +import gleam/list +import ext/setx +import ext/resultx as resx +import util/input_util +import util/parser as p +import util/hex.{type Hex} + +fn parse_tiles(lines: List(String)) -> List(Hex) { + let tile_parser = + [ + p.replace(p.literal("e"), with: hex.e), + p.replace(p.literal("se"), with: hex.se), + p.replace(p.literal("sw"), with: hex.sw), + p.replace(p.literal("w"), with: hex.w), + p.replace(p.literal("nw"), with: hex.nw), + p.replace(p.literal("ne"), with: hex.ne), + ] + |> p.any + |> p.many1 + |> p.map(with: list.fold(over: _, from: hex.zero, with: hex.add)) + + list.map(lines, with: fn(line) { + line + |> p.parse_entire(with: tile_parser) + |> resx.assert_unwrap + }) +} + +fn part1(lines: List(String)) -> Int { + let tiles = parse_tiles(lines) + let blacks = list.fold(over: tiles, from: set.new(), with: setx.toggle) + set.size(blacks) +} + +fn part2(lines: List(String)) -> Nil { + Nil +} + +pub fn main() -> Nil { + let testing = input_util.read_lines("test24") + let assert 10 = part1(testing) + io.debug(part2(testing)) + + let input = input_util.read_lines("day24") + io.debug(part1(input)) + io.debug(part2(input)) + + Nil +} diff --git a/aoc-2020-gleam/src/ext/setx.gleam b/aoc-2020-gleam/src/ext/setx.gleam index 68d185a..6df9256 100644 --- a/aoc-2020-gleam/src/ext/setx.gleam +++ b/aoc-2020-gleam/src/ext/setx.gleam @@ -3,16 +3,24 @@ import gleam/set.{type Set} import gleam/iterator as iter import ext/iteratorx as iterx -pub fn count(set: Set(a), satisfying predicate: fn(a) -> Bool) -> Int { - set +pub fn count(s: Set(a), satisfying predicate: fn(a) -> Bool) -> Int { + s |> set.to_list |> iter.from_list |> iterx.count(satisfying: predicate) } -pub fn map(set: Set(a), with fun: fn(a) -> b) -> Set(b) { - set +pub fn map(s: Set(a), with fun: fn(a) -> b) -> Set(b) { + s |> set.to_list |> list.map(with: fun) |> set.from_list } + +pub fn toggle(in s: Set(a), this value: a) -> Set(a) { + s + |> case set.contains(in: s, this: value) { + True -> set.delete(from: _, this: value) + False -> set.insert(into: _, this: value) + } +} diff --git a/aoc-2020-gleam/src/util/hex.gleam b/aoc-2020-gleam/src/util/hex.gleam new file mode 100644 index 0000000..21db192 --- /dev/null +++ b/aoc-2020-gleam/src/util/hex.gleam @@ -0,0 +1,21 @@ +pub opaque type Hex { + Hex(q: Int, r: Int, s: Int) +} + +pub const zero = Hex(q: 0, r: 0, s: 0) + +pub const e = Hex(q: 1, r: 0, s: -1) + +pub const se = Hex(q: 0, r: 1, s: -1) + +pub const sw = Hex(q: -1, r: 1, s: 0) + +pub const w = Hex(q: -1, r: 0, s: 1) + +pub const nw = Hex(q: 0, r: -1, s: 1) + +pub const ne = Hex(q: 1, r: -1, s: 0) + +pub fn add(a: Hex, b: Hex) -> Hex { + Hex(q: a.q + b.q, r: a.r + b.r, s: a.s + b.s) +} |