From f13a5c8be9553043c1f90e29bba060f8e1584f4f Mon Sep 17 00:00:00 2001 From: petrSchreiber Date: Tue, 5 Dec 2017 20:04:45 +0100 Subject: [PATCH] feat: solutions for day 01 --- 01/01_forAdvanced.tbasic | 40 +++++++++++++++++++++++++++++++++++++++ 01/01_forBeginners.tbasic | 33 ++++++++++++++++++++++++++++++++ 01/data.txt | 1 + README.md | 6 ++++++ 4 files changed, 80 insertions(+) create mode 100644 01/01_forAdvanced.tbasic create mode 100644 01/01_forBeginners.tbasic create mode 100644 01/data.txt create mode 100644 README.md diff --git a/01/01_forAdvanced.tbasic b/01/01_forAdvanced.tbasic new file mode 100644 index 0000000..eaf14ea --- /dev/null +++ b/01/01_forAdvanced.tbasic @@ -0,0 +1,40 @@ +' This solution is faster, with more advanced tricks + +uses "file", "console" + +string input = file_load("data.txt") +input = rtrim$(input, $LF) + +printl GetCaptcha(input, 1) +printl GetCaptcha(input, len(input)/2) +waitkey + +function GetCaptcha(input as string, lookupOffset as long) + ' This overlays BYTE array over STRING memory + byte ascii(len(input)) at strptr(input) + long indexA, indexB, result, valueA, valueB, addedTimes + + for indexA = 1 to len(input) + ' Reading ASCII value of character at position + valueA = ascii(indexA) + + indexB = indexA + lookupOffset + while indexB > len(input) + ' Short for indexB = indexB - len(input) + indexB -= len(input) + wend + + valueB = ascii(indexB) + + if valueA = valueB then + result += valueA + incr addedTimes ' Optimized addition of 1 to variable + end if + next + + result -= 48 * addedTimes ' Because ASCII of 0 starts as 48 + ' This is faster than subtracting 48 each time + ' from ASCII value of indexA, indexB + return result + +end function diff --git a/01/01_forBeginners.tbasic b/01/01_forBeginners.tbasic new file mode 100644 index 0000000..7f49b55 --- /dev/null +++ b/01/01_forBeginners.tbasic @@ -0,0 +1,33 @@ +' This solution is very simplistic, unoptimized, makes one cry + +uses "file", "console" + +string input = file_load("data.txt") +input = rtrim$(input, $LF) + +printl GetCaptcha(input, 1) +printl GetCaptcha(input, len(input)/2) +waitkey + +function GetCaptcha(input as string, lookupOffset as long) + + long indexA, indexB, result, valueA, valueB + + for indexA = 1 to len(input) + valueA = val(mid$(input, indexA, 1)) + + indexB = indexA + lookupOffset + while indexB > len(input) + indexB = indexB - len(input) + wend + + valueB = val(mid$(input, indexB, 1)) + + if valueA = valueB then + result = result + valueA + end if + next + + return result + +end function diff --git a/01/data.txt b/01/data.txt new file mode 100644 index 0000000..9081efc --- /dev/null +++ b/01/data.txt @@ -0,0 +1 @@ +7385764686251444473997915123782972536343732657517834671759462795461213782428342931896181695578996274321317419242359534783957372932953774336338118488967172727651862498838195317654289797558683458511126996217953322817229372373455862177844478443391835484591525235651863464891177927244954925827786799436536592561374269299474738321293575385899438446558569241236278779779983587912431395475244796538888373287186921647426866237756737342731976763959499149996315591584716122199183295277439872911371313924594486766479438544417416529743495114819825984524437367225234184772617942525954961136976875325182725754768372684531972614455134523596338355374444273522115362238734383164778129376628621497662965456761631796178353599629887665939521892447361219479646483978798392716119793282717739524897385958273726776318154977675546287789874265339688753977185129334929715486381875286278528247696464162297691698154712775589541945263574897266575996455547625537947927972497979333932115165151462742216327321116291372396585618664475715321298122335789262942284571328414569375464386446824882551918843185195829547373915482687534432942778312542752798313434628498295216692646713137244198123219531693559848915834623825919191532658735422176965451741869666714874158492556445954852299161868651448123825821775363219246244515946392686275545561989355573946924767442253465342753995764791927951158771231944177692469531494559697911176613943396258141822244578457498361352381518166587583342233816989329544415621127397996723997397219676486966684729653763525768655324443991129862129181215339947555257279592921258246646215764736698583211625887436176149251356452358211458343439374688341116529726972434697324734525114192229641464227986582845477741747787673588848439713619326889624326944553386782821633538775371915973899959295232927996742218926514374168947582441892731462993481877277714436887597223871881149693228928442427611664655772333471893735932419937832937953495929514837663883938416644387342825836673733778119481514427512453357628396666791547531814844176342696362416842993761919369994779897357348334197721735231299249116477 diff --git a/README.md b/README.md new file mode 100644 index 0000000..56add09 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Advent of Code 2017, thinBASIC edition + +This repository stores examples of solutions for [Advent of Code 2017](http://adventofcode.com/2017) challenge. + + +Let's explore where thinBASIC shines and on what ideas we need to work on. \ No newline at end of file