diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-02-20 18:08:07 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-02-20 18:08:07 +0100 |
commit | 58a01f64f8807668f5a403bf45579a62524c8791 (patch) | |
tree | eaa5f5f1330603c21c24fd6b0a6404d6d5086ede /aoc-2020-gleam/src/days | |
parent | 282e89c70e7e0e48a198d4d243d1d4d0561b8654 (diff) | |
download | gleam_aoc2020-58a01f64f8807668f5a403bf45579a62524c8791.tar.gz gleam_aoc2020-58a01f64f8807668f5a403bf45579a62524c8791.zip |
Finish day 5
Diffstat (limited to 'aoc-2020-gleam/src/days')
-rw-r--r-- | aoc-2020-gleam/src/days/day05.gleam | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/aoc-2020-gleam/src/days/day05.gleam b/aoc-2020-gleam/src/days/day05.gleam new file mode 100644 index 0000000..f655be9 --- /dev/null +++ b/aoc-2020-gleam/src/days/day05.gleam @@ -0,0 +1,55 @@ +import gleam/io +import gleam/int +import gleam/string +import gleam/list +import gleam/set +import gleam/iterator as iter +import ext/resultx +import util/input_util + +fn get_seat_id(pass: String) -> Int { + pass + |> string.to_graphemes + |> list.map(with: fn(grapheme) { + case grapheme { + "F" | "L" -> "0" + "B" | "R" -> "1" + } + }) + |> string.concat + |> int.base_parse(2) + |> resultx.force_unwrap +} + +fn part1(lines: List(String)) -> Int { + lines + |> list.map(with: get_seat_id) + |> list.reduce(with: int.max) + |> resultx.force_unwrap +} + +fn part2(lines: List(String)) -> Int { + let seat_ids = + lines + |> list.map(with: get_seat_id) + |> set.from_list + + let occupied = fn(id) { set.contains(in: seat_ids, this: id) } + + iter.find( + in: iter.range(from: 1, to: 1023), + one_that: fn(id) { occupied(id - 1) && !occupied(id) && occupied(id + 1) }, + ) + |> resultx.force_unwrap +} + +pub fn run() -> Nil { + let test = input_util.read_lines("test05") + assert 820 = part1(test) + + let input = input_util.read_lines("day05") + io.debug(part1(input)) + io.debug(part2(input)) + + Nil +} |