existence of certain continuous function 
Let $(X,d)$ be a metric space and $\emptyset \neq A\subseteq X.$ Define for $x\in X,\mathrm{dist}(x,A) := \inf\{d(x,a) : a\in A\}.$


*

*Show that there is a sequence $U_1, U_2,\cdots$ of open subsets in $(X,d)$ for which $\overline{A} = \cap_{n=1}^{\infty} U_n.$

*Let $\emptyset\neq K\subset X$ be a compact set with $K\cap \overline{A} = \emptyset.$ Show that there is a continuous function $g : X\to [0,1]$ so that $g(x) = 0$ for $x\in A$ and $g(y) = 1$ for $y \in K.$
For the first part, define for each $n\in\mathbb{N}, U_n := \{x \in X : \mathrm{dist}(x,A) < \frac{1}n\}.$  We claim that each $U_n$ is open. Fix $n\in\mathbb{N}.$ Let $y \in U_n.$ Then $\mathrm{dist}(y, A) =\inf\{d(x,a) : a \in A\} < \frac{1}n,$ so $\exists a \in A$ so that $d(y,a) < \frac{1}n.$ Consider the open ball $B(y, \epsilon), \epsilon := \frac{1}{n} -d(y,a) > 0.$ Then for any $x \in B(y, \epsilon), d(x,a)\leq d(x,y) + d(y,a) < \epsilon + d(y,a) = \frac{1}n$ and hence $\mathrm{dist}(x,A) \leq d(x,a) < \frac{1}n,$ so that $B(y,\epsilon)\subseteq U_n.$
Now we claim that $\cap_{n\in \mathbb{N}}U_n = \overline{A}.$ Let $x \in \cap_{n\in \mathbb{N}} U_n.$ Then for all $n\in\mathbb{N}, \mathrm{dist}(x,A) < \frac{1}n$ and since $\mathrm{dist}(y,A)\geq 0$ for all $y \in X, $ this implies $ \mathrm{dist}(x,A) = 0.$ One can then show using the definition of infimum that $x\in \overline{A}.$ Thus, $\cap_{n\in\mathbb{N}} U_n \subseteq \overline{A}.$ Now let $b\in \overline{A}.$ Then $\mathrm{dist}(b,A) = 0$ so $b \in \cap_{n\in\mathbb{N}}U_n.$ Thus, $\overline{A} = \cap_{n\in\mathbb{N}} U_n.$

Is this correct?

For the second part, I should make use of the properties of compact sets (e.g. being closed and bounded) and the fact that $K$ and $\overline{A}$ are disjoint. I know the function $f : X\to \mathbb{R}, f(x) = \mathrm{dist}(x,A)$ for all $x$ is continuous. However, I'm not sure how to define the function $g.$
 A: The first part is correct. For the second part, you can define
$g(x)=\min\left(1,\frac{d(A,x)}{d(K,x)}\right)$.
You can check the continuity of this function in the closed sets $\{x\in X;\frac{d(A,x)}{d(K,x)}\leq1\}$ and in $\{x\in X;\frac{d(A,x)}{d(K,x)}\geq1\}$ separately (it´s easy in both cases). And then by the glueing lemma $g$ is continuous.
In fact, it´s enough for $K$ to be closed and disjoint with $A$ for this function to be continuous.
A: A basic fact that is handy in metric spaces $(X,d)$: for a given non-empty set $A$, the function $f_A: X \to \Bbb R, x \to d(x,A)$ is continuous.
This follows from $|f_A(x)-f_A(y)| \le d(x,y)$, which in turn easily follows from triangle inequality arguments. The OP already knows this fact it seems.
Another standard fact is that $x \in \overline{A}$ iff $d(x,A)= 0$, for all $x \in X$.
It follows that for a closed set $A$, so where $A = f_A^{-1}[\{0\}]$, we can write
$$A = \bigcap_n U_n, \text{ where } U_n = f^{-1}[(-\frac1n, \frac1n)] \text{ is open in } X$$
This is quite close to the idea in the OP's proof. But we don't need a separate proof of the openness of te $U_n$ as they are the inverse images of open sets in $\Bbb R$ under a continous function.
If $K \cap \overline{A} = \emptyset$, we can conclude: $f_A(x) >0$ for all $x \in K$, by the above fact and $f_A$ restricted to $K$ thus assumes some minimum $m_0 >0$ (by compactness the continuous function $f_A$ has a minimum and a maximum on $K$) and then we can define a continuous (piecewise linear even) function $h$ (helper function, hence $h$) on $[0, +\infty) \to [0,1]$ that maps  $[0,\frac12m_0]$ to $0$, $[\frac12m_0,m_0]$ linearly from $0$ to $1$, and is $1$ on $[m_0, +\infty)$. Then $g:=h \circ f_A$ is as required: all points in $A$ are mapped to $0$ by $f_A$ and still to $0$ by $h$, and everything in $K$ is mapped into $[m_0,\rightarrow)$ (as $m_0$ was the minimum of $f_A$ on $K$) and then to $1$ by $h$, etc. The composition of continuous maps is continuous so we're done.
