Skip to content

Add alphametics exercise (#312) - #443

Merged
glennj merged 14 commits into
exercism:mainfrom
kirtisrivastava22:exercise/alphametics
Aug 14, 2026
Merged

glennj merged 14 commits into
exercism:mainfrom
kirtisrivastava22:exercise/alphametics

Conversation

@kirtisrivastava22

Copy link
Copy Markdown
Contributor

Closes #312

Implements the alphametics solver using a column-by-column backtracking
approach (right-to-left, with carry propagation), which prunes far more
aggressively than brute-force permutation — verified against all canonical
test cases including the 199-addend case.

I don't have gawk/bats set up locally on this machine — happy to fix
anything CI flags.

Comment thread exercises/practice/alphametics/.meta/template.j2 Outdated
Comment thread config.json
Comment thread exercises/practice/alphametics/.meta/example.awk Outdated
Comment thread exercises/practice/alphametics/.meta/example.awk Outdated
Comment thread exercises/practice/alphametics/.meta/template.j2 Outdated
Comment thread exercises/practice/alphametics/.meta/template.j2
@test "{{ case.description }}" {
run awk -f alphametics.awk <<< "{{ case.input.puzzle }}"
assert_success
assert_output "{% if case.expected %}{% for letter, digit in case.expected.items() | sort %}{{ letter }}={{ digit }}{% if not loop.last %} {% endif %}{% endfor %}{% endif %}"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no output, we probably want to test for that explicitly with refute_output.

kirtisrivastava22 and others added 2 commits August 13, 2026 21:39
Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com>
Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com>
@kirtisrivastava22

Copy link
Copy Markdown
Contributor Author

Please review and tell any more modifications required

@IsaacG IsaacG left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the template used to generate the tests?

}
print out
} else {
print ""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't need to print an empty line when there's no solution :)

@kirtisrivastava22

Copy link
Copy Markdown
Contributor Author

Fixed the {% %} → {{ }} typo and idx → loop.first in the template, removed the empty-line print in example.awk, and regenerated the bats file. @IsaacG — to your question, the .bats file wasn't in sync with the template before this push; it should be now. If you have the generator command handy, happy to confirm by rerunning it rather than my manual version.

@IsaacG

IsaacG commented Aug 13, 2026

Copy link
Copy Markdown
Member

Fixed the {% %} → {{ }} typo and idx → loop.first in the template, removed the empty-line print in example.awk, and regenerated the bats file. @IsaacG — to your question, the .bats file wasn't in sync with the template before this push; it should be now. If you have the generator command handy, happy to confirm by rerunning it rather than my manual version.

./bin/generate_tests alphametrics

@kirtisrivastava22

Copy link
Copy Markdown
Contributor Author

Fixed the template's idx/case unpacking and the null-output branch in example.awk, then regenerated test-alphametics.bats with bin/generate_tests — diff picked up both the skip-guard lines and the refute_output change as expected.

@glennj

glennj commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

I was just holding back on approving until I was at a machine where I could see how long this took to run. But then I realized I could just look at the check. 10 sec is reasonable.

@glennj
glennj merged commit da1161d into exercism:main Aug 14, 2026
4 checks passed
@kirtisrivastava22
kirtisrivastava22 deleted the exercise/alphametics branch August 14, 2026 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement new exercise: Alphametics

3 participants