Skip to content

Instantly share code, notes, and snippets.

@mankyKitty
Last active December 22, 2017 00:58
Show Gist options
  • Save mankyKitty/81a687d364370c80f0b1ffbda5dc0a7a to your computer and use it in GitHub Desktop.
Save mankyKitty/81a687d364370c80f0b1ffbda5dc0a7a to your computer and use it in GitHub Desktop.
Day1 AoC Attempt using ATS2 <3
#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"
// 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.
// 1111 produces 4 because each digit (all 1) matches the next.
// 1234 produces 0 because no digit matches the next.
// 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
fn fold_stylez { n: int } ( last: int, xs: list(int, n) ): int =
let
implement list_foldleft$fopr<(int,int)><int> (inp, n) =
if inp.0 = n then (n, g0int_add_int((inp.1), n)) else (n, (inp.1))
in
(list_foldleft<(int,int)> ( xs, (last, 0) )).1
end
fn sum_doubles { n: int } ( xs: list(int, n) ): int =
if (list_is_nil(xs)) then 0
else case xs of list_cons (x0, xs0) =>
g0int_add_int(
(if (x0 = list_last_exn(xs0)) then x0 else 0),
fold_stylez(x0, xs0)
)
// This is nice.
#define :: list_cons
val x = sum_doubles( (1 :: 1 :: 2 :: 2 :: list_nil()) )
val y = sum_doubles( (1 :: 1 :: 1 :: 1 :: list_nil()) )
val z = sum_doubles( (1 :: 2 :: 3 :: 4 :: list_nil()) )
val a = sum_doubles( (9 :: 1 :: 2 :: 1 :: 2 :: 1 :: 2 :: 9 :: list_nil()) )
val () = println!(x)
val () = println!(y)
val () = println!(z)
val () = println!(a)
implement main0 (): void = ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment