PDA

View Full Version : Advent of Code, 2017



Petr Schreiber
11-12-2017, 21:54
Hello,

Advent of Code is a much healthier alternative to advent calendar.

0% chocolate, 100% code.

You are offered new code challenge each day, till Xmas.
This is an excellent opportunity to get better in algorithmic thinking as well as in mastering thinBASIC!

For me, it is also an opportunity to compare thinBASIC to other languages to identify possible weak points we can work on in future.

It is fun and you will reach Xmas not few kilos heavier (as advent calendar can do), but few IQ points smarter instead. Let's play with it :)

Links
Official Advent of Code challenge page: http://adventofcode.com/2017

My GitHub (in progress)
- with thinBASIC solutions: https://github.com/petrSchreiber/advent-of-code-2017-thinbasic
- with Python 3 solutions: https://github.com/petrSchreiber/advent-of-code-2017-python3 (to see what other interpreters offer... and we can steal :bom:)


Petr

ErosOlmi
12-12-2017, 11:56
Always great ideas from Petr.

I work for a company that makes 50% of global annual income in 4 months from Sept to Dec so in this period my time is very limited.
But I will follow with great interest getting ideas to implement thinBasic and if I will have some time I will do my versions of the scripts.

Ciao
Eros

PS: First of all I hate to see py version of script 01 is much shorter than thninBasic one.
:mad::mad:
:D

ErosOlmi
12-12-2017, 15:58
In Day 01 what about:

Dim lInput(len(input)) as string * 1 at strptr(input)

This remove the need of all MID$ functions and also takes advance of the fact that thinBasic automatically convert strings to numbers if destination is a number.

Using "Load_File" included in Core module instead of "File_Load" included in File module. Both functions are the same so File module is not needed.

The example could be something like:

Uses "console"

string input = RTrim$(Load_File("data.txt"), $LF)

printl GetCaptcha(input, 1)
printl GetCaptcha(input, len(input)/2)
waitkey

function GetCaptcha(input as string, lookupOffset as long)
long indexA, indexB, result

Dim lInput(len(input)) as string * 1 at strptr(input)

for indexA = 1 to len(input)

indexB = indexA + lookupOffset
while indexB > len(input)
indexB = indexB - len(input)
wend

if lInput(indexA) = lInput(indexB) then result += lInput(indexA)

next

return result

end function


I didn't get the WHILE/WEND usage. Maybe because string is "circular"?

Petr Schreiber
12-12-2017, 23:34
Dear Eros,

thanks a lot for the deep analysis, very much appreciated :)

I did use DIM..AT magic in the 01_forAdvanced.tbasic, yet I used overlay of BYTE array instead of STRING * 1.
Now, as you reminded me addition of number-like string to numeric value is a correct operation, I like your version better. It reduces the need to subtract 48 :)

As for magical WHILE, I thought Cycle_Next will help me, but once I go around the upper limit with step bigger than one, it always returns 1, which is a party breaker.
Yet it inspired me to express it better as:


if indexB > len(input) then
indexB -= len(input) * (indexB \ len(input))
end if


For those interested, Eros commented this version:
https://github.com/petrSchreiber/advent-of-code-2017-thinbasic/commit/f13a5c8be9553043c1f90e29bba060f8e1584f4f#diff-a6600ea125f711f82ca1b4f6b8fd1386

I mentioned advanced version here:
https://github.com/petrSchreiber/advent-of-code-2017-thinbasic/commit/f13a5c8be9553043c1f90e29bba060f8e1584f4f#diff-dd4ebafbfb11d08c837248f44f3ff985

And the current advanced version, after suggestions of Eros looks like:
https://github.com/petrSchreiber/advent-of-code-2017-thinbasic/blob/master/01/01_forAdvanced.tbasic

I am looking forward to the review of 02, once you have a time :drink:


Petr

ErosOlmi
13-12-2017, 08:50
My version for Day 02: calculate checksum of a data file containing lines of tab delimited integer numbers.
Checksum is the sum of the difference between the max and min value of each data row.

I used the "matrix" version of PARSE to load file into a matrix instead of an array of strings to be parsed later.


uses "console"

#MinVersion 1.10.4

'---Parse a Matrix from a file
string gMatrix()
parse(file "data.txt", gMatrix, $lf, $tab)

'---Calc and print CheckSum
printl CheckSum(gMatrix)

waitkey

function CheckSum(lMatrix() as String) as long
long row
long col
long lMin, lMax
long total

For row = 1 to UBound(lMatrix, 1)
lMin = +2147483647 '---Max possible long, see help "Numeric Variables"
lMax = -lMin '---Min possible long, see help "Numeric Variables"
For col = 1 to UBound(lMatrix, 2)
lMin = Min(lMin, lMatrix(row, col))
lMax = Max(lMax, lMatrix(row, col))
Next
total += lMax - lMin
Next

function = total


end function