diff options
Diffstat (limited to 'aoc-2020-gleam')
-rw-r--r-- | aoc-2020-gleam/manifest.toml | 4 | ||||
-rw-r--r-- | aoc-2020-gleam/src/days/day02.gleam | 9 | ||||
-rw-r--r-- | aoc-2020-gleam/src/days/day03.gleam | 3 | ||||
-rw-r--r-- | aoc-2020-gleam/src/days/day04.gleam | 3 | ||||
-rw-r--r-- | aoc-2020-gleam/src/days/day05.gleam | 2 | ||||
-rw-r--r-- | aoc-2020-gleam/src/days/day07.gleam | 10 | ||||
-rw-r--r-- | aoc-2020-gleam/src/days/day08.gleam | 16 | ||||
-rw-r--r-- | aoc-2020-gleam/src/ext/genericx.gleam | 7 | ||||
-rw-r--r-- | aoc-2020-gleam/src/ext/intx.gleam | 3 | ||||
-rw-r--r-- | aoc-2020-gleam/src/ext/resultx.gleam | 6 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/parser.gleam | 14 |
11 files changed, 50 insertions, 27 deletions
diff --git a/aoc-2020-gleam/manifest.toml b/aoc-2020-gleam/manifest.toml index 2b3dceb..e349bd0 100644 --- a/aoc-2020-gleam/manifest.toml +++ b/aoc-2020-gleam/manifest.toml @@ -2,8 +2,8 @@ # You typically do not need to edit this file packages = [ - { name = "gleam_erlang", version = "0.18.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "14ABC93A7975369CCDDC0C4D9A34C0E0FE99CA3AD336BE6A17299EC0285B7107" }, - { name = "gleam_stdlib", version = "0.26.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "B17BBE8A78F3909D93BCC6C24F531673A7E328A61F24222EB1E58D0A7552B1FE" }, + { name = "gleam_erlang", version = "0.18.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "C69F59D086AD50B80DE294FB0963550630971C9DC04E92B1F7AEEDD2C0BE226C" }, + { name = "gleam_stdlib", version = "0.27.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "9DBDD21B48C654182CDD8AA15ACF85E8E74A0438583C68BD7EF08BE89F999C6F" }, ] [requirements] diff --git a/aoc-2020-gleam/src/days/day02.gleam b/aoc-2020-gleam/src/days/day02.gleam index cedb604..530ab0f 100644 --- a/aoc-2020-gleam/src/days/day02.gleam +++ b/aoc-2020-gleam/src/days/day02.gleam @@ -3,6 +3,8 @@ import gleam/list import gleam/bool import gleam/string as str import ext/listx +import ext/intx +import ext/genericx as genx import ext/resultx as resx import util/input_util import util/parser as p @@ -50,8 +52,8 @@ fn part1(lines: List(String)) -> Int { fn(line) { line.password |> str.to_graphemes - |> listx.count(satisfying: fn(g) { g == line.policy.grapheme }) - |> fn(l) { line.policy.min <= l && l <= line.policy.max } + |> listx.count(satisfying: genx.equals(_, line.policy.grapheme)) + |> intx.is_between(line.policy.min, and: line.policy.max) }, ) } @@ -64,7 +66,8 @@ fn part2(lines: List(String)) -> Int { line.password |> str.to_graphemes |> list.at(index - 1) - |> resx.assert_unwrap == line.policy.grapheme + |> resx.assert_unwrap + |> genx.equals(line.policy.grapheme) } bool.exclusive_or( grapheme_matches(line.policy.min), diff --git a/aoc-2020-gleam/src/days/day03.gleam b/aoc-2020-gleam/src/days/day03.gleam index e4cecda..4c1ef59 100644 --- a/aoc-2020-gleam/src/days/day03.gleam +++ b/aoc-2020-gleam/src/days/day03.gleam @@ -5,6 +5,7 @@ import gleam/string as str import gleam/function as fun import gleam/iterator as iter import gleam/set.{Set} +import ext/intx import ext/resultx as resx import ext/iteratorx as iterx import util/input_util @@ -62,7 +63,7 @@ fn has_tree(in area: Area, at pos: Pos) -> Bool { } fn is_valid(pos: Pos, in area: Area) -> Bool { - 0 <= pos.1 && pos.1 < area.height + intx.is_between(pos.1, 0, and: area.height - 1) } fn tree_count(in area: Area, with slope: Pos) -> Int { diff --git a/aoc-2020-gleam/src/days/day04.gleam b/aoc-2020-gleam/src/days/day04.gleam index 21998e2..305cfa1 100644 --- a/aoc-2020-gleam/src/days/day04.gleam +++ b/aoc-2020-gleam/src/days/day04.gleam @@ -4,6 +4,7 @@ import gleam/function as fun import gleam/result as res import gleam/map.{Map} import ext/listx +import ext/intx import ext/resultx as resx import util/input_util import util/parser as p @@ -61,7 +62,7 @@ fn is_valid1(passport: Passport) -> Bool { fn is_valid2(passport: Passport) -> Bool { let int_between = fn(min, max) { p.int() - |> p.satisfying(rule: fn(num) { min <= num && num <= max }) + |> p.satisfying(rule: intx.is_between(_, min, and: max)) |> p.ignore } diff --git a/aoc-2020-gleam/src/days/day05.gleam b/aoc-2020-gleam/src/days/day05.gleam index 8eeb94b..b3448a8 100644 --- a/aoc-2020-gleam/src/days/day05.gleam +++ b/aoc-2020-gleam/src/days/day05.gleam @@ -34,7 +34,7 @@ fn part2(lines: List(String)) -> Int { |> list.map(with: get_seat_id) |> set.from_list - let occupied = fn(id) { set.contains(in: seat_ids, this: id) } + let occupied = set.contains(in: seat_ids, this: _) iter.find( in: iter.range(from: 1, to: 1023), diff --git a/aoc-2020-gleam/src/days/day07.gleam b/aoc-2020-gleam/src/days/day07.gleam index 2e7c890..fb5949c 100644 --- a/aoc-2020-gleam/src/days/day07.gleam +++ b/aoc-2020-gleam/src/days/day07.gleam @@ -1,10 +1,12 @@ import gleam/io +import gleam/int import gleam/list import gleam/pair import gleam/result as res import gleam/function as fun import gleam/map.{Map} import gleam/iterator.{Iterator} as iter +import ext/genericx as genx import ext/resultx as resx import ext/iteratorx as iterx import util/graph @@ -85,16 +87,18 @@ fn part1(lines: List(String)) -> Int { graph |> map.keys |> iter.from_list - |> iter.filter(for: fn(bag) { bag != special_bag }) + |> iter.filter(for: genx.different(_, than: special_bag)) |> iterx.count(satisfying: fn(start) { start |> graph.dfs(with: neighbours) - |> iter.any(fn(bag) { bag == special_bag }) + |> iter.any(satisfying: genx.equals(_, special_bag)) }) } fn part2(lines: List(String)) -> Int { - bag_count(of: special_bag, in: parse_graph(lines)) - 1 + special_bag + |> bag_count(in: parse_graph(lines)) + |> int.subtract(1) } pub fn run() -> Nil { diff --git a/aoc-2020-gleam/src/days/day08.gleam b/aoc-2020-gleam/src/days/day08.gleam index 6f074cf..9ef430b 100644 --- a/aoc-2020-gleam/src/days/day08.gleam +++ b/aoc-2020-gleam/src/days/day08.gleam @@ -1,5 +1,7 @@ import gleam/io +import gleam/int import gleam/list +import gleam/bool import gleam/set.{Set} import gleam/iterator.{Iterator} as iter import gleam/option.{None, Option, Some} as opt @@ -35,7 +37,7 @@ fn parse_program(lines: List(String)) -> Program { p.replace(p.literal("-"), with: -1), ]) |> p.then(p.int()) - |> p.map2(with: fn(sign, magnitude) { sign * magnitude }) + |> p.map2(with: int.multiply) let instr_parser = p.any(of: [ @@ -77,10 +79,14 @@ fn execution_result_helper( cpu: Cpu, visited: Set(Int), ) -> ExecutionResult { - case set.contains(visited, cpu.pc), fetch(from: program, with: cpu) { - True, _ -> InfiniteLoop(acc_before_second: cpu.acc) - _, None -> Termination(acc_after: cpu.acc) - _, Some(instr) -> + use <- bool.guard( + when: set.contains(visited, cpu.pc), + return: InfiniteLoop(acc_before_second: cpu.acc), + ) + + case fetch(from: program, with: cpu) { + None -> Termination(acc_after: cpu.acc) + Some(instr) -> execution_result_helper( program, execute(instr, on: cpu), diff --git a/aoc-2020-gleam/src/ext/genericx.gleam b/aoc-2020-gleam/src/ext/genericx.gleam new file mode 100644 index 0000000..57ce450 --- /dev/null +++ b/aoc-2020-gleam/src/ext/genericx.gleam @@ -0,0 +1,7 @@ +pub fn equals(left: a, right: a) -> Bool { + left == right +} + +pub fn different(left: a, than right: a) -> Bool { + left != right +} diff --git a/aoc-2020-gleam/src/ext/intx.gleam b/aoc-2020-gleam/src/ext/intx.gleam new file mode 100644 index 0000000..9f06850 --- /dev/null +++ b/aoc-2020-gleam/src/ext/intx.gleam @@ -0,0 +1,3 @@ +pub fn is_between(number: Int, min: Int, and max: Int) { + min <= number && number <= max +} diff --git a/aoc-2020-gleam/src/ext/resultx.gleam b/aoc-2020-gleam/src/ext/resultx.gleam index d354216..2067291 100644 --- a/aoc-2020-gleam/src/ext/resultx.gleam +++ b/aoc-2020-gleam/src/ext/resultx.gleam @@ -1,4 +1,6 @@ pub fn assert_unwrap(result: Result(t, _)) -> t { - let assert Ok(value) = result - value + case result { + Ok(value) -> value + _ -> panic + } } diff --git a/aoc-2020-gleam/src/util/parser.gleam b/aoc-2020-gleam/src/util/parser.gleam index 1ca2447..f143c92 100644 --- a/aoc-2020-gleam/src/util/parser.gleam +++ b/aoc-2020-gleam/src/util/parser.gleam @@ -150,10 +150,8 @@ pub fn ignore(parser: Parser(a)) -> Parser(Nil) { pub fn then(first: Parser(a), second: Parser(b)) -> Parser(#(a, b)) { create(fn(input) { - use parsed1 <- res.then(run(first, on: input)) - let #(value1, remaining1) = parsed1 - use parsed2 <- res.then(run(second, on: remaining1)) - let #(value2, remaining2) = parsed2 + use #(value1, remaining1) <- res.then(run(first, on: input)) + use #(value2, remaining2) <- res.then(run(second, on: remaining1)) Ok(#(#(value1, value2), remaining2)) }) |> labeled(with: first.label <> " |> then(" <> second.label <> ")") @@ -219,8 +217,7 @@ fn flat_map( with mapper: fn(a) -> Result(b, ParseError), ) -> Parser(b) { create(fn(input) { - use parsed <- res.then(run(parser, on: input)) - let #(value, remaining) = parsed + use #(value, remaining) <- res.then(run(parser, on: input)) value |> mapper |> res.map(with: fn(new_value) { #(new_value, remaining) }) @@ -248,7 +245,7 @@ fn succeeding(with value: a) -> Parser(a) { } fn failing(with error: ParseError) -> Parser(a) { - create(fn(_) { Error(error) }) + create(fun.constant(Error(error))) } fn lift2(function: fn(a, b) -> c) -> fn(Parser(a), Parser(b)) -> Parser(c) { @@ -308,8 +305,7 @@ pub fn str_of_many0(of parser: Parser(String)) -> Parser(String) { pub fn many1(of parser: Parser(a)) -> Parser(List(a)) { create(fn(input) { - use parsed <- res.then(run(parser, on: input)) - let #(value, rest) = parsed + use #(value, rest) <- res.then(run(parser, on: input)) let #(previous, rest) = do_zero_or_more(rest, with: parser) Ok(#([value, ..previous], rest)) }) |