diff --git a/day1/Makefile b/day1/Makefile new file mode 100644 index 0000000..d380c4f --- /dev/null +++ b/day1/Makefile @@ -0,0 +1,9 @@ +BASENAME= $(shell basename `pwd`) + +build: + rm $(BASENAME) || echo "" + gcc $(BASENAME).c ../libs/* -o $(BASENAME) + +run: build + ./$(BASENAME) input.txt a + ./$(BASENAME) input.txt b diff --git a/day1/day1 b/day1/day1 index bec4242..461a59e 100755 Binary files a/day1/day1 and b/day1/day1 differ diff --git a/day1/day1.c b/day1/day1.c index 83b5f6e..eb5ce08 100644 --- a/day1/day1.c +++ b/day1/day1.c @@ -1,10 +1,21 @@ +#include #include #include #include +#include #include "../libs/buffered_reader.h" +#include "../libs/utils.h" + +int decodeA(const char *str); +int decodeB(const char *str); int main(int argc, char *argv[]) { + if (argc < 3) { + printf("usage: day1 INPUT_FILE a|b\n"); + exit(1); + } + filelines *lines = flines(argv[1]); if (lines == NULL) { @@ -12,9 +23,58 @@ int main(int argc, char *argv[]) { exit(1); } + int sum = 0; for (int i = 0; i < lines->size; i++) { - printf("%s\n", get(lines, i)); + if (argv[2][0] == 'b') { + sum+= decodeB(lines->lines[i]); + } else if (argv[2][0] == 'a') { + sum += decodeA(lines->lines[i]); + } } + printf("%d\n", sum); + return 0; } + +int decodeA(const char *str) { + int tens = -1; + int ones = -1; + int len = strlen(str); + + for (int i = 0; i < len; i++) { + int front = str[i]; + int back = str[len - 1 - i]; + + if (!isalpha(front) && tens < 0) { + tens = front - 48; + } + if (!isalpha(back) && ones < 0) { + ones = back - 48; + } + printf("front %c, tens %d, back %d, ones %d\n", front, tens, back, ones); + } + + return tens * 10 + ones; +} + +int decodeB(const char *str) { + int tens = -1; + int ones = -1; + int len = strlen(str); + + for (int i = 0; i < len; i++) { + int front = str[i]; + int back = str[len - 1 - i]; + + if (!isalpha(front) && tens < 0) { + tens = front - 48; + } + if (!isalpha(back) && ones < 0) { + ones = back - 48; + } + printf("front %c, tens %d, back %d, ones %d\n", front, tens, back, ones); + } + + return tens * 10 + ones; +} diff --git a/day1/input.txt b/day1/input.txt index 83db48f..a4dc984 100644 --- a/day1/input.txt +++ b/day1/input.txt @@ -1,3 +1,1000 @@ -line1 -line2 -line3 +46threevqs8114 +threetwoonez1gtrd +6ffxbtff +769twotwo6rv9 +gjrcjrkvghthreegqqrg82qbct +zkxjhgprtrlcfeight795five8 +99seven3vdcgvmvxtjtwodc5 +three5eightthree3four3vtkkqrgxs +four863mrrnrsxrkone +sevenntgvnrrqfvxh2ttnkgffour8fiveone +49fbsfb +3rbmlsksg +ztgszqjjsrtmgqx6572 +3bqnfxkdbonesixseven +mfgx32ftpbhgngm7 +fzrpfhbfvj6dbxbtfs7twofksfbshrzkdeightwoqg +2xcftwo +cshmmltsml4fiveeightdn +eightthsix1 +two1gvfxcqnrfnbeightthreexznhbmmk +3917sevenvxqxntcgxskh +ksctcnfxdsk96drlbjkthreesfqlvnpvfbcbmg +8nvrmzfs46 +5dbgltmgg1xvtqfkdxsrxzltwo1pgqlqndlc +eight7four6rpbtmjzj5 +41seven +gpmfhninexxgqr6 +15sixdrhxzcmqf +8fivettgmcslxptwofivelckzvfkl +67ninetjngsrvcpxb8eighteightwofh +4nine7oneighthm +njtwonefvhjplkjgvsevenbjg77 +eighthrspkszngkpdtzdpcsmjnvlnhcm9pqmpkxqmbtmbv +ninehthhgbfsrrbpn2qpcflhgdvh9twotpzkvzmmsj +6fourzpjthmkrkvqkvvp +vnrnkfp6 +pfouronefour6 +87fourmznhvmt7 +nxbssjc1sevenvrcjlczct6ninekclbffs +eightrpzsdggsixthree9dhrnqtjcbxthree9 +pkcmnktzvdjtbbkvxhxpsfp8 +sffd124fkqsq1bd +txzsmrzlcnnine72kgfkcf +dbfbdskfdn5nine2sdr +eightseven47six57 +2eight634sixmlrkhrbslf +hpeightwopgrqmblthree1fmfnvd +2jbbf +vdsbvmjseven4eight195vdkfc4 +five3xjzlrmxvqznine +nine4vqxmzqxcvfhlm45 +rtwo1 +six8jddkprxvbh98hd +five5sevenfourfour4 +seventhree4lnxcvdprp66hsjfive +ninevrxxhgvkghmcs8gsvplvsfour +6bkeight3fhkfqvvsbcsixljfive +6sixktwo +lmscbrnlzmbqpl75ptwo64eightwoxcm +2sixfivetwoeightdpskxjtlrfive4 +87gfmklvnzfz4594qlksr5 +five3one65 +vxrnhpvrrdd7nqdjfqnhfglfive7qqtzhcrk +7sixthree441pf16 +11hrfdvdkf +vst7three2vtwojrkdkjltgdt5 +vqmtwo5 +nffpcvfpjleightfiveeightknmjfive2 +ddfhzdndxvhcgjrcmqq158 +kkeightwo14 +8onetwo +sevenrslxphqpcpdnxhpzqdf1twoone9 +four8onelgqhkmxktf5six +8ckshssfour73twoone +threethree8onefourkkhvfive4five +64xpzhxtsixseven59 +two8srjslxss +sixsixninecpgjkfhzdqdjlc48seven +lpjgghdtgk1sevenzpkmxzqxfourthreethree +cvznpxfhj2jnlgmnbkdsix +ninetwo1fournjqvsb +sxhl3kc +mtllnjrbscfkdhseveneightgjr4one +qnineeighttwovjsnvs7five4svdmjt +bcplhrpvmkxkjxlzl7zkbpdnoneninethreej +1mbmfktgtwonine +9threefkptqhhnine2six1fgxnvvfvplv +8onefourtwoxl42shxq3 +38rlbhvlpeightfivelbcm3jfd +97one1threexjfsljjz41 +kqmkvmh1three9three74 +3fivenineeightwod +6sixkgnqqb6 +sevenjdn8csthpjxgnd83 +qhssrppqxlone649 +3gjpsncshmnhkdpzskmmfst3sixfive5 +fivejprgsxddzgcnrn6cpnxnj +4three3ninedqknine +61xfrcfseven6 +sfpqbxbdrhqkndfdpldvfive7gxmnthreetwofour +lbvrzv2onec2six8three +ninenine2grvzhgtgfourthree98 +1579416 +5fzcbbxzfjjtwo6k +xmxbtqvnxf665lplnhlgjlkzhkkbknjcxqvkpsp3 +fivefive26fivefour5 +5dbxqcbbkjonehseven +1k +8sixqmone9zthx6 +ldsmxpbkkx1rppscjfour3six +fivethreethreekxzgqmrz8 +lxklrjjmqsix5snb7 +threethreetwothree6 +8three6 +fnine7jmmseven3 +rfhlkhhlx8sjqgclvlsctwo +vrzpdfjs5fxldpnhvhmmxhleight +9tmqznszjgrnlhnpxbc +3sxfscfseventhree +565vs2 +four86six6 +16 +4vbcrh +62jrjfrdcldbvjzhp +onecsqpbrxpmqtrbbtxnrhkdlthree3s1six +two798tqsjv +znktgzzmtwo28 +8hllzrjqhvfone +tgnlbtc2ninejmcsixfour +fivek1five +rklpvqfvtwo3 +lgdgchcpcl55hfdmdj +onesix7onefive9 +jrnthree46seven +qqptcnrxlllvccssrkkmkxnz8 +cmfour8threelthree +xfgqc36oneightvlx +xrvrchbdhftgpn22fgmnhbrscfqrqpgzjmgfive1 +stwotwofourfourthree2 +eightjcfqthcvcgpxxl6j +cjvfdgmplfeight9 +591eightwoc +jftninejfb11one1zqpnine +threeqndgjdfive4fourhgcsbptb +7pmxvqdhbhf8six +67zhx64tlnjkvhvv5cblxd +29zqfive3jkzmbj1 +smvlmnph1seven +61four +zjmgnbkoneczgsftnhtvfbtjvbjzpspcffrdkv8 +threesix4 +fhqm8 +ninefour1 +two422dqjqhvhbqkdqf4sixoneightgrd +llnrdbhthree778dqzrcplfoureight +eightjblmbgqhrj2onenineszr2two +8cxbxqssfkzpponeppcqsx +seventzlsxrskxqm8threecrclkpqjpmeight1 +nine1twonem +6threeckmkktkvxjteight559ldn +2zxhldmvpqdcddsxpnnvrvxmc99 +fourone4dzftfnccnvzjdrg49seven +7eight44 +xmjqm8two47rpjfninethree +7zhmjjhtrqjcmfprhsix5twobfive4 +rccsqlt8 +seven6three +lrbp3five19zxb45 +8jlbtllzbtdgzbcfln929 +kdeightlfk9two2fhgshhbzhf +dvxllmfourpzfpvch7 +four28 +3threefour4hxmfbmjdheight +6twosphthbbqcltcgxm5ssvqndphrnine +438rrxntninetwophsix +eight3eightnine1sevenxsvfr +4643kntqsvgqqtwo +two8two +7gbxkjonetncmeight52 +kg517 +onetwo933 +fbdmpjrjsscsfvfntwofive8tzbrbjsj +7eightfive9nineninednsfhzvtonepccctxssbc +93twofour2eight +7bvzknzdmkjvrxkfngfnsd4foureight5 +onescsxpcfivelmqvfqxmc99vbshtgzjkjfour +1hvdnine1 +542zhgcmvzcj3threefrvlcc +sxfc95seven3 +nczqknxn4four7xj81r +8threefive +7zvhlf4 +drbxjhqp9gfgxxtkl4 +five2twotklsixfour +9four9 +2jkeightthreekbqbkffpsztwonine +3vtffqfkfh9onenine4rqxm7two +4hlxllh7nrlxcpnrrcfivebkfkjdfjbksnine1 +drgsb3four +fourhscfkzkjfcdlsjgjrbmlvvcfgcmxtsixjlhfrfmk8 +ncrkhfive1eight1 +8fz3zkhgleight9tqsgnbfoureight +5six26twoone3two +ptksmjfkl5twothree4fpshnrcfrmqxeight1 +jfkccclrrr49four6eighttwo5 +threefourgj7eight6 +73slt1one5 +six1zrlfk +5six17 +qrsix7sixonexmclfz2 +3fivethreebplrfgklj +xz9sixhlt4eightone +gkknnbzkt6ffourvrjbfkqz8threesevenn +2onenine9 +czsgh1five4qtxmtfksln +eight6fourhpppr +lpshxfqsddeightqrfgrdn1five6bnr +5lmmlszndzfoureightqdbhlt7 +9eight5 +8sixrpctqfgf9dm95znhx +1sixsix6qpmqmtwolh8 +six4threenine2four982 +8jbthree59nine +q5 +9twosixone9fivehdsvpkttbkcjdbkone +sixsix4cldngone3four +9mvsggrgone9gjhfzdmvqbnqzlxgpckg +gmtqnkdlfv8 +8seven7rxtzksl74four +bmrtbjzqhbgzcjtwo74fourfourlvj +twoone4five3twoljrznhdcggone +42nine1eightseven +45fiveoneseven6 +mxfoursixone8coned5 +sntmhcsix633seven +eighttwolrcrl5four +p22 +1vspmvx +cpcsx9xgqlmblhshvnsix2hnczvdrrg +grgtvsqdtslml4tlqrh +hshgmv3 +fivethree63m3one +95threecdfhqtmqb +xjvd14dnfrjthqxvvq1xmqfhv7 +eightnzx48nkqhfssix +8nzvck4hpfxbstf6fivesix +4drxbsxgtskqtlnhgrrrz +6foureightonezbpdbqflp5one88 +twofivefbhfourone4cpnrc79 +5trhksixfivesix2k7 +25three +1phc9zhhn +five8ptkczvqk87gkr8 +six61eightfourjjzrktr +4gxrqrrvkshnpgnrjg2threekpcrfn +7bclvsb1glbpss +2eighthdbvxpz +439 +3drtsf2two +ninenine8hqqmfvddgspzdsnine7 +xzrjqdlrp2threedrhjztbsvg4mmqvchlhlp3 +99threesixzpgxnseven5one +2hvst +twohvrvp164one +1nine9seven1eight8 +dfkdzprs95 +zthree925fourf +kssmqhkonetwozcjgvjgxzr3gfdcvgm +bbsqsix6 +onefour9seven73 +onesevenx1ncpgtczseightssevenone +419d43dzjlvn +ninefourb6threezgqvmzklghgsj1 +7crdxpfkcmtqvbvkk3 +8rvp2p +gnvrlsfjxtfssqz8two6dcfive +jgtwoneoneseven7oneoneeight +dfsccdclmffourpsqggngjzn3 +22brtxbtkkdfmkbmv +sixxlpxqdtwo7 +six8mrvhptlfbmczlnfqrgbvz5d +84fiveeightsix8 +nine5rzqxp99gmklplmxtveightwombf +seventhree7 +mfklcxxbhpqsdt2one1 +two7seven2 +63sevenpmdzdrkzjkjzgnnseightjztlvzqhx55 +8twosmrgbzzgmjbnthree +twosksrj4mk +onethreeseven4eight +2kjldqzszg7twolfivethree +3qdnbnzglheightjtnlqqqt5 +6l4vddmkphgmb3vcbsbgclkzhmmtmfour +sixvtzmppfssl35eightqc +six924lt5 +5sixnmmeight7 +mkggvlrdhkjxcr9jcs2fhsptxhj3 +nineggh1 +qzzckckm3 +hxgjhxthreetwo3x4 +7qonejntfsvpcftwogxkvpvbj +2twoqlg3 +1sixhvsclvhshbr +seven8tffqvgs +twobktthxfqqthree38 +397 +lpm9six31seven +75nrrfxrj +8sixfour +ccfbzbjjqlgkbqnxhgd2 +7eighttbrtddvbvrtmxjeight5nine1 +sixfourvbjv5six61two +sixff4dskpsqsrcd7 +three5five8 +8sixthreethree6 +oneeight7cdjxfmfjbpbjlsevenjgthreexh +ninetwo4xdxmxpmd86 +eight312 +pmvgbvn8seven +djrftlbzvfxjjgrh37bmfkone +ksf53onefourfive6 +7vzqrmklveightnine +two55eight69 +kbjprbkcgpskbnthree6gznqk5fkttt +22three37bvmsmjmx +zpchdzmbjhzl1five26threesix +2onetdppvzcbzfive29four35 +66one9clqlkbqpfdqkkxv +lql5four +two41sevenseventhreeone1seven +fiverbkxdczfive6smlnbgn6sevensix +lpveightwoeight9two4frfz54 +twohfkvxqrninesix1 +6twosix +9ninefive47 +sixsixrjznqb33sevenhkl +one5nineeight48zqkhjgqlcq +foureight4seven83 +zzoneightvcnhxlmgjp4ctclzzztwo46six +2gljzksrvdr21ms +eight75twofive +threexdsbjrf6 +lhvrfb3925six +fgrlrvpnctkdblxvzone2tsxst43 +zpnbvs5foursevensevenjleight +five89seven4seven +5rnntxqsngssixonefb3fourdglmstvmft +tmvldx3eight163 +hmnvbpr6516 +3jfqshjf +9vgpgcflbbonejhmlncrrjvxdcshdflktr +vgjmhqnvghc718sevencfdzknkhonesix +dlzrjfklp3chqxjdpdrv +eightseven4tcbdhrgssmpvxsxszxrmpzrt7 +five416sixp2 +2threefourj +hmzoneightmvrksbxbjkvpslkv1tlxxkzsgdseven86four +pzleightwo774five5 +dvsmtwokrzscgnvztznqh9 +6chqttwo +67sn3dfdeight55 +4oneightqd +jhlhcjnd6sixqptqlmrskfive1 +9mfxbvb138zxngghqs43 +2srctwohfplclone928j +7eightsdsxsskkd8five82 +gccnsvpnfg1195nine +8584threethreeskzkfpdhz7 +prhhdpfivenine5fsskx5eight7 +9nzhbn7mvczcqcfive7tslfive +vqxseven5seven4332 +95sevenninefive +eightzdnddmlrpbxnvxglqnqnr334hpjrl +one6three +13dhjftxnp5k +92threeone3seven +ncsfdmmft2eightmlfxxdbv +four3ninethzh +31five9ksssbdqzlfive +twokghhrljdc72 +two6fourdktfpddftwoseven +zgr3bdfqvnkdvpcntkrcfnhpnpdrtkzpthree +9rnpxdtkbhjbjnf +1247lqdnsgnp3xtdxnbxkrthfhsrm +sevendcdbr38ninebljxshxdzqcknvld +six3gpngxvdqlnrf9gjqjvzeight +twogfxctgnb4sixqzdmhc2sevenbq +sevenpjmrj2skcc5 +eightsix9threemxtx13zczqvtvsix +1tmgrhxzfql9lbpgscnmlcghptbtmpfgct +2foursdhmtqmfjlf77 +bmgttpr2eight177six9three +1nmbnkszc +three14six1lnlfpfmfhm9 +eight4four6five46nine +26sevengdnoneeightfive +vhz1fivejzjqnfgrpfbgfz11nlx +4jnkzktpninejc +ninefourone1five2four3 +cljtfplrgbv6fsrlfxqmxhtwo75two +41htzjlzsdbg +hfnvdnninezhhbsmxfour3ninesevenhdnflzlt +8vlshckcch57 +28tsbvgmzd +qccgcsqb5trsvmmdtninehbgsxqjftrvsix7 +3eightkfqlsone +fgoneightsix4eighteight4one +8csdgpcbfjbmsixmjztbrqdj5 +9two152 +onevseven1 +vfsjtpzttxdrfive6 +69mdbtdnqgxjmzt +threemgzspvnvsjone7eightjxssg5 +threelfslcjhhgmfftmftfzsix3qs57 +onejhskmbxcqpkxjxh4threeeightseven +hhvgnineeight8xrtvmqjnkccdptpvqxlrdkkgljmbnzx +onezvj2threej5dlnrcbhfxlkkmkpx +ninepmgbdcvpds3onefive2 +9tblnmmeight1xdmtwo +cnczmrhjmmcsccnine54 +2fourggjjrxckgvsevencpkgqzzfnptwofivefive +61threexnrhqbhxb9 +seven14 +32seven +4cqtssgd3vdxbnllfb2 +smbqxbxgmeightthreeonedsfxfronec2nine +3145fvqz2 +bgdgbptnr9dqmvvckb +tpt2ndklfgxtthree +four16vztcgtwo5xkjmp5 +59onelp +ztwone9cnvbtpglsq78 +4rmmhxpqsknxhgqmlh9tfzfngqsnbqdmsgtcjsbntr +4x6lcmrxhhkcscxgxtgpjz33seven +cnzlsqbvk8sbbzphzkhmeightfive58 +twofour7 +jcsgmrgeightcpzbb8135 +4grxpsix7hgrrhtjvgq68zzbvsn +qc6 +sgxdninevmmzqdkqmjxpksixfckbjjkgqtwoseven8 +6lhfcjc9 +zkpkjn18six51 +xtnhzzxceight6seventhree27 +qttwone3seven5 +threeone8ksgt9 +8fqkddzb +nine3sixbprlvxpniner +dmbvmrthreefcrqvmhp54neightcz3 +twosl6fdtwokrqrqtwo6eight +8dkrshdpj8threer +27snqfjdg +nine7mdsonenine +threefive2twotktc9 +1twoninesjgqjxphhnineeightthree7 +xxlcmbnctsszkjdhfzqbdr2szvkmscnrg +tcmsnine6ct +2tqmkfmc +sevenpmceighteight1 +hnnthreefourknjznt5nine +5fhjvttdmzdpkjdsgnpqxlxfninepgrsns +psxzzdkspgmgnnhkzt6fsdmfour +1lkb +seventwo2dglxkrlvgthree +seven27kzhnknqltvkbxrtvtwo +7frmrdctninefive +61twoninebbsbqsbhtxmxkffspd +3nlzgzlntrk965nine8ljpj +3pfxjjvcg42lzlkzfs +7pkztnine54bq +sixxbxvgsknjxsix4nine3 +fourvbch2cdrsrxzh9 +one1f4mtvslfczm +456bkzhmxlmhfmkgxrmkbmvhhpmsfk8six +one9rmbvtdp5oneeightfive8 +67zh4 +83hdqrsxfc2twoninekksg +1gzmqcrtksixseven81three +two6threefour2 +nine4rcmbhmhsttgqzfjdm +jg63 +twofour3one1 +5eight1eightqdmgtszxvx26nine +482three9seven +cxdjmjsk1mgqsqkkbq97fourqflf +bvfplgzj69pqfcpglsix6 +v3458fourcpqkn +seven769sevenfive9 +dztdmlkjgonejjpxrsevennineninepkc3 +4ninehvgninethree +tnvgjhzr6 +sevenfour5nineshrsknbbxxsgztrl +four4flpgsixshxxtmnvxfivensznine3 +73cfxzzsfourzqghjqvkbf7six8qgcd +fgqk133t +vjbzpkvsxkoneoneeighteightfive1zrvgqbf +9six6ntrmhtkgrj +fournrrmxldk24mxhsevenfive +8fiveninenine25ninenine +seven1xb3vbzbqkjdvseightfive6qs +five8dgqkhldbsfthree7 +six11three5zeight +hgmqhchrdfivevmftjcgtl5 +oneonesix2bzfsqvkmjzbdq1 +fourx5threethree3mxhgtnine +hzeightwozfcpkjvfour3three9b +1oneseven +hmdtcxld4fdgfgdbjlsevenzrkmnftsevenvsqc +sevengzpnkfn1sevenjcbfxbg8six +2sqjnbtskft5 +fknxnxg9944hvtxj +nine3951 +94sixqslqtxleight +1zpbxc3 +sixddscq1zxsvgbjnqjj6zknkfczj6fxcqmf +threetfvhftlzjn72sevenljtwobhgvvsttg +doneighttwo3vm +llhzdtwovbxr2 +onemjkvtbsix39eight88 +lhzgpqmhm4ptmcxhseven4eightcrvnqzplrnb6 +3tqljjnx47sixsixmxqbflxjp1 +tkvdxbbsdpkdjl3gtnmjdxhrj5 +qgnnghvnkppvvnine8 +89sixeighthmhsvbsg +67hqvxtmq +fiveeighteight7cfmdmfhdld +nnsxvrgpeightjvcfj3fjeight27 +8ninefourfourcjrbxone +1bx482 +pponeight1seventllldzgdvgcdmr +tworrjfkmntbvkpseven4jnrllr4 +ninesixsix9 +csv2ninesixtwo3 +9vzrvg9twokbjcnvcghthreeskfpgssbdxq +foureight6133rszlz2zvxdx +6jfklqcdrgm +3sixhds1kgfvzknhthreefive +fbgnjqhnghssthzshktxll7rqnhjqjb +5xjkchhffgz2 +threexjkeight86 +three8eightztthtfvzdxthreethree +gdxjvzlktltseight5 +twomxfqrxqkrjhs4 +fiveeightnine1ninekhrjbmqhmpjqxtqsjpzbbvnbbzmfour +nxnrqtsplonefive6zrthree +8fivejmr1lzbdmvdpdrhvsthpvone2 +2sdfs1threejjxvjxp +jhkeightskq7 +1eightfive4 +fivefqpmq7twoeightkggxgvrbmbhjdzrmn +5twozrcnf9fourtmxjtkmfiveone +hghfivedpgntxbpndqm4 +twodzbplxm9xlgvnmfvxh7oneightzvm +77eightsixxlvkzfive3 +qrgz7 +eightkdcvchgfourqtrtxtnzeighteightseven1 +69onefivetwoone6 +2onetwothree81qvsmzxpvmpbgsd +263383seven3rrmskzfgcj +threeeightthreesixnzgkggflxfour3 +ggk6two2sixeight +eighteightseven8nine9six +pzthreenine2nine +5threefivecpxfjoneightjcn +mdmhone4ninesix92two7 +nm2twoonerqvckjtxjlzvldq +6fourzxgncsixninenine4ninethree +xxflpfeight5nine13qsgrqvsz8 +1threeseven4mvfzm653 +eight23three7dkgdxkrk +9lqvhv115threezmcnbplgfzjlqmgrpjn +2pgphcxbglfive5vtldzlrrnx +dshlq2zcqhzssbdseven5twothree +eightpvqcqnsprhtvlh5 +789sixtwo3 +eighttmzkfzksd39sevensix8six +bkk5two +gkgqxksdpn78 +577gnmbvzf7 +rcfl6foureight4mszdbmkl8one +17n +fvnkgxqlpflmmnf8eightsix3twokgj +lh84twosvptcklvt +62one +lmlbfcxpdqlmeight94nineknk +threenine5three4qqhtfkg +3nine8threetwoc5ninecxcc +sgxrhdonefcpone1tsb +oneqjkppzkpmzcgqv7nine +lfngqdlhone4rhfsrgdldl5mrpvzr9 +vvvhfbz52eighttbbr +9threethree +cnntzjvnplnineeightzdmtsx6 +4ninefourdndsvnbthree +3zbt3nine28 +1seventhreedfggtfvvsngszlsix3 +six13nine14 +9gjzdmdzssixone +nnxbmrdmjdhrjnhktlprtbvknninefive3662 +6rrmxmfm479tcxmvk +ninedfchdzxbmlrjkckkjpqqxchz58 +three22sixeightrbzmdlfztwo +jqljmhxgmfxkgsevenone77ffour +9xbphsix +onetwo271sixninek +sevenbdxtwofivexscbkflmkncvh7 +jxlqvscdjz7six62 +six4five +eightsevensixone7eight +five4five9 +btsjninegdrcbxdrgmmg23q +67mqtphlkhtp +3drdmqx77 +8twoninenine +eightfcng16kzgtfzbdfxbltn113 +fc68 +3rkqrhlskj8one8 +eightqtkls21five99 +41clcg5qmfkdkq6snghpone +pdmnvqhg8vjrvlld9ninexcglqpgm62 +7one4two2ninencplhmrztsqjgt +6cvdc +2sixone76eighthqrlmfxzk +six512nrsstwoeight +2fourfourqlctwo +sixb23seven3eightcbmvk +jxthree1bvtwombbmcdbsbf +eightzdqnjbjzd9sevenflcqqflk +four88sixtwotnmzrltlnineoneddrsxk +9six5 +5ninegfhvsqblftwogmmfgddncqfmqjtqvbc6 +9svv5jlfbrvpntgpqkkg68 +khglrpxfqqfour4 +qkfour1sixfnpzxkdzr +22onefivemhsxvhllnzr2 +1onehpdqbrklhg +bs6sjsndrz2 +8five9 +ninetwo3five5xmpkzrnqzq5 +2797two2 +91tdmpxtrkfourq7fourcvtbmsk +four82 +three3sjgxtp86eightwof +sszzthsixvrdbglczld3 +four1fourfour5sqknhn +vlhzqfbht8px7kznvtffourq +jshqhczgr3 +4two1fvvxdfms +seight3fghflv +tgqdzsdzdthree787 +45nrdlxtqggcseveneight +87tkbsmfvhl5five3zqx +6rmpdxvrlszpjtqmhcd +fourtwothree49346eightwohq +9kkgdv +ndoneightsevenfive8 +threeonecc63pvgxd7 +ctwone8zbxbf +foursevenrpkshs75two8one +2jsvxtrrkjxjzxmh +nkf8three +gzbplsixeight86lxpcrzxpbx7seven +5pzpfs +six12ssffeightfourfivefive +one2four71 +5onebhrjgrrltbsix +hclv99two89nsfdfour4 +8qk8fbflndsix9 +lmvcprone16 +rknqlczt15thqlpvk4pgllfmsix16 +seven8jgpq427sixtwo1eightwokb +nine8xgghnfive +nineeight7ninefourrtbtd +threetwothbdmvtcrjgsm3fivehdqlxtxnkdstgs +4sevenfourvrqfkhspbkqjcxjkjfniner +5three69mzqfqrhrjdqhbpmscz +478kj1 +5bhcrpjtvjsxpskmscxclpzxkzc8five +sevenhlxgzntwo4knxtcqscdone1 +three5dmjg +ssxtz342sevenfivefive7 +84two9vsqlbh5fivefour +4fiveninetwo +8cqz +twoptxmcmnsbgrrndfdgtwo2ppglmzcfour +hdqfnlzrscveight5plplhvbg +sevenmgxjzjtwonine5 +2thfbhzjsdk +jmzsvtqjxmgstbqctdv8qc +gv8glpnsixninesxhzbmfqxkqfd8eightwonj +88vsqztnhst +sixgf2sssrzvqlsm9one26twonedzq +4five611one +sixdvbsctpqbqfive6 +one3c +xcnkrcdxtfivefour3nine +7srqvlkftzztpgrfive2threesix3gtzcnfdvs +3hqsevenlzgbf +cggclvfppfive1dgnnqgzqzsdhdpbg +noneight9tktnzqnttwo9fivenine +2nfourfourmmfltjvxzfivezfntcxfctqsdckcjl +htzvlsixsix8twoone +91xnrqfdqsbrvf6sixtfour +hvtwonenzqnine9fourvtcdndzptfourx +sixfiverp8fourbnfgnlkqld +1728 +18nineonejhbvdbm22jgrbsgf +2rcpqxgsix8fivebscvblxzcmbztrlsx +9threetwoeightxbsrxpjns4zkpztwo +six6sixfive +zpqjsckrcztv5 +3nvsixdnmfqplqtqpq9 +onesixeightthree6nine74 +66sdh +235lmnrgxztt2nctntsgfive57 +fivegnzfnbfpvnl72fournine +3jrfive9srqvsxxfz +vs4threeeighttwotrcpphdq +zlfcpfptwoninescshbtcdv58sixrkxfour +eightfour8 +4onesfkbnnfsevensix +fourhjsbvjgqhrvtdvmrzrtsixpglhrlcrrkr2 +fourthreedczqpjskdsqnnhczhdc47 +3sevenfourtwobn +3fivesix1 +4qbtlplbl25 +66grjtlgsbflfjbcdmdnnine6 +1rbszctwo7pkmvsixsevenoneightqc +snpvmbppvtwofiveeight1rnmfkrsfbhs +5frcvcsixmlhfcv +2nbd +557sevengrvxddpdgfzsqsqx6three +five41lrlkdmhsbjq +eight3vzzgspkgqh4eighteightstthdqm +vqctjspmq9 +four12clqrgts +45two8zv +c4xklll3 +four3hhfivedtj +six55two3 +gxdttwo256 +njthree45 +7threevl5174 +bkvcbfournine6seven +four8jjg +725rgkxrztbhjmsbxgvc2 +ninetjqd48 +xbpthl9x +1sbf8seven +qfsbld6five7four18 +9kvbcnlctngbthqlbqlbfhttmf51 +98eight3vfcxzbsvfour4one +tlbpdxbglrcqppkcb6two6rbffxpzmnntpdr +fivevgnzjkjcfeight7twozvseven +vjmdjsrl8onetrnlgsclrfmpvkdblfourktwonet +tvvkszfvnineeightninetwo9 +2twollqsbnconescjjj +one23qz +nine2vbglpdpvfhrqp8eight +threeone7seven +vxxone8onetbgplp3 +two1mlqrfour7 +ninezdkbvkbonetmzxgszqxdnttkgsgqd7eight +vrfbzcfourfivenlgmvktszpthreepqkgfnjfc9rfmkxbv +ninezhsfngmbc5fivers +tzjxrncfnine1cglgmnmxhssevenbdmxfjtg +sevenseventwo5five9seventhreeflvgdbstfr +7rggpvgkmone +1veight +41vk7two5 +7onetwoone6 +kvm25nine +onekzcjprtxbncpqchpxpvtttvgmqfour3pxpcmfourtwo +chrrlnddfivefive5 +ttvnlchqz8hrxhmgqrfour9rmkkdlr14 +5jcmdfnldlq +6431khcknnine7 +onegtmzngkqgfggfmhxshnzltdbkvmkjjvnfpkclhxntvngvlnqd3 +q4fzzrpt +cllbnine5 +six18ddmk22six +2knsdjp2nphnonesevenfive +vqjkllm1threemfrnlvrmjzzsnhmxsxtwosgxjgjcqrtwo +six4onefhpxkpnqfivekbxhmxtbx +djsptzspn3eighthb9nthreelhpzd +twofdvrmxrcmfive27zdcssbvcsthree +5eight5ptfgcnthree1zjvkk3l +3one8two +bsdphtbdfour1twosevensevensix92 +four7six4smppkbtwo4 +87dpqjbbtwo78662 +1ninefour8four32 +232spbt1jfzfourthree +3cxxkqldtwosevenninetwomcdvls7 +5seven7pgnkdjgckfive +rvmfpjs2threespszkcvfklpdhtmbnseven2seventjrhx +eightfive3twonbxs5 +3frglvrm32 +19778gmtrsjkcrdqlptpjzg +zpvskbdztrone5eight3sixnmcpdshrgblddrht +7seven2tdnblk782eight +mjjldone2cqmlmmzxbt +hfponeightoneplk66threezzklrkrmvronekhqsrnvm +lghjkgdssfour423sevensix +sevenfmqtwo1onecmrhrzhl +dmpf2fivethxhpss27jn +2lnpfrdfour6x +onetpztmmfr2 +66qlzhrrlchcxznsjrfxqxkone +9fivenlzrgm +mstbglvtxnzlnhjrltwo238bsfourczkvctl +zkz6dcdsfgssl8b +cgnqsdeighteightfourhnpkdvggdsixfour3 +96xschgpnlktsix4eight537 +31c +4smrxzzpvqmrxhmltcmgq8zrxftd38 +onemncsrqlklxdx9ngpbxhxpk7 +gzr2sevenhjsczr5twoqsmhj +nkfh4eight9gzpkvvxjsqfjbt +two2nine +cxxqmzfnm3mbsmltpkpfour6fourfrjjcztt5 +lxnqtpzldptcvnlcggonehndzmx7863 +3fmgl36lbcvrrczdfqdss +fourfour4xjxgmnctwothree +nineseveneightgrdvffkntwo948 +fouronenine8 +8xfktsxszhn53 +6onedgffr57 +krmvzbhctztlpfour9fcmdscctdhsf535 +fbjgdrzxv1four573vljtseven +8three9ngrjrhh +1gqfoneninegzpbjtbd74 +6vkf +9eightzmfsbxqttg1one +56k9ninerqprllq +jnhlpkbxvm6three6seventwonegp +j929 +sevensixnxgbtfksix5fbdvgkbd77 +39sevencpdcbnshpcsevenhggbcthree1znldhfngnk +618fourtzfrql +kldcjxktwo5fivetwoninehsbmrgtn +msvjzqteightthree3 +2xtvr581xsbctkfftjprmb +seven13dvstp848dsbllng +sevencqhmth7tdshrtrphx9 +three2eight8six6 +fourprlhnl7vczqdtl8bjmncrrfpzk +5four1one5 +seventhreethknine5 +two6vqgpzvmhlhfourklvxvhmnlqnmrhknstwo +4bvxhmfz2bktbfltj +nk9 +xjb713vmxzcfxl3ninegkjrcq +rrmplxeight1znine +zsjgngpfourfoureighttwo1 +99twojqzlggpseven +eightfivetwo7 +8ninevlkxsptd2ninenine +4hrzzjxp26sixpxjxphmkpc8 +vlqsf4ntssmfrz +eight93tflqhb9two76six +ttxfgqtrfournine9pqqgd +7274mmjgcssvrsix +1threesix5dmkdtjsmptthreeqgfgsix +msrtbbschxmvcm2ctprdlnkdzthree +dztlvgn3two +h7crsblhtp +seven5eight7qrgxlninethreetwobbsjdbxcvt +2hfbeight +eightrdlfourtwo3vzrlsbcjndnineone +ljxbtvhztkkb4three94fiveeight +3fourkbpmpctthreetwovzhgq +6xznbcgdmng +3jl5zlkbjpeight1tvmrqdlk +23sjvtghccsevenninesixzlhgzqtkhb +xptf34eightp4cdqcv +gfneighthvf3nine +threeeightthreenineninethreetwoone5 +9nine22qdfzgbdsd7two +gfourtwotbxddnxzc2 +four84gxllvp76four +t86 +nheightwofive525two +hh115 +lqvxpsxthreethreefive34three +sgvqhjqdvtzfnine6bgjcbn2tqrvgfmdtdbh +nine2seventwop +7hllxxndzggvsrsh9zrktwoone2rxqczzbqpx +s2one574 +9fourseventzqfseven +62six1jdqcnqm4sixjsjvnx +five6fpktwo1l3 +five1d2 +fbnoneightknthree6 +4fbtxqsix6dxpjclhtlk +8vggprkfjrpqz8hhlvffmfiveone8 +rz29eightfghseven +two9four98cgmntgcncsmppz +bmrvhsz4twotwo +1oneeightthreeeightfourtwo +fivethreefvz9rzvzdqxqbmtxcnqc +3oneqvseight56lkxjk +6l99six +dsevenninefour48kjs +1sixxvrsevenone7 +lpmdghtcdj3nine28g77glk +tnkhrdone1hhtfzhnz +4cpzzht +foursixlmtmvrptqonejmssjldcnz8one +1bnd5slzddmkmvthreep4three +7jlrm +seventhree3 +vmjsix8sevenktbrxnftfour +gmczxbkcgsixseven6six +9oneone2hhnj8 +bbpeightjbtp4qfpks2four +twoeight3 +7mqnzkddm3hrqvhdvzgvgbnfcjtfour +4onesixgkcsix +chmkonenine1btgnldh +dpxzrthkjzdfkgpkdfvxveight5 +hxlfiveeight6 +3xlkg23fivethreesixeightksmc +nfourrjdqx4four6 +ptzrcdzthreer3phpkqlftwo24 +five7sevenjpjmmppfive +11sevenfivezcglsdkhqffivethreeseven5 +4eight5mjlkzrgnmlnmxntqmtlxmqlkjccttcpmgznfouroneightk +1oneightx +25fivevhtpblpfthreefour2xrzsix +eightfour41four8fiveone3 +pxmrbcqt22vfvzvvp4seven3 +3zqbqnnonesxtgz9eightonepqnld +d2six5dmlqczzrtp79brzzq +nine48 +ninevbjkjcd11qhtvb +six4nine1four2three5 +lcnvnxgzssonethree5mnvlsevenclrcpxbjbzvhlj9 +8onethree6n1 +98ddgtwov6nzvjzsscd +bklvmzjrqgfqx7 +5eight95ninetwo8 +qcrgbmzlztvtzrsjsixmvps2 +jsnmkd9seveneight +mkdzj28twohhmkctnsix +kb38sixttdhv9three +8eightninetwo +twoppvc7 +sixfour5sj6cdfjsskfb +242p28mxh8zrkvzp +sixnine5kqqrhxms +21sixfivepmspvnbscjc +threetjgcjpvflndq98 +sgonefivefive8 +nine5sevenfouronedsqpkgpthbeight3hrfdmcql +1sixkhqpcv5 +bsnnone3 +46sevenbnfsgnjldbtjnsjlmhvjrp8sevenx +twotwosevenonelzlpnmkdqq2rqthff +sixfournbzqfhff6jdlfhpt974 +5sixnvs2trqprv +nine8ninefourninelgcxntninektmhgjqrpd1 +hsjmpqkpkqzjnmgbz2nine6four +6two7jzfivethree25four +twobxtwosixfbhj5prfntlmfour +4lfsevenxsvvlmbzcnprsix +45lxtrdvsgfzzcmzjz7ninenvcvc +qfzvfthree9 +sevenlxfzcsevenseven4 +sjpqvfmx9twonep +qsljtpgdprjbkgzcnv5seveneightseven +zmhdkvltbxv3qftmt143 +5sevennh68 +sevenfour6rg +1sixonesixfournbbz9hfeight +sevensevenfour265one +88sixtwo +one5rcczjeightknjeight +qgftwohszlmfgjnn7 +xbtcpx3htlseven +7tnmvjdj4zlltljq8pdlnfghnd +jcpcgznmmnine4ninenine +zvdsbmlvn5four555 +threejpnbcbttggm9onefour4vtjhdfjsnz +7x7 +veightone1 +eightjlkckhqjfonesix94twoeightvggdqxgcn +2two2fdzkxgfour9sjtfourvxklldpvjm +849ndjngjhs65hgvdbcxps +fiveeightsevenseventfqzdxnhxkxgmnqmdjmxmdc35 +4sevenqnhvszlfour +seightwozbjkfsgk5three1tcpbshgfzeightfive +fourgxmqgpgbtwo4 +4nine9 +twokfvsvfgdtmtwom2xlkjseven +9one43ninedrtznff +zlfive98rfpmnsevennljlhbxhcn +lmfgxfdsevenchrkbhxlrrssbcqqf7fivectglcvrsrg8 +6twofive3two +mchm6 +mbvtbcjvv33rqfsllshb diff --git a/day1/out.txt b/day1/out.txt new file mode 100644 index 0000000..7c19ac4 --- /dev/null +++ b/day1/out.txt @@ -0,0 +1,6 @@ +rm day1 || echo "" +gcc day1.c ../libs/* -o day1 +./day1 input.txt a +53194 +./day1 input.txt b +53706 diff --git a/day1/testb.txt b/day1/testb.txt new file mode 100644 index 0000000..a0d7e2e --- /dev/null +++ b/day1/testb.txt @@ -0,0 +1 @@ +eightwothree diff --git a/libs/buffered_reader.c b/libs/buffered_reader.c index 51698b8..36d1095 100644 --- a/libs/buffered_reader.c +++ b/libs/buffered_reader.c @@ -13,7 +13,7 @@ filelines* flines(const char* filename) { //Define loop things bool loop = true; char *buffer; - size_t bufsize = 128; + size_t bufsize = 8; size_t characters; filelines *lines; @@ -23,21 +23,29 @@ filelines* flines(const char* filename) { //loop over file if (file) { while (loop) { - buffer = (char *)malloc(bufsize * sizeof(char)); + int size = bufsize; + buffer = (char *)malloc(size * sizeof(char)); int i = 0; while((c = getc(file)) != EOF) { - if (c == '\n' || i == bufsize) { - printf("breaking\n"); + if (c == '\n') { break; } + if (i == size) { + size <<= 1; + buffer = (char*)realloc(buffer, size * sizeof(char)); + } + //TODO: resize string if input is too big - printf("Adding %d\n", c); buffer[i] = c; i += 1; } if (i > 0){ + if (i == size) { + buffer = (char*)realloc(buffer, size * sizeof(char) + 1); + } + buffer[i] = '\0'; insert(lines, buffer); } if (c == EOF) { diff --git a/libs/data_structures.c b/libs/data_structures.c new file mode 100644 index 0000000..e69de29 diff --git a/libs/data_structures.h b/libs/data_structures.h new file mode 100644 index 0000000..378eeca --- /dev/null +++ b/libs/data_structures.h @@ -0,0 +1,20 @@ +#ifndef data_structures + +#include + +typedef struct { + size_t size; + size_t capacity; + size_t head; + size_t tail; + char* arr; +}list; + +void initList(list **l); +void pushs(list *l, char c); +char pops(list *l); +void enqueue(list *l, char c); +char dequeue(list *l); + +#endif + diff --git a/libs/utils.c b/libs/utils.c new file mode 100644 index 0000000..f623074 --- /dev/null +++ b/libs/utils.c @@ -0,0 +1,20 @@ +#include "utils.h" + +#include +#include +#include + +char *strreplace(char *s, const char *s1, const char *s2) { + char *p = strstr(s, s1); + while (p != NULL){ + size_t len1 = strlen(s1); + size_t len2 = strlen(s2); + if (len1 != len2){ + memmove(p + len2, p + len1, strlen(p + len1) + 1); + } + memcpy(p, s2, len2); + p = strstr(s, s1); + } + return s; +} + diff --git a/libs/utils.h b/libs/utils.h new file mode 100644 index 0000000..c32323f --- /dev/null +++ b/libs/utils.h @@ -0,0 +1,6 @@ +#ifndef utils + + +char *strreplace(char *s, const char *s1, const char *s2); + +#endif