-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import gleam/int | ||
import gleam/io | ||
import gleam/list | ||
import gleam/option | ||
import gleam/string | ||
import nibble.{do, return} | ||
import nibble/lexer | ||
|
||
pub type T { | ||
M | ||
U | ||
L | ||
Open | ||
Close | ||
Comma | ||
Number(Int) | ||
Other | ||
} | ||
|
||
pub fn pt_1(input: String) { | ||
let assert Ok(out) = | ||
[ | ||
lexer.token("m", M), | ||
lexer.token("u", U), | ||
lexer.token("l", L), | ||
lexer.token("(", Open), | ||
lexer.token(")", Close), | ||
lexer.token(",", Comma), | ||
lexer.int(Number), | ||
lexer.keep(fn(the_string, _) { | ||
case | ||
the_string | ||
|> string.to_graphemes | ||
|> list.map(fn(char) { string.contains("mul(),0123456789", char) }) | ||
|> list.all(fn(x) { x }) | ||
{ | ||
True -> Error(Nil) | ||
False -> Ok(Other) | ||
} | ||
}), | ||
] | ||
|> lexer.simple() | ||
|> lexer.run(input, _) | ||
|
||
let int_parser = { | ||
// Use `take_map` to only consume certain kinds of tokens and transform the | ||
// result. | ||
use tok <- nibble.take_map("expected number") | ||
case tok { | ||
Number(n) -> option.Some(n) | ||
_ -> option.None | ||
} | ||
} | ||
|
||
let parser = { | ||
use acc <- nibble.loop(0) | ||
nibble.one_of([ | ||
{ | ||
use _ <- do(nibble.token(M)) | ||
use _ <- do(nibble.token(U)) | ||
use _ <- do(nibble.token(L)) | ||
use _ <- do(nibble.token(Open)) | ||
use x <- do(int_parser) | ||
use _ <- do(nibble.token(Comma)) | ||
use y <- do(int_parser) | ||
use _ <- do(nibble.token(Close)) | ||
|
||
{ x * y } | ||
|> return | ||
} | ||
|> nibble.backtrackable | ||
|> nibble.map(int.add(acc, _)) | ||
|> nibble.map(nibble.Continue), | ||
// if that fails take everything until we find another `m` | ||
nibble.take_until1("wtf bro", fn(tok) { tok == M }) | ||
|> nibble.replace(acc) | ||
|> nibble.map(nibble.Continue), | ||
// we need to explicitly handle the case where we have an `m` | ||
// but `mul_parser` failed | ||
nibble.token(M) | ||
|> nibble.replace(acc) | ||
|> nibble.map(nibble.Continue), | ||
// we reached the end, return our list | ||
nibble.eof() | ||
|> nibble.map(fn(_) { acc }) | ||
|> nibble.map(nibble.Break), | ||
]) | ||
} | ||
|
||
let assert Ok(out) = nibble.run(out, parser) | ||
|
||
out | ||
} | ||
|
||
pub fn pt_2(input: String) { | ||
todo as "part 2 not implemented" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import gleam/bool | ||
import gleam/dict | ||
import gleam/list | ||
import gleam/string | ||
|
||
pub type Pos { | ||
Pos(row: Int, col: Int) | ||
} | ||
|
||
pub type Displacement { | ||
Displacement(row: Int, col: Int) | ||
} | ||
|
||
pub type Letter { | ||
X | ||
M | ||
A | ||
S | ||
} | ||
|
||
pub fn parse(input: String) -> dict.Dict(Pos, Letter) { | ||
use acc, line, row <- list.index_fold(string.split(input, "\n"), dict.new()) | ||
use acc, letter, col <- list.index_fold(string.to_graphemes(line), acc) | ||
case letter { | ||
"X" -> X | ||
"M" -> M | ||
"A" -> A | ||
"S" -> S | ||
_ -> panic as { "unexpected letter: " <> letter } | ||
} | ||
|> dict.insert(acc, Pos(row:, col:), _) | ||
} | ||
|
||
fn verify( | ||
sequence: List(Letter), | ||
pos: Pos, | ||
data: dict.Dict(Pos, Letter), | ||
direction: Displacement, | ||
) -> Bool { | ||
case sequence { | ||
[] -> True | ||
[head, ..tail] -> | ||
case dict.get(data, pos) { | ||
Ok(letter) if letter == head -> | ||
verify( | ||
tail, | ||
Pos(row: pos.row + direction.row, col: pos.col + direction.col), | ||
data, | ||
direction, | ||
) | ||
_ -> False | ||
} | ||
} | ||
} | ||
|
||
pub fn pt_1(input: dict.Dict(Pos, Letter)) { | ||
use acc, start, letter <- dict.fold(input, 0) | ||
use <- bool.guard(when: letter != X, return: acc) | ||
acc | ||
+ list.count( | ||
[ | ||
// down to the left | ||
Displacement(1, -1), | ||
// down | ||
Displacement(1, 0), | ||
//down to the right | ||
Displacement(1, 1), | ||
// to the right | ||
Displacement(0, 1), | ||
// to the left | ||
Displacement(0, -1), | ||
// up to the left | ||
Displacement(-1, -1), | ||
// up | ||
Displacement(-1, 0), | ||
// up to the right | ||
Displacement(-1, 1), | ||
], | ||
verify([X, M, A, S], start, input, _), | ||
) | ||
} | ||
|
||
pub fn pt_2(input: dict.Dict(Pos, Letter)) { | ||
use acc, a_position, letter <- dict.fold(input, 0) | ||
use <- bool.guard(when: letter != A, return: acc) | ||
acc | ||
+ list.count( | ||
[ | ||
// M S | ||
// A | ||
// M S | ||
#(Displacement(row: 1, col: 1), Displacement(row: -1, col: 1)), | ||
// M M | ||
// A | ||
// S S | ||
#(Displacement(row: 1, col: 1), Displacement(row: 1, col: -1)), | ||
// S M | ||
// A | ||
// S M | ||
#(Displacement(row: 1, col: -1), Displacement(row: -1, col: -1)), | ||
// S S | ||
// A | ||
// M M | ||
#(Displacement(row: -1, col: 1), Displacement(row: -1, col: -1)), | ||
], | ||
fn(starts) { | ||
verify( | ||
[M, A, S], | ||
Pos( | ||
row: a_position.row - { starts.0 }.row, | ||
col: a_position.col - { starts.0 }.col, | ||
), | ||
input, | ||
starts.0, | ||
) | ||
&& verify( | ||
[M, A, S], | ||
Pos( | ||
row: a_position.row - { starts.1 }.row, | ||
col: a_position.col - { starts.1 }.col, | ||
), | ||
input, | ||
starts.1, | ||
) | ||
}, | ||
) | ||
} |