As one learns programming, how useful it is to spend time to solve hard problems? Like, I saw a problem solving site where one task is for example to find a regular expression for decimal numbers divisible by 9 or do regular expression for a particular problem that is as small as possible (in sense of number of characters in the expression). Is there worth of spending time to try those kind of problems or should one always try to solve problems as easily as possible?
It is probably a mistake to always do one or the other. If you are learning a hard task (becoming a competent programmer), it isn't helpful if you mix in another hard task as well (hard problems). Initially, it is good to write programs for problems that you already understand well, so that you can focus on the creation. A program has both form and content. If you work on very hard problems exclusively, it is possible that you will wind up just hacking a solution together without any elegance.
But if you never solve hard problems it won't become clear to you why advanced programming structures and paradigms are needed.
My advice would be to start out your programming education with fairly easy problems - that you can get your mind around easily. But mix in a harder problem or two as you go. Then when you become more proficient with the forms, move to harder problems and occasional very hard ones.
You are stretching your brain in two dimensions here. Eventually you want it to be very flexible. But you can stretch more in one dimension than the other at a time. If your problems are so difficult that you get frustrated then you may not make much progress on any dimension.
And, of course, problems that seem hard to a novice programmer, may not seem very hard to a pro. You learn a lot of tricks-of-the-trade as you go along.
Also the problem that you mentioned doesn't result in a very big program. It is a bit tricky, of course, but very small as programs go. You want to reach a place where you can intelligently create very large programs.
This question reminds me of the thought-provoking question: Would you rather slay one horse-sized duck or 100 duck-sized horses.
For beginners, I recommend solving lots of simple problems. This practice is crucial to learning, because all the programming concepts are new, and you need to experience them. For example, sometimes it takes several programs that use a single for loop to really understand how a for loop works.
For advanced programmers, the hard problems can be beneficial. But what makes them hard? Is it the length of the program? Is it low-level or advanced concepts (e.g., threads). I would accept a very difficult assignment if it meant learning something useful or desirable.
In general, I tell my students to practice writing lots of programs, no matter how small/large, easy/difficult. Any program is better than not writing one.
Is there worth of spending time to try those kind of problems or should one always try to solve problems as easily as possible?
You should work on problems that are fun and interesting to you, and that are slightly harder than the problems you're already comfortable with.
Programming is a craft, similar to playing a musical instrument or playing a sport. You have to practice programming to improve, exactly the way you have to practice playing the piano or throwing a baseball.
When learning to play the piano, do you always play the songs that are easiest for you, or do you always try to play the hardest song ever written? When learning how to hit a baseball, do you only practice with slow underhand tosses, or do you only practice with 100 MPH curveballs?
The answer is none of the above. You practice by getting a little bit better every day. For piano, maybe you learn a new song that's a little bit more complicated than the song you learned last month. For baseball, maybe you practice with somebody who throws the ball a little bit faster over time.
The same is true of programming. You have to meet yourself where you are. Don't spend too much time on stuff you already have memorized (although, there is some merit to programming kata), but don't spend too much time struggling on impossible problems either.
In other words, you should try to keep yourself just slightly outside of your own comfort zone. You also might want to read about the zone of proximal development.
Specifically, here's what I recommend:
Give yourself an achievable goal. The goal should be fun and interesting to you, and it should be achievable in a reasonable amount of time. A single weekend is a good starting point. Finish that goal, and then iterate. Explore topics that you're interested in, but do it with short achievable steps.