aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-02-20 18:08:07 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-02-20 18:08:07 +0100
commit58a01f64f8807668f5a403bf45579a62524c8791 (patch)
treeeaa5f5f1330603c21c24fd6b0a6404d6d5086ede
parent282e89c70e7e0e48a198d4d243d1d4d0561b8654 (diff)
downloadgleam_aoc2020-58a01f64f8807668f5a403bf45579a62524c8791.tar.gz
gleam_aoc2020-58a01f64f8807668f5a403bf45579a62524c8791.zip
Finish day 5
-rw-r--r--aoc-2020-gleam/src/aoc_2020_gleam.gleam2
-rw-r--r--aoc-2020-gleam/src/days/day05.gleam55
2 files changed, 57 insertions, 0 deletions
diff --git a/aoc-2020-gleam/src/aoc_2020_gleam.gleam b/aoc-2020-gleam/src/aoc_2020_gleam.gleam
index bef5e96..4ba7c26 100644
--- a/aoc-2020-gleam/src/aoc_2020_gleam.gleam
+++ b/aoc-2020-gleam/src/aoc_2020_gleam.gleam
@@ -4,6 +4,7 @@ import days/day01
import days/day02
import days/day03
import days/day04
+import days/day05
pub fn main() -> Nil {
use day <- runner.with_day()
@@ -12,6 +13,7 @@ pub fn main() -> Nil {
2 -> day02.run()
3 -> day03.run()
4 -> day04.run()
+ 5 -> day05.run()
_ -> io.println("Day not found!")
}
}
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
+}