호두나무 공방/Exercism in Elixir

Pacman Rules - Exercism in Elixir

2021. 10. 24. 01:17

문제 보기

기초 문제인데 Pacman이라기에 약간 쫄았으나, 문제를 읽어 보니 그냥 논리 연산 문제였다. 문제를 해석할 수만 있으면 코드로 옮기는 것은 간단하다. 함수명이 ?로 끝날 수도 있다는 사실 역시 암시적으로 소개되고 있다.

defmodule Rules do
  def eat_ghost?(power_pellet_active, touching_ghost) do
    power_pellet_active and touching_ghost
  end

  def score?(touching_power_pellet, touching_dot) do
    touching_power_pellet or touching_dot
  end

  def lose?(power_pellet_active, touching_ghost) do
    not power_pellet_active and touching_ghost
  end

  def win?(has_eaten_all_dots, power_pellet_active, touching_ghost) do
    has_eaten_all_dots and not lose?(power_pellet_active, touching_ghost)
  end
end

다른 사람들의 답 중에는 패턴 매칭을 이용한 경우도 있었다. 오버라는 생각이 드는 한편 논리 연산마저도 패턴 매칭으로 대신할 수 있구나 싶어 새삼 신기했다.