Skip to content

Instantly share code, notes, and snippets.

@jarble
Created September 20, 2023 22:40
Show Gist options
  • Save jarble/62ab089629ddb2587b0cd14d2baabaac to your computer and use it in GitHub Desktop.
Save jarble/62ab089629ddb2587b0cd14d2baabaac to your computer and use it in GitHub Desktop.
Hamming distance in MiniZinc
function int: hamming_distance(array[int] of int: arr1, array[int] of int: arr2) =
sum([if arr1[i] != arr2[i] then 1 else 0 endif | i in index_set(arr1)]);
% Example usage
array[int] of int: arr1 = [107, 97, 114, 111, 108, 105, 110];
array[int] of int: arr2 = [107, 97, 116, 104, 114, 105, 110];
int: distance = hamming_distance(arr1, arr2);
output ["The Hamming distance is ", show(distance)];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment