aoc2023/day4.quox

55 lines
1.8 KiB
Plaintext
Raw Normal View History

2023-12-06 21:47:23 -05:00
-- introducing: let expressions!!!
-- i implemented them yesterday because this was just too annoying otherwise
load "list.quox"
load "string.quox"
load "io.quox"
def0 Line : ★ = List × List
-- i still don't have char literals. quox has no idea what "a char" is
def colon = char.from- 0x3A
def bar = char.from- 0x7C
def split-line : ω.String → Line =
λ line ⇒
letω line = snd (string.break (char.eq colon) line);
pair = string.break (char.eq bar) line;
got = fst pair; want = snd pair;
got = list.filter String string.not-null (string.split char.ws? got);
want = list.filter String string.not-null (string.split char.ws? want);
got = list.tail-or-nil String got;
want = list.tail-or-nil String want;
to-s = list.map String
(λ s ⇒ maybe.fold 0 (λ n ⇒ n) (string.to- s)) in
(to-s got, to-s want)
def mem : ω. → ω.(List ) → Bool =
λ n ⇒ list.foldlω Bool 'false (λ b n' ⇒ bool.or b (nat.eq n n'))
def all-members : ω.(List ) → ω.(List ) → List =
λ g w ⇒ list.filter (λ n ⇒ mem n w) g
def score-from-len : ω.(List ) → =
λ xs ⇒
maybe.foldω ( × List ) 0
(λ xxs ⇒ list.foldlω 1 (λ c _ ⇒ nat.times 2 c) (snd xxs))
(list.uncons xs)
def score : ω.Line → =
λ ln ⇒ score-from-len (all-members (fst ln) (snd ln))
def total-score : ω.(List Line) → =
list.foldlω Line 0 (λ n l ⇒ nat.plus n (score l))
#[main]
def main =
io.bindω String True
(io.read-fileω "in/day4")
(λ s ⇒ io.dump
(letω lines = string.split (char.eq char.newline) s;
lines = list.mapω String Line split-line lines in
total-score lines))