Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exercise 010 #11

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions exercises/010-allergies/allergies.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
BEGIN {
FS = ","

map[1] = "eggs"
map[2] = "peanuts"
map[4] = "shellfish"
map[8] = "strawberries"
map[16] = "tomatoes"
map[32] = "chocolate"
map[64] = "pollen"
map[128] = "cats"
}

$2 == "list" { print get_items_list($1) }
$2 == "allergic_to" { print match(get_items_list($1), $3) ? "true" : "false" }

function get_highest_allergen(score) {
if (score <= 2) return score

highest_allergen = 1

while (1) {
if(highest_allergen * 2 > score) break

highest_allergen *= 2
}

return highest_allergen
}

function get_items_list(score) {
if (score <= 0) return ""

highest_allergen = get_highest_allergen(score)
result = ""

for(i = highest_allergen; i >= 1; i /= 2) {
if(score - i < 0) continue

score -= i

if(!map[i]) continue

result = map[i] (result ? "," result : "")
}

return result
}
3 changes: 3 additions & 0 deletions exercises/010-allergies/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 010-allergies

- https://exercism.org/tracks/awk/exercises/allergies
Loading
Loading