Skip to content

Commit

Permalink
Add largest-series-product exercise (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Nov 25, 2024
1 parent 81f61d4 commit d948f8e
Show file tree
Hide file tree
Showing 13 changed files with 3,682 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "largest-series-product",
"name": "Largest Series Product",
"uuid": "26f31fed-b645-454e-86d6-c9db6d637f96",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "luhn",
"name": "Luhn",
Expand Down
26 changes: 26 additions & 0 deletions exercises/practice/largest-series-product/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Instructions

Your task is to look for patterns in the long sequence of digits in the encrypted signal.

The technique you're going to use here is called the largest series product.

Let's define a few terms, first.

- **input**: the sequence of digits that you need to analyze
- **series**: a sequence of adjacent digits (those that are next to each other) that is contained within the input
- **span**: how many digits long each series is
- **product**: what you get when you multiply numbers together

Let's work through an example, with the input `"63915"`.

- To form a series, take adjacent digits in the original input.
- If you are working with a span of `3`, there will be three possible series:
- `"639"`
- `"391"`
- `"915"`
- Then we need to calculate the product of each series:
- The product of the series `"639"` is 162 (`6 × 3 × 9 = 162`)
- The product of the series `"391"` is 27 (`3 × 9 × 1 = 27`)
- The product of the series `"915"` is 45 (`9 × 1 × 5 = 45`)
- 162 is bigger than both 27 and 45, so the largest series product of `"63915"` is from the series `"639"`.
So the answer is **162**.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Introduction

You work for a government agency that has intercepted a series of encrypted communication signals from a group of bank robbers.
The signals contain a long sequence of digits.
Your team needs to use various digital signal processing techniques to analyze the signals and identify any patterns that may indicate the planning of a heist.
19 changes: 19 additions & 0 deletions exercises/practice/largest-series-product/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"largest_series_product.s"
],
"test": [
"largest_series_product_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.",
"source": "A variation on Problem 8 at Project Euler",
"source_url": "https://projecteuler.net/problem=8"
}
65 changes: 65 additions & 0 deletions exercises/practice/largest-series-product/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.equ INVALID_CHARACTER, -1
.equ NEGATIVE_SPAN, -2
.equ INSUFFICIENT_DIGITS, -3

.text
.globl largest_product

/* extern int64_t largest_product(int span, const char *digits); */
largest_product:
mov x2, x1 /* input pointer */

.scan:
ldrb w3, [x2], #1 /* load byte, with post-increment */
cbz w3, .check_length

sub w3, w3, #'0'
cmp w3, #10
blo .scan /* unsigned < */

mov x0, INVALID_CHARACTER
ret

.check_length:
sub x2, x2, #1 /* address of null terminator */

cmp w0, wzr /* compare with zero */
blt .negative_span

sub x3, x2, x1 /* number of digits */
cmp x3, x0
blt .insufficient_digits

mov x4, x0 /* span */
mov x0, xzr /* largest product */

.series:
add x5, x1, x4 /* end of series */
mov x6, #1 /* current product */
mov x7, x1 /* input pointer */

.digit:
cmp x7, x5
beq .update_largest

ldrb w8, [x7], #1 /* load digit */
sub w8, w8, #'0'
mul x6, x6, x8
b .digit

.update_largest:
cmp x6, x0
csel x0, x6, x0, hi
cmp x7, x2
add x1, x1, #1 /* start of next series */
bne .series

ret

.negative_span:
mov x0, NEGATIVE_SPAN
ret

.insufficient_digits:
mov x0, INSUFFICIENT_DIGITS
ret
60 changes: 60 additions & 0 deletions exercises/practice/largest-series-product/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[7c82f8b7-e347-48ee-8a22-f672323324d4]
description = "finds the largest product if span equals length"

[88523f65-21ba-4458-a76a-b4aaf6e4cb5e]
description = "can find the largest product of 2 with numbers in order"

[f1376b48-1157-419d-92c2-1d7e36a70b8a]
description = "can find the largest product of 2"

[46356a67-7e02-489e-8fea-321c2fa7b4a4]
description = "can find the largest product of 3 with numbers in order"

[a2dcb54b-2b8f-4993-92dd-5ce56dece64a]
description = "can find the largest product of 3"

[673210a3-33cd-4708-940b-c482d7a88f9d]
description = "can find the largest product of 5 with numbers in order"

[02acd5a6-3bbf-46df-8282-8b313a80a7c9]
description = "can get the largest product of a big number"

[76dcc407-21e9-424c-a98e-609f269622b5]
description = "reports zero if the only digits are zero"

[6ef0df9f-52d4-4a5d-b210-f6fae5f20e19]
description = "reports zero if all spans include zero"

[5d81aaf7-4f67-4125-bf33-11493cc7eab7]
description = "rejects span longer than string length"

[06bc8b90-0c51-4c54-ac22-3ec3893a079e]
description = "reports 1 for empty string and empty product (0 span)"

[3ec0d92e-f2e2-4090-a380-70afee02f4c0]
description = "reports 1 for nonempty string and empty product (0 span)"

[6d96c691-4374-4404-80ee-2ea8f3613dd4]
description = "rejects empty string and nonzero span"

[7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74]
description = "rejects invalid character in digits"

[5fe3c0e5-a945-49f2-b584-f0814b4dd1ef]
description = "rejects negative span"
include = false

[c859f34a-9bfe-4897-9c2f-6d7f8598e7f0]
description = "rejects negative span"
reimplements = "5fe3c0e5-a945-49f2-b584-f0814b4dd1ef"
36 changes: 36 additions & 0 deletions exercises/practice/largest-series-product/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.equ INVALID_CHARACTER, -1
.equ NEGATIVE_SPAN, -2
.equ INSUFFICIENT_DIGITS, -3

.text
.globl largest_product

largest_product:
ret
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "vendor/unity.h"

#include <stdint.h>

#define INVALID_CHARACTER -1
#define NEGATIVE_SPAN -2
#define INSUFFICIENT_DIGITS -3

extern int64_t largest_product(int span, const char *digits);

void setUp(void) {
}

void tearDown(void) {
}

void test_finds_the_largest_product_if_span_equals_length(void) {
TEST_ASSERT_EQUAL_INT(18, largest_product(2, "29"));
}

void test_can_find_the_largest_product_of_2_with_numbers_in_order(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(72, largest_product(2, "0123456789"));
}

void test_can_find_the_largest_product_of_2(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(48, largest_product(2, "576802143"));
}

void test_can_find_the_largest_product_of_3_with_numbers_in_order(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(504, largest_product(3, "0123456789"));
}

void test_can_find_the_largest_product_of_3(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(270, largest_product(3, "1027839564"));
}

void test_can_find_the_largest_product_of_5_with_numbers_in_order(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(15120, largest_product(5, "0123456789"));
}

void test_can_get_the_largest_product_of_a_big_number(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(23520, largest_product(6, "73167176531330624919225119674426574742355349194934"));
}

void test_reports_zero_if_the_only_digits_are_zero(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(0, largest_product(2, "0000"));
}

void test_reports_zero_if_all_spans_include_zero(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(0, largest_product(3, "99099"));
}

void test_rejects_span_longer_than_string_length(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(INSUFFICIENT_DIGITS, largest_product(4, "123"));
}

void test_reports_1_for_empty_string_and_empty_product_0_span(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(1, largest_product(0, ""));
}

void test_reports_1_for_nonempty_string_and_empty_product_0_span(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(1, largest_product(0, "123"));
}

void test_rejects_empty_string_and_nonzero_span(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(INSUFFICIENT_DIGITS, largest_product(1, ""));
}

void test_rejects_invalid_character_in_digits(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(INVALID_CHARACTER, largest_product(2, "1234a5"));
}

void test_rejects_negative_span(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(NEGATIVE_SPAN, largest_product(-1, "12345"));
}

void test_large_span(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(131681894400, largest_product(17, "98765432123456789"));
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_finds_the_largest_product_if_span_equals_length);
RUN_TEST(test_can_find_the_largest_product_of_2_with_numbers_in_order);
RUN_TEST(test_can_find_the_largest_product_of_2);
RUN_TEST(test_can_find_the_largest_product_of_3_with_numbers_in_order);
RUN_TEST(test_can_find_the_largest_product_of_3);
RUN_TEST(test_can_find_the_largest_product_of_5_with_numbers_in_order);
RUN_TEST(test_can_get_the_largest_product_of_a_big_number);
RUN_TEST(test_reports_zero_if_the_only_digits_are_zero);
RUN_TEST(test_reports_zero_if_all_spans_include_zero);
RUN_TEST(test_rejects_span_longer_than_string_length);
RUN_TEST(test_reports_1_for_empty_string_and_empty_product_0_span);
RUN_TEST(test_reports_1_for_nonempty_string_and_empty_product_0_span);
RUN_TEST(test_rejects_empty_string_and_nonzero_span);
RUN_TEST(test_rejects_invalid_character_in_digits);
RUN_TEST(test_rejects_negative_span);
RUN_TEST(test_large_span);
return UNITY_END();
}
Loading

0 comments on commit d948f8e

Please sign in to comment.