Last active
February 11, 2018 07:24
-
-
Save mankyKitty/9dad4869d0a16e631847069b9efb6a31 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(* | |
** | |
** A template for single-file ATS programs | |
** | |
*) | |
(* ****** ****** *) | |
// | |
#include "share/atspre_define.hats" | |
#include "share/atspre_staload.hats" | |
// | |
(* ****** ****** | |
--- Part Two --- | |
You notice a progress bar that jumps to 50% completion. Apparently, the door | |
isn't yet satisfied, but it did emit a star as encouragement. The | |
instructions change: | |
Now, instead of considering the next digit, it wants you to consider the | |
digit halfway around the circular list. That is, if your list contains 10 | |
items, only include a digit in your sum if the digit 10/2 = 5 steps forward | |
matches it. Fortunately, your list has an even number of elements. | |
For example: | |
1212 produces 6: the list contains 4 items, and all four digits match the digit 2 items ahead. | |
1221 produces 0, because every comparison is between a 1 and a 2. | |
123425 produces 4, because both 2s match each other, but no other digit has a match. | |
123123 produces 12. | |
12131415 produces 4. | |
*) | |
fun everything | |
{i,m,n:nat} // | i < m && m - i >= 0 && m - i < n && (i + 1) < m} | |
{s:pos | ((i + s) > m && s - (m - i) < m) || i + s < m} // | (i + s) < m } | |
.<n>. | |
( | |
all: list(int,m), | |
len: int m, | |
shift: int s, | |
xs: list(int, n), | |
ix: int i, | |
acc: int | |
): int = | |
case+ xs of | |
| list_nil _ => acc | |
| list_cons (x1, xs2) => | |
let | |
val inc = succ ix | |
val loc = ix + shift | |
val flipLoc = shift - (len - ix) | |
val nxt = if loc > len | |
then list_get_at(all, flipLoc) | |
else list_get_at(all, loc) | |
val acc1 = if x1 = nxt then (acc + x1) else acc | |
in | |
everything( | |
all, | |
len, | |
shift, | |
xs2, | |
inc, | |
acc1 | |
) | |
end | |
(* ****** ****** *) | |
#define :: list_cons | |
val xs = (1 :: 2 :: 1 :: 2 :: list_nil()) | |
// val ys = sum_doubles( (1 :: 2 :: 2 :: 1 :: list_nil()) ) | |
val () = println! (everything( xs, list_length(xs), list_length(xs) / 2, xs, 0, 0)) | |
implement main0 () = () // a dummy implementation for [main] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment