With the help of Andreas Blass' comment I was able to solve the exercise. I will post the full solution here.
Lemma (Exercise 13.28). Let $\alpha \geq \omega$ be a countable ordinal. There exists $A \subseteq \omega$ such that $\alpha$ is countable in $L[A]$.
Proof. Let $f : \omega \to \alpha$ be one-to-one and onto. Define $W \subseteq \omega \times \omega$ by stipulating that $n \, W \, m$ iff $f(n) \in f(m)$. Then $W$ is a well-ordering of order-type $\alpha$. Since "$W$ is a well-ordering of order-type $\alpha$" is $\Delta_0$ relative to $\alpha$, we have that $L[W] \models$ "$W$ is a well-ordering of order-type $\alpha$", so $L[W] \models \alpha$ is countable.
Let $A = \Gamma(W) \subseteq \omega$, where $\Gamma$ is the canonical well-ordering of $\mathbf{ORD}^2$ (recall that $\Gamma(\omega_\alpha \times \omega_\alpha) = \omega_\alpha$ for all ordinals $\alpha$). Since $\Gamma$ is definable without parameters, we have that $A \in L[W]$ and $W \in L[A]$. Thus $L[A] = L[W]$. Note that $\mathsf{AC}$ is not used in this proof. $\square$
Lemma (Exercise 13.30). There exists $A \subseteq \omega_1$ such that $\omega_1 = \omega_1^{L[A]}$.
Proof. By the above lemma, for each $\alpha < \omega_1$ there exists $A_\alpha \subseteq \omega$ such that $\alpha$ is countable in $L[A_\alpha]$. Let $F$ be the function $\alpha \mapsto A_\alpha$ (this is a choice function, so $\mathsf{AC}$ is required), and let $A = \bigcup_{\alpha < \omega_1} \{\alpha\} \times F(\alpha) \subseteq \omega_1 \times \omega_1$. Then $L[A_\alpha] \subset L[A]$, and since "$X$ is countable'' is $\Sigma_1$, hence upward absolute, $\alpha$ is countable in $L[A]$. Thus $\omega_1^{L[A]} = \omega_1$.
To ensure that $A \subseteq \omega_1$, we simply consider the canonical well-ordering $\Gamma$ on $\omega_1 \times \omega_1$. Then $L[\Gamma(A)] = L[A]$, as we argued in the previous lemma. $\square$
Theorem (Exercise 13.31). If $\omega_2$ is not inaccessible in $L$, then there exists $A \subseteq \omega_1$ such that $\omega_1^{L[A]} = \omega_1$ and $\omega_2^{L[A]} = \omega_2$.
Proof. Since $L \models \mathsf{GCH}$, we have that in $L$, $\omega_2$ is inaccessible iff it is a regular limit cardinal (weakly inaccessible). As "$\alpha$ is a regular cardinal'' is $\Pi_1$we have that $\omega_2$ is a regular cardinal in $L$. Therefore, if $\omega_2$ is not inaccessible in $L$, then $\omega_2$ is a successor cardinal in $L$.
Let $\alpha < \omega_2$ be an ordinal such that $\omega_2 = \alpha^+$ in $L$. By repeating the proof in the first lemma but for $\alpha \geq \omega_1$ and $|\alpha| = \omega_1$, one may obtain an $A \subseteq \omega_1$ such that $|\alpha| = \omega_1^{L[A]}$ in $L[A]$. By the second lemma, one obtain another $B \subseteq \omega_1$ such that $\omega_1 = \omega_1^{L[B]}$ ($\mathsf{AC}$ is used here). Thus we have that $\omega_1^{L[A \times B]} = \omega_1$ and $\omega_2^{L[A \times B]} = \omega_2$. Now convert $A \times B \subseteq \omega_1 \times \omega_1$ to a subset of $\omega_1$ using the canonical well-ordering $\Gamma$. $\square$