Advent of Code 2023

I also found Day 6 much easier than day 5. By using a little bit of maths we can implement a “no loops required” solution using quadratic inequalities…

Day 6 spoilers

If we name the time that the player holds in the button variable x, then we want to solve the following equation for all values of x…

(time - x) * x > distance

Using the quadratic formula we can solve x and get two answers a and b like follow…

a = (time - Sqrt(time * time - 4 * distance)) / 2
b = (time - Sqrt(time * time - 4 * distance)) / 2

Your wins given the hold time and a record of distance will now be…

wins = Ceil(b) - Floor(a) - 1

Final CalculateWins function…

Function CalculateWins(time As Integer, distance As Integer) As Integer
Var a As Double
Var b As Double
Var wins As Integer

a = (time - Sqrt(time * time - 4 * distance)) / 2
b = (time + Sqrt(time * time - 4 * distance)) / 2
wins = Ceil(b) - Floor(a) - 1

return wins
End Function

Runs 0.05ms in IDE.

Show off. :grinning:

But isn’t that the same formula for a and b?

Haha… I’m just glad I didn’t end up with a slow 15s run like with yesterday’s puzzle :slight_smile:

I fixed the formula, a has a -, and b has a +

1 Like
Day 6 Discussion

I used the same algabraic method as Alwyn. I’d noticed immediately that the distance v. time function would be a parabola, and so it could be solved with a quadratic formula.

Interesting. I ran mine in the IDE and it only took 140 microseconds. And that included the time to parse the input data (but not any output time).

PrintLine "Day 6 Part 2"
LoadData
dim et As Double = Microseconds
dim raceTime() As string = split(CompressWhitespace(parseTxt(pzlData,"Time:",EndOfLine))," ")
dim Distance() As string = split(CompressWhitespace(parseTxt(pzlData,"Distance:",EndOfLine))," ")
dim p As Integer = 1
for i as Integer = 0 to raceTime.Ubound
  dim c As Integer = 0
  dim t_r As Integer = raceTime(i).ToInteger
  dim d As double = Distance(i).ToInteger+1
  'calc range of record beaters
  dim t As double = sqrt(t_r^2-4*d)
  'calc lower and upper limits of charge time to beat record
  dim t1 As double = ((t_r-t)/2)
  dim t2 As double = ((t_r+t)/2)
  'calc the count of ways to beat record
  dim count As integer =  floor(t2) - ceil(t1) +1 
  p = p*(count\1)
next i
et = Microseconds-et
PrintLine "Part 1 solution: "+str(p)
PrintLine "Solution time: "+str(et)+" microseconds"

I left the input data in arrays as per Part 1 even though there was only a single race to calculate. So, probably could have made it faster yet.

1 Like
Day 6 Discussion

Robert, in your code… why are you doing an integer divide by 1 on count. Couldn’t you just have it as…

p = p * count

Perhaps for next year’s AoC event we could try and convince the Xojo marketing team to set up an official Xojo leaderboard on AoS, and sponsor a 1-year Xojo Pro license as the first prize to the winner?

It could be a great way to motivate people to join the AoC event, while showcasing Xojo as a programming language?

I see that is what Kotlin is doing on AoC to bring attention to their programming language, and attract new users.

@Dana_Brown ^

1 Like
Day 6 discussion

In an earlier version of the code I had ‘count’ declared as a double, and I couldn’t remember if Xojo had an Int() function, so I just did an integer divide by one to truncate it. Sloppy, but I was in a hurry. So yes, that can be removed.

Your hurriedness paid off Robert, because you completed the puzzle in a very respectable time :slight_smile:

Took me a while to get the maths right :slight_smile:

Day 6 discussion

I didn’t have any problem with the quadratic formula, but converting the floating point results back into integers had me scratching my head for a while. Had to draw a diagram in order to figure out where to use floor() and where to use ceil().

I think you mean @April_Baynes . Dana is no longer with Xojo.

2 Likes

Oh, I didn’t realize Dana moved on… been a while since I’ve been active on the Xojo forums.

Thank you for rectifying Kem.

Such a competition would be a bit unfair for those of us in Europe, though, because we’d have to get up very early in the morning to be able to compete.

Also, as soon as there are prizes involved, there’s a possibility that people cheat by looking up solutions and then re-coding them in Xojo instead of doing the full works.

Might still be worth considering an “official” Xojo competition regardless.

You are making a few valid points that I didn’t think of Thomas.

Perhaps one could have a leaderboard/prize per region to accommodate for the time differences, but I guess there won’t be an easy way to deal with any kind of cheating. Might still be worth it though, because cheating or not… if it raises awareness about Xojo as a programming language, and get people to try the language it is a win for Xojo.

If Xojo had to host their own competition I would definitely join, but it would be difficult to reproduce the level of puzzles that AoS created. I can only imagine that a lot of thought, time and testing goes into creating these puzzles.

I found Day 7 to be a nice change. It was more in line with the kind of programming that I typically do. However, my code had several obscure errors that still managed to give correct answers for part 1 and the example data in part 2, but then gave the wrong answer for the full part 2 data. Took a while to chase those down.

This year I’m a bit behind the schedule. With a little 10 months old girl around I don’t have too much spare time, so I’ve completed Day 5 and 6 by brute force.

No regrets :rofl:

About doing a competition with AoC, that would be nice and fun. The leaderboard isn’t fair, because of the different timezones… and can be cheated (we’ve seen this last year), so we’ll have to think about a different way.

Yeah… day 7’s puzzle feels a lot more like logical programming than the previous puzzles.

I definitely like the diversity of these puzzles.

Had a business function today so only had time to complete part 1 of day 7… I’m going to tackle part 2 now.

I hear you Ricardo… my boy is now 13 months old… babies definitely command their slice of time.

1 Like

I’ve been sick for the past few days so decided to forgo AoC in favor of sleep. I haven’t even looked at day 7 yet, and given work demands, may have to circle back later.

1 Like

Hope you feel better soon Kem.

2 Likes

Get well soon!

1 Like