├── 4clojure ├── 002.clj ├── 009.clj ├── 012.clj ├── 014.clj ├── 001.clj ├── 011.clj ├── 017.clj ├── 018.clj ├── 004.clj ├── 006.clj ├── 007.clj ├── 013.clj ├── 003.clj ├── 005.clj ├── 015.clj ├── 024.clj ├── 008.clj ├── 010.clj ├── 016.clj ├── 019.clj ├── 020.clj ├── 025.clj ├── README.md ├── 028.clj ├── 022.clj ├── 021.clj ├── 029.clj ├── 023.clj ├── 027.clj └── 026.clj ├── project_euler ├── .gitignore ├── 001 │ ├── solution.hs │ └── Solution.scala ├── 013 │ └── prog.py ├── 057 │ └── solution.py ├── 097 │ └── solution.py ├── 002 │ ├── Solution.scala │ └── solution.hs ├── 006 │ ├── solution.hs │ └── Solution.scala ├── 003 │ ├── solution.hs │ └── Solution.scala ├── 005 │ ├── solution.go │ └── Solution.scala ├── 028 │ └── solution.go ├── 091 │ └── solution.py ├── 018 │ └── triangle.txt └── README.md ├── codeforces ├── 952 │ ├── d │ │ └── solution.py │ ├── a │ │ └── solution.py │ ├── f │ │ └── solution.py │ └── b │ │ └── solution.py └── 1066 │ └── a │ └── solution.py ├── kattis ├── 2048 │ └── notes.md ├── howl │ └── solution.py ├── ovissa │ └── solution.py ├── sanic │ └── solution.py ├── velkomin │ └── solution.py ├── echoechoecho │ └── solution.py ├── vidsnuningur │ └── solution.py ├── _meta │ ├── requirements.txt │ └── README.md ├── findingana │ └── solution.py ├── fyi │ └── solution.py ├── oktalni │ └── solution.py ├── bing │ ├── notes.md │ └── solution.py ├── character │ └── solution.py ├── filip │ └── solution.py ├── generalchineseremainder │ └── notes.md ├── nsum │ └── solution.py ├── reversebinary │ └── solution.py ├── tetration │ └── solution.py ├── autori │ └── solution.py ├── haughtycuisine │ └── solution.py ├── planina │ └── solution.py ├── prozor │ └── notes.md ├── triarea │ └── solution.py ├── runlengthencodingrun │ └── notes.md ├── greetings2 │ └── solution.py ├── arithmetic │ └── solution.py ├── crophard │ └── notes.md ├── crosscountry │ └── notes.md ├── kornislav │ └── solution.py ├── simpleaddition │ └── solution.py ├── README.md ├── bossbattle │ └── solution.py ├── egypt │ ├── notes.md │ └── solution.py ├── 3dprinter │ └── solution.py ├── jobexpenses │ └── solution.py ├── okvir │ └── notes.md ├── pivot │ └── notes.md ├── tenis │ └── notes.md ├── crne │ └── solution.py ├── hissingmicrophone │ └── solution.py ├── pebblesolitaire2 │ └── notes.md ├── pieceofcake2 │ └── solution.py. ├── hello │ └── solution.c ├── minspantree │ └── notes.md ├── pascal │ └── notes.md ├── touchscreenkeyboard │ └── notes.md ├── bookclub │ └── notes.md ├── convexhull2 │ └── notes.md ├── shortestpath1 │ └── notes.md ├── thanos │ ├── notes.md │ └── solution.py ├── inflation │ ├── notes.md │ └── solution.py ├── jackolanternjuxtaposition │ └── solution.py ├── jumbojavelin │ └── solution.py ├── deathknight │ └── solution.py ├── fallingapart │ └── solution.py ├── lostmap │ └── notes.md ├── railroad2 │ └── solution.py ├── treasurehunt │ └── notes.md ├── deathstar │ └── notes.md ├── heimavinna │ └── solution.py ├── houselawn │ └── notes.md ├── islandhopping │ └── notes.md ├── nikola │ └── notes.md ├── terraces │ └── notes.md ├── whichbase │ ├── notes.md │ └── solution.py ├── perket │ └── notes.md ├── codecleanups │ └── notes.md ├── conversationlog │ └── notes.md ├── helpme │ └── notes.md ├── ratingproblems │ └── solution.py ├── babybites │ └── notes.md ├── boatparts │ ├── notes.md │ └── solution.py ├── faktor │ └── solution.c ├── tajna │ ├── notes.md │ └── solution.py ├── pebblesolitaire │ └── notes.md ├── pripreme │ ├── solution.py │ └── notes.md ├── r2 │ └── solution.c ├── slatkisi │ └── solution.py ├── sodaslurper │ └── solution.py ├── variablearithmetic │ └── notes.md ├── areal │ └── solution.c ├── baloni │ └── notes.md ├── boundingrobots │ └── notes.md ├── stickykeys │ └── solution.py ├── temperatureconfusion │ └── notes.md ├── countingclauses │ └── solution.cc ├── pizza2 │ └── solution.c ├── recenice │ └── notes.md ├── batterup │ └── solution.py ├── eulersnumber │ └── solution.py ├── helpaphd │ └── solution.py ├── krizaljka │ └── notes.md ├── oddecho │ └── solution.py ├── robotprotection │ └── notes.md ├── shopaholic │ └── solution.py ├── trollhunt │ ├── solution.py │ └── notes.md ├── empleh │ └── notes.md ├── everywhere │ └── solution.py ├── heartrate │ └── solution.py ├── ofugsnuid │ └── solution.py ├── skener │ └── solution.py ├── svada │ └── notes.md ├── flowshop │ └── notes.md ├── hailstone │ └── solution.py ├── meowfactor │ └── solution.py ├── multiplicationgame │ └── notes.md ├── nizovi │ └── notes.md ├── simonsays │ └── solution.py ├── tripletexting │ └── solution.py ├── finalexam2 │ ├── solution2.py │ └── solution.py ├── judgingmoose │ └── solution.py ├── kutevi │ └── notes.md ├── brexit │ └── notes.md ├── fbiuniversal │ └── notes.md ├── breakingbranches │ └── solution.py ├── cats │ └── notes.md ├── hothike │ └── solution.py ├── islands │ ├── notes.md │ └── solution.py ├── quiteaproblem │ └── solution.py ├── flyingsafely │ └── solution.py ├── threepowers │ └── notes.md ├── bestcompression │ └── solution.py ├── greedilyincreasing │ └── solution.py ├── limbo1 │ ├── notes.md │ └── solution.py ├── temperature │ └── solution.py ├── aboveaverage │ └── solution.py ├── climbingworm │ └── solution.py ├── electricaloutlets │ └── solution.py ├── quadrant │ └── solution.py ├── runningmom │ └── notes.md ├── shoppinglist │ └── solution.py ├── stringmatching │ └── notes.md ├── different │ └── solution.py ├── ostgotska │ └── solution.py ├── shoppinglisteasy │ └── solution.py ├── sortofsorting │ └── solution.py ├── teacherevaluation │ ├── notes.md │ └── solution.py ├── timeloop │ └── solution.c ├── vivoparc │ └── notes.md ├── honey │ └── notes.md ├── polarexpress │ └── notes.md ├── sith │ └── solution.py ├── fruitbaskets │ └── notes.md ├── nine │ └── solution.py ├── oddgnome │ └── solution.py ├── wizardofodds │ ├── notes.md │ └── solution.py ├── batteries │ └── solution.py ├── nodup │ └── solution.py ├── prinova │ └── notes.md ├── tutorial │ └── notes.md ├── differentdistances │ └── solution.py ├── reachableroads │ └── notes.md ├── thinkingofanumber │ └── notes.md ├── bishops │ └── solution.c ├── dasblinkenlights │ ├── notes.md │ └── solution.py ├── enlarginghashtables │ └── notes.md ├── basketballoneonone │ └── solution.py ├── ceremony │ └── solution.py ├── ivana │ └── notes.md ├── redrover │ └── solution.py ├── anthonyanddiablo │ └── solution.py ├── catalan │ └── solution.py ├── flexible │ └── solution.py ├── npuzzle │ ├── notes.md │ └── solution.py ├── polygonarea │ └── notes.md ├── shortestpath2 │ └── notes.md ├── sibice │ └── solution.py ├── tolower │ └── solution.py ├── chineseremainder │ └── notes.md ├── erase │ └── solution.py ├── icpcawards │ └── solution.py ├── lineup │ └── solution.py ├── nastyhacks │ └── solution.py ├── sumoftheothers │ └── solution.py ├── carrots │ └── solution.c ├── mapcolouring │ └── notes.md ├── palindromicpassword │ └── notes.md ├── password │ └── solution.py ├── simplicity │ └── solution.py ├── sok │ └── solution.py ├── twostones │ └── solution.c ├── froshweek2 │ └── notes.md ├── sretan │ └── solution.py ├── bits │ └── solution.py ├── guess │ └── solution.py ├── justaminute │ └── solution.py ├── missingnumbers │ └── solution.py ├── numberfun │ └── solution.py ├── coloring │ └── notes.md ├── countingtriangles │ └── notes.md ├── herman │ └── solution.c ├── ladder │ └── solution.c ├── aaah │ └── solution.c ├── calculator │ └── notes.md ├── mixedfractions │ └── solution.c ├── avion │ └── solution.py ├── breakingbad │ └── notes.md ├── irepeatmyself │ └── notes.md ├── register │ └── solution.py ├── roberthood │ └── notes.md ├── rot │ └── notes.md ├── savingforretirement │ └── solution.c ├── stickysituation │ └── solution.py ├── akcija │ └── solution.py ├── hardware │ └── notes.md ├── janitortroubles │ └── solution.py ├── lostlineup │ └── solution.py ├── snappereasy │ └── solution.py ├── snapperhard │ └── solution.py ├── splat │ └── notes.md ├── wheels │ └── notes.md ├── babelfish │ └── solution.py ├── beautifulprimes │ └── notes.md ├── bijele │ └── solution.c ├── keytocrypto │ └── solution.py ├── loorolls │ └── solution.py ├── pauleigon │ └── solution.c ├── cd │ └── solution.py ├── compass │ └── solution.c ├── frosting │ └── notes.md ├── redistribution │ └── solution.py ├── romans │ └── solution.cc ├── cold │ └── solution.c ├── communication │ └── solution.py ├── listgame │ ├── notes.md │ └── solution.py ├── ones │ └── solution.py ├── stopwatch │ └── solution.py ├── telephones │ └── notes.md ├── awkwardparty │ └── solution.py ├── convexhull │ └── notes.md ├── dicecup │ └── solution.c ├── qaly │ └── solution.cc ├── soylent │ └── solution.c ├── spavanac │ └── solution.c ├── znanstvenik │ └── notes.md ├── haypoints │ └── solution.py ├── incognito │ ├── notes.md │ └── solution.py ├── piperotation │ └── notes.md ├── putovanje │ └── solution.py ├── whatdoesthefoxsay │ └── solution.py ├── airlinehub │ └── notes.md ├── estimatingtheareaofacircle │ └── solution.c ├── limbo2 │ └── notes.md ├── smallestmultiple │ └── solution.py ├── symmetricorder │ └── solution.py ├── yinyangstones │ └── solution.c ├── quickbrownfox │ └── solution.py ├── textureanalysis │ └── solution.py ├── brokenswords │ └── solution.py ├── gcpc │ └── notes.md ├── sodasurpler │ └── solution.c ├── tarifa │ └── solution.c ├── architecture │ └── solution.py ├── cakeymccakeface │ └── notes.md ├── engineeringenglish │ └── solution.py ├── fiftyshades │ └── solution.py ├── recount │ └── solution.py ├── veci │ └── solution.py ├── apaxiaaans │ └── solution.c ├── billiard │ └── solution.py ├── bookingaroom │ └── solution.py ├── cetvrta │ └── solution.py ├── sifferprodukt │ └── solution.py ├── bond │ └── notes.md ├── bootstrappingnumber │ └── solution.py ├── cardtrick2 │ └── solution.py ├── minimumscalar │ └── solution.py ├── rijeci │ └── solution.c ├── datum │ └── solution.py ├── drunkvigenere │ └── solution.py ├── hangingout │ └── solution.py ├── intergalacticbidding │ └── notes.md ├── shatteredcake │ ├── solution.py │ └── solution.c ├── apaxianparent │ └── solution.py ├── beavergnaw │ └── solution.c ├── cetiri │ └── solution.py ├── chanukah │ └── solution.py ├── kitten │ └── solution.py ├── cups │ └── solution.py ├── friday │ └── solution.py ├── heliocentric │ └── solution.py ├── kleptography │ └── solution.py ├── triangleornaments │ └── notes.md ├── lektira │ └── solution.py ├── oddmanout │ └── solution.py ├── esej │ └── notes.md ├── oddities │ └── solution.c ├── sidewayssorting │ └── solution.py ├── downtime │ └── solution.py ├── triangle │ └── solution.py ├── artichoke │ └── solution.py ├── humancannonball2 │ └── solution.py ├── musicyourway │ └── solution.py ├── emergency │ └── solution.py ├── fodelsedagsmemorisering │ └── solution.py ├── walrusweights │ └── notes.md ├── polymul1 │ └── solution.py ├── primesieve │ └── notes.md ├── drmmessages │ └── solution.py ├── lost │ └── notes.md ├── synchronizinglists │ └── solution.py ├── airconditioned │ └── solution.py ├── allpairspath │ └── notes.md ├── grassseed │ └── solution.c ├── mathhomework │ └── solution.py ├── modulo │ └── solution.c ├── pet │ └── solution.c ├── segmentdistance │ └── notes.md ├── subseqhard │ └── solution.py ├── classy │ └── notes.md ├── lastfactorialdigit │ └── solution.cc ├── tritiling │ └── solution.c ├── fizzbuzz │ └── solution.c ├── halfacookie │ └── solution.py ├── memorymatch │ └── notes.md ├── rotatecut │ ├── solution.py │ └── notes.md ├── stararrangements │ └── solution.py ├── backspace │ └── solution.c ├── commercials │ └── solution.py ├── zoo │ └── solution.py ├── compoundwords │ └── solution.py ├── doorman │ └── solution.py ├── eventplanning │ └── solution.py ├── grandpabernie │ └── solution.py ├── luhnchecksum │ └── solution.py ├── server │ └── solution.c ├── bottledup │ └── solution.c ├── conformity │ └── solution.py ├── hangman │ └── solution.py ├── judging │ └── solution.py ├── luckynumber │ └── solution.py ├── pseudoprime │ └── solution.py ├── reducedidnumbers │ └── solution.py ├── schoolspirit │ └── solution.py ├── taisformula │ └── solution.py ├── freefood │ └── solution.cc ├── peasoup │ └── solution.py ├── pokerhand │ └── solution.cc ├── shortestpath3 │ └── notes.md ├── simon │ └── solution.c ├── anothercandies │ └── solution.c ├── barshelf │ └── solution.py ├── pervasiveheartmonitor │ └── solution.py ├── simplification │ └── notes.md ├── conundrum │ └── solution.c ├── isithalloween │ └── solution.cc ├── blockgame2 │ └── solution.py ├── bus │ └── solution.c ├── combinationlock │ └── solution.c ├── harshadnumbers │ └── solution.cc ├── juryjeopardy │ └── notes.md ├── mosquito │ └── solution.py ├── growlinggears │ └── notes.md ├── permutationencryption │ └── solution.py ├── quickestimate │ └── solution.c ├── rollcall │ └── solution.py ├── tri │ └── solution.py ├── unlockpattern │ └── solution.py ├── abc │ └── solution.c ├── bubbletea │ └── solution.py └── iboard │ └── solution.py ├── .gitignore ├── hacker.org ├── 103 │ └── solution.py ├── 105 │ └── solution.py ├── 106 │ └── solution.py ├── 127 │ └── solution.md ├── 132 │ └── solution.py ├── 136 │ └── solution.py ├── 139 │ └── solution.py ├── 143 │ └── solution.py ├── 156 │ └── solution.py ├── 157 │ └── solution.py ├── 158 │ └── solution.py ├── 159 │ └── solution.py ├── 162 │ └── solution.py ├── 002 │ └── solution.py ├── 017 │ └── solution.py ├── 097 │ └── solution.py ├── 003 │ └── solution.md ├── 046 │ └── solution.md ├── 025 │ └── solution.md ├── 029 │ └── solution.py ├── 057 │ └── solution.md ├── 010 │ └── solution.md ├── 011 │ └── solution.md ├── 024 │ └── solution.py ├── 087 │ └── solution.md ├── 001 │ └── solution.py └── 021 │ └── solution.md ├── uva ├── 10055 │ └── solution.cc ├── 10071 │ └── solution.cc ├── 10878 │ └── solution.py ├── 11151 │ └── notes.md ├── 13146 │ └── notes.md ├── 00458 │ └── solution.py └── 00673 │ └── notes.md ├── advent_of_code ├── 2015 │ ├── 08 │ │ └── test.txt │ └── 04 │ │ ├── part1.go │ │ └── part2.go ├── 2017 │ ├── 15 │ │ └── part1.py │ ├── 17 │ │ ├── part1.py │ │ └── part2.py │ ├── 01 │ │ ├── part1.py │ │ └── part2.py │ ├── 02 │ │ ├── part1.py │ │ └── part2.py │ ├── 05 │ │ ├── part1.py │ │ └── part2.py │ ├── 04 │ │ ├── part1.py │ │ └── part2.py │ └── 03 │ │ └── part1.py ├── 2018 │ └── 01 │ │ ├── part1.py │ │ └── part2.py ├── 2019 │ ├── 01 │ │ ├── part1.py │ │ └── part2.py │ └── 04 │ │ └── part1.py ├── 2020 │ ├── 10 │ │ ├── part1.py │ │ └── part2.py │ ├── 13 │ │ └── part1.py │ ├── 01 │ │ ├── part1.py │ │ └── part2.py │ ├── 03 │ │ └── part1.py │ ├── 02 │ │ ├── part2.py │ │ └── part1.py │ └── 06 │ │ └── part1.py ├── 2021 │ ├── 01 │ │ ├── part1.py │ │ └── part2.py │ ├── 07 │ │ ├── part1.py │ │ └── part2.py │ ├── 02 │ │ ├── part1.py │ │ └── part2.py │ └── 08 │ │ └── part1.py ├── 2022 │ ├── 01 │ │ ├── part1.py │ │ └── part2.py │ └── 04 │ │ └── part2.py └── 2025 │ ├── 01 │ └── part1.py │ ├── 09 │ └── part1.py │ └── 02 │ └── part1.py ├── spoj ├── README.md ├── ADDREV │ └── notes.md ├── HOTELS │ └── notes.md ├── GEOPROB │ └── solution.py ├── CRDS │ └── notes.md ├── TEST │ └── Main.scala ├── EIGHTS │ └── solution.py ├── SAMER08F │ ├── solution.cc │ └── notes.md └── ONP │ └── notes.md ├── rosalind ├── README.md └── bioinf_stronghold │ ├── rna │ └── solution.py │ ├── revc │ └── solution.lisp │ ├── fib │ └── solution.py │ └── hamm │ └── solution.c ├── leetcode ├── README.md ├── reverse-string │ └── solution.py ├── length-of-last-word │ └── solution.py ├── valid-palindrome │ └── solution.py ├── remove-duplicates-from-sorted-array │ └── solution.py ├── min-cost-climbing-stairs │ └── solution.py ├── powx-n │ └── solution.py ├── sum-of-square-numbers │ └── solution.py ├── group-anagrams │ └── solution.py └── two-sum │ └── solution.py ├── enigmagroup ├── basics │ ├── pre │ │ ├── 1 │ │ │ └── solution.md │ │ ├── 2 │ │ │ └── solution.md │ │ └── 3 │ │ │ └── solution.md │ ├── js │ │ ├── 1 │ │ │ └── solution.md │ │ ├── 2 │ │ │ └── solution.md │ │ ├── 3 │ │ │ └── solution.md │ │ ├── 4 │ │ │ └── solution.md │ │ └── 5 │ │ │ └── solution.md │ ├── xss │ │ └── 1 │ │ │ └── solution.md │ └── sql │ │ └── 1 │ │ └── solution.md ├── cryptography │ ├── 1 │ │ └── solution.md │ ├── 2 │ │ └── solution.md │ └── 3 │ │ └── solution.md └── steganography │ ├── 1 │ └── solution.md │ └── 2 │ └── solution.md ├── CONTRIBUTING.md ├── interview_training └── cracking_coding_interview │ └── p1.9.py ├── dailyprogrammer └── 2015 │ └── 11 │ └── 02 │ ├── easy.py │ └── easy.hs ├── README.md ├── hackerrank ├── challenges │ ├── a-very-big-sum │ │ └── solution.c │ ├── compare-the-triplets │ │ └── solution.c │ ├── staircase │ │ └── solution.c │ └── countingsort1 │ │ └── solution.c └── w26 │ └── game-with-cells │ └── solution.c └── primers └── art_optimal └── solution.py /4clojure/002.clj: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /4clojure/009.clj: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /4clojure/012.clj: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /4clojure/014.clj: -------------------------------------------------------------------------------- 1 | 8 2 | -------------------------------------------------------------------------------- /4clojure/001.clj: -------------------------------------------------------------------------------- 1 | true 2 | -------------------------------------------------------------------------------- /4clojure/011.clj: -------------------------------------------------------------------------------- 1 | [:b 2] 2 | -------------------------------------------------------------------------------- /4clojure/017.clj: -------------------------------------------------------------------------------- 1 | [6 7 8] 2 | -------------------------------------------------------------------------------- /4clojure/018.clj: -------------------------------------------------------------------------------- 1 | [6 7] 2 | -------------------------------------------------------------------------------- /4clojure/004.clj: -------------------------------------------------------------------------------- 1 | :a :b :c 2 | -------------------------------------------------------------------------------- /4clojure/006.clj: -------------------------------------------------------------------------------- 1 | :a :b :c 2 | -------------------------------------------------------------------------------- /4clojure/007.clj: -------------------------------------------------------------------------------- 1 | [1 2 3 4] 2 | -------------------------------------------------------------------------------- /4clojure/013.clj: -------------------------------------------------------------------------------- 1 | [20 30 40] 2 | -------------------------------------------------------------------------------- /4clojure/003.clj: -------------------------------------------------------------------------------- 1 | "HELLO WORLD" 2 | -------------------------------------------------------------------------------- /4clojure/005.clj: -------------------------------------------------------------------------------- 1 | (list 1 2 3 4) 2 | -------------------------------------------------------------------------------- /4clojure/015.clj: -------------------------------------------------------------------------------- 1 | (fn [x] (* x 2)) 2 | -------------------------------------------------------------------------------- /4clojure/024.clj: -------------------------------------------------------------------------------- 1 | #(reduce + %) 2 | -------------------------------------------------------------------------------- /4clojure/008.clj: -------------------------------------------------------------------------------- 1 | (set '(:a :b :c :d)) 2 | -------------------------------------------------------------------------------- /project_euler/.gitignore: -------------------------------------------------------------------------------- 1 | *.txt 2 | 3 | -------------------------------------------------------------------------------- /4clojure/010.clj: -------------------------------------------------------------------------------- 1 | ({:a 10, :b 20, :c 30} :b) 2 | -------------------------------------------------------------------------------- /codeforces/952/d/solution.py: -------------------------------------------------------------------------------- 1 | print('red') 2 | -------------------------------------------------------------------------------- /kattis/howl/solution.py: -------------------------------------------------------------------------------- 1 | print(input()+'O') 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.in 3 | *.out 4 | *.swp 5 | -------------------------------------------------------------------------------- /4clojure/016.clj: -------------------------------------------------------------------------------- 1 | (fn [n] (str "Hello, " n "!")) 2 | -------------------------------------------------------------------------------- /4clojure/019.clj: -------------------------------------------------------------------------------- 1 | (fn [x] (nth x (- (count x) 1))) 2 | -------------------------------------------------------------------------------- /4clojure/020.clj: -------------------------------------------------------------------------------- 1 | (fn [x] (nth x (- (count x) 2))) 2 | -------------------------------------------------------------------------------- /hacker.org/002/solution.py: -------------------------------------------------------------------------------- 1 | print(17 ^ 39 ^ 11) 2 | -------------------------------------------------------------------------------- /hacker.org/017/solution.py: -------------------------------------------------------------------------------- 1 | print(input()[::-1]) 2 | -------------------------------------------------------------------------------- /hacker.org/097/solution.py: -------------------------------------------------------------------------------- 1 | print(72311 ^ 89525) 2 | -------------------------------------------------------------------------------- /kattis/ovissa/solution.py: -------------------------------------------------------------------------------- 1 | print(len(input())) 2 | -------------------------------------------------------------------------------- /kattis/sanic/solution.py: -------------------------------------------------------------------------------- 1 | print(eval(input())-1) 2 | -------------------------------------------------------------------------------- /kattis/velkomin/solution.py: -------------------------------------------------------------------------------- 1 | print('VELKOMIN!') 2 | -------------------------------------------------------------------------------- /uva/13146/notes.md: -------------------------------------------------------------------------------- 1 | Classic levenshtein problem. 2 | -------------------------------------------------------------------------------- /kattis/echoechoecho/solution.py: -------------------------------------------------------------------------------- 1 | print((input()+' ')*3) 2 | -------------------------------------------------------------------------------- /kattis/vidsnuningur/solution.py: -------------------------------------------------------------------------------- 1 | print(input()[::-1]) 2 | -------------------------------------------------------------------------------- /4clojure/025.clj: -------------------------------------------------------------------------------- 1 | (fn [coll] (filter #(= (mod % 2) 1) coll)) 2 | -------------------------------------------------------------------------------- /kattis/_meta/requirements.txt: -------------------------------------------------------------------------------- 1 | requests 2 | beautifulsoup4 3 | -------------------------------------------------------------------------------- /codeforces/952/a/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | print(n%2) 3 | -------------------------------------------------------------------------------- /uva/11151/notes.md: -------------------------------------------------------------------------------- 1 | A strings / dynamic programming problem. 2 | -------------------------------------------------------------------------------- /advent_of_code/2015/08/test.txt: -------------------------------------------------------------------------------- 1 | "" 2 | "abc" 3 | "aaa\"aaa" 4 | "\x27" -------------------------------------------------------------------------------- /kattis/findingana/solution.py: -------------------------------------------------------------------------------- 1 | d = input() 2 | print(d[d.find('a'):]) 3 | -------------------------------------------------------------------------------- /kattis/fyi/solution.py: -------------------------------------------------------------------------------- 1 | print(1 if input().startswith('555') else 0) 2 | -------------------------------------------------------------------------------- /kattis/oktalni/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input(), 2) 2 | print(oct(n)[2:]) 3 | -------------------------------------------------------------------------------- /hacker.org/003/solution.md: -------------------------------------------------------------------------------- 1 | The answer is in a html comment on the page. 2 | -------------------------------------------------------------------------------- /hacker.org/046/solution.md: -------------------------------------------------------------------------------- 1 | Just write the human names for those colours. 2 | -------------------------------------------------------------------------------- /kattis/bing/notes.md: -------------------------------------------------------------------------------- 1 | With help from [xciarag](https://github.com/xciarag). 2 | -------------------------------------------------------------------------------- /kattis/character/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | print(2**(n) - n - 1) 3 | -------------------------------------------------------------------------------- /kattis/filip/solution.py: -------------------------------------------------------------------------------- 1 | print(max([s[::-1] for s in input().split()])) 2 | -------------------------------------------------------------------------------- /kattis/generalchineseremainder/notes.md: -------------------------------------------------------------------------------- 1 | See problem `chineseremainder`. 2 | -------------------------------------------------------------------------------- /kattis/nsum/solution.py: -------------------------------------------------------------------------------- 1 | input() and print(sum(map(int, input().split()))) 2 | -------------------------------------------------------------------------------- /kattis/reversebinary/solution.py: -------------------------------------------------------------------------------- 1 | print(int(bin(int(input()))[:1:-1], 2)) 2 | -------------------------------------------------------------------------------- /kattis/tetration/solution.py: -------------------------------------------------------------------------------- 1 | n = float(input()) 2 | print(n ** (1 / n)) 3 | -------------------------------------------------------------------------------- /spoj/README.md: -------------------------------------------------------------------------------- 1 | [My spoj.com profile](http://www.spoj.com/users/iandioch/) 2 | -------------------------------------------------------------------------------- /hacker.org/105/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | print(hex(n).split('x')[1]) 3 | -------------------------------------------------------------------------------- /kattis/autori/solution.py: -------------------------------------------------------------------------------- 1 | print(''.join([s[0] for s in input().split('-')])) 2 | -------------------------------------------------------------------------------- /kattis/haughtycuisine/solution.py: -------------------------------------------------------------------------------- 1 | input() 2 | print('\n'.join(input().split())) 3 | -------------------------------------------------------------------------------- /kattis/planina/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | e = 2**n + 1 3 | print(e**2) 4 | -------------------------------------------------------------------------------- /kattis/prozor/notes.md: -------------------------------------------------------------------------------- 1 | Annoying but reasonably trivial implementation problem. 2 | -------------------------------------------------------------------------------- /kattis/triarea/solution.py: -------------------------------------------------------------------------------- 1 | a, b = map(int, input().split()) 2 | print(a*b*0.5) 3 | -------------------------------------------------------------------------------- /rosalind/README.md: -------------------------------------------------------------------------------- 1 | [My rosalind.info profile](http://rosalind.info/users/iandioch/) -------------------------------------------------------------------------------- /kattis/runlengthencodingrun/notes.md: -------------------------------------------------------------------------------- 1 | Trivial strings problem. Runs in 0.00s in C++. 2 | -------------------------------------------------------------------------------- /4clojure/README.md: -------------------------------------------------------------------------------- 1 | My 4clojure profile is [here](http://www.4clojure.com/user/iandioch). 2 | -------------------------------------------------------------------------------- /kattis/greetings2/solution.py: -------------------------------------------------------------------------------- 1 | print(''.join(c if c in 'hy' else c + c for c in input())) 2 | -------------------------------------------------------------------------------- /hacker.org/143/solution.py: -------------------------------------------------------------------------------- 1 | print sum([x * (x - 1) for x in [y * y for y in xrange(3,11)]]) 2 | -------------------------------------------------------------------------------- /kattis/arithmetic/solution.py: -------------------------------------------------------------------------------- 1 | val = int(input(), 8) 2 | print('{0:x}'.format(val).upper()) 3 | -------------------------------------------------------------------------------- /kattis/crophard/notes.md: -------------------------------------------------------------------------------- 1 | See notes for problem `cropeasy`, as the solutions are identical. 2 | -------------------------------------------------------------------------------- /kattis/crosscountry/notes.md: -------------------------------------------------------------------------------- 1 | BFS will suffice here, using a heapq sorted by time taken so far. 2 | -------------------------------------------------------------------------------- /spoj/ADDREV/notes.md: -------------------------------------------------------------------------------- 1 | A numbers problem. Best keep the solution simple. C++ here runs in 0.00s. 2 | -------------------------------------------------------------------------------- /hacker.org/156/solution.py: -------------------------------------------------------------------------------- 1 | a = int(input(), 16) 2 | b = int(input(), 16) 3 | 4 | print(chr(a^b)) 5 | -------------------------------------------------------------------------------- /kattis/kornislav/solution.py: -------------------------------------------------------------------------------- 1 | d = sorted([int(x) for x in raw_input().split()]) 2 | print d[0]*d[2] 3 | -------------------------------------------------------------------------------- /kattis/simpleaddition/solution.py: -------------------------------------------------------------------------------- 1 | a = int(raw_input()) 2 | b = int(raw_input()) 3 | print a + b 4 | -------------------------------------------------------------------------------- /spoj/HOTELS/notes.md: -------------------------------------------------------------------------------- 1 | A combinatorics problem, ish. Just use a sliding window. Runs in 0.06s in C++. 2 | -------------------------------------------------------------------------------- /4clojure/028.clj: -------------------------------------------------------------------------------- 1 | (fn flat [d] 2 | (if (sequential? d) 3 | (mapcat flat d) 4 | [d])) 5 | -------------------------------------------------------------------------------- /kattis/README.md: -------------------------------------------------------------------------------- 1 | #kattis 2 | 3 | My kattis profile is [here](https://open.kattis.com/users/iandioch). 4 | -------------------------------------------------------------------------------- /kattis/bossbattle/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | if n <= 2: 3 | print(1) 4 | else: 5 | print(n-2) 6 | -------------------------------------------------------------------------------- /kattis/egypt/notes.md: -------------------------------------------------------------------------------- 1 | Simply do some maths. Pythagoras will say if it's a right angled triangle or not. 2 | -------------------------------------------------------------------------------- /4clojure/022.clj: -------------------------------------------------------------------------------- 1 | (fn len [coll] 2 | (if (= (next coll) nil) 3 | 1 4 | (+ (len (next coll)) 1))) 5 | -------------------------------------------------------------------------------- /hacker.org/025/solution.md: -------------------------------------------------------------------------------- 1 | Manually go to http://www.hacker.org/challenge/chal.php?id=25&answer=spaghetti 2 | -------------------------------------------------------------------------------- /hacker.org/029/solution.py: -------------------------------------------------------------------------------- 1 | b = input().split() 2 | for c in b: 3 | print(chr(int(c, 2)), end='') 4 | print() 5 | -------------------------------------------------------------------------------- /kattis/3dprinter/solution.py: -------------------------------------------------------------------------------- 1 | from math import log, ceil 2 | n = int(input()) 3 | print(1 + ceil(log(n, 2))) 4 | -------------------------------------------------------------------------------- /leetcode/README.md: -------------------------------------------------------------------------------- 1 | # leetcode 2 | 3 | You can view my leetcode profile [here](https://leetcode.com/o_d/). 4 | -------------------------------------------------------------------------------- /kattis/_meta/README.md: -------------------------------------------------------------------------------- 1 | In this directory are some scripts to compare my kattis progress to that of some friends. 2 | -------------------------------------------------------------------------------- /kattis/jobexpenses/solution.py: -------------------------------------------------------------------------------- 1 | input() 2 | d = sum(-c for c in map(int, input().split()) if c < 0) 3 | print(d) 4 | -------------------------------------------------------------------------------- /kattis/okvir/notes.md: -------------------------------------------------------------------------------- 1 | An annoying output formatting problem. Decide the chequerboard square colour by `(x+y)%2`. 2 | -------------------------------------------------------------------------------- /kattis/pivot/notes.md: -------------------------------------------------------------------------------- 1 | See [the blog post I wrote about this problem](http://mycode.doesnot.run/2018/04/11/pivot/). 2 | -------------------------------------------------------------------------------- /kattis/tenis/notes.md: -------------------------------------------------------------------------------- 1 | An annoying simulation-ish problem. Just check at all points that all rules are followed. 2 | -------------------------------------------------------------------------------- /project_euler/001/solution.hs: -------------------------------------------------------------------------------- 1 | main = print (foldl (+) 0 (filter (\n -> n `mod` 3 == 0 || n `mod` 5 == 0) [1..999])) -------------------------------------------------------------------------------- /enigmagroup/basics/pre/1/solution.md: -------------------------------------------------------------------------------- 1 | # Basics 1 2 | 3 | Just view the source of the page, the password is `39f13b`. 4 | -------------------------------------------------------------------------------- /kattis/crne/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | p = n//2 3 | q = p 4 | if p+q < n: 5 | p += 1 6 | print((p+1)*(q+1)) 7 | -------------------------------------------------------------------------------- /kattis/hissingmicrophone/solution.py: -------------------------------------------------------------------------------- 1 | v = 'ss' in input() 2 | if v: 3 | print('hiss') 4 | else: 5 | print('no hiss') 6 | -------------------------------------------------------------------------------- /kattis/pebblesolitaire2/notes.md: -------------------------------------------------------------------------------- 1 | Same solution as `pebblesolitaire`, but this time the memoisation is actually needed. 2 | -------------------------------------------------------------------------------- /kattis/pieceofcake2/solution.py.: -------------------------------------------------------------------------------- 1 | n, h, v = map(int, input().split()) 2 | ans = max(h, n-h)*max(v, n-v)*4 3 | print(ans) 4 | -------------------------------------------------------------------------------- /4clojure/021.clj: -------------------------------------------------------------------------------- 1 | (fn ans [coll index] 2 | (if (= index 0) 3 | (first coll) 4 | (ans (next coll) (- index 1)))) 5 | -------------------------------------------------------------------------------- /enigmagroup/basics/js/3/solution.md: -------------------------------------------------------------------------------- 1 | # JS 3 2 | 3 | Similar to JS 2, just view the source and see the password is `Sauc3`. 4 | -------------------------------------------------------------------------------- /hacker.org/057/solution.md: -------------------------------------------------------------------------------- 1 | The instructions to solve the puzzle are laid out clearly 2 | in the text on the linked page. 3 | -------------------------------------------------------------------------------- /kattis/hello/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | printf("Hello World!"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /kattis/minspantree/notes.md: -------------------------------------------------------------------------------- 1 | A minimum spanning tree problem. Solved here with Kruskal's algorithm, runs in 1.6s in Python3. 2 | -------------------------------------------------------------------------------- /kattis/pascal/notes.md: -------------------------------------------------------------------------------- 1 | A number theory and prime numbers problem. Find the lowest prime factor of N. Runs in 0.00s in C. 2 | -------------------------------------------------------------------------------- /kattis/touchscreenkeyboard/notes.md: -------------------------------------------------------------------------------- 1 | An ad-hoc strings problem. Simply find the distances (by manhattan distance) and sort. 2 | -------------------------------------------------------------------------------- /advent_of_code/2018/01/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | m = map(int, sys.stdin) 3 | f = 0 4 | for n in m: 5 | f += n 6 | print(f) 7 | -------------------------------------------------------------------------------- /kattis/bookclub/notes.md: -------------------------------------------------------------------------------- 1 | A bipartite matching problem. Use DFS to see if the current node can be matched. If not, print 'NO'. 2 | -------------------------------------------------------------------------------- /kattis/convexhull2/notes.md: -------------------------------------------------------------------------------- 1 | A convex hull problem. Run Graham scan to get the ordered set of points. Runs in 0.45s in Python3. 2 | -------------------------------------------------------------------------------- /kattis/shortestpath1/notes.md: -------------------------------------------------------------------------------- 1 | Can just build up a dict of the shortest path to each node through DFS or BFS, and then query it. 2 | -------------------------------------------------------------------------------- /kattis/thanos/notes.md: -------------------------------------------------------------------------------- 1 | Ad-hoc/simulation. Simply simulate each year until the population is too large. Runs in 0.03s in Python3. 2 | -------------------------------------------------------------------------------- /enigmagroup/basics/xss/1/solution.md: -------------------------------------------------------------------------------- 1 | # XSS 1 2 | 3 | Entering `">` into the text box will win the challenge. 4 | -------------------------------------------------------------------------------- /hacker.org/010/solution.md: -------------------------------------------------------------------------------- 1 | The answer is written in black text on the site's black background 2 | right below the problem statement. 3 | -------------------------------------------------------------------------------- /hacker.org/011/solution.md: -------------------------------------------------------------------------------- 1 | The answer is written in black text on the site's black background 2 | right below the problem statement. 3 | -------------------------------------------------------------------------------- /kattis/inflation/notes.md: -------------------------------------------------------------------------------- 1 | An ad-hoc problem. Sort the list and see if any element is greater than its index. Runs in 0.21s in Python3. 2 | -------------------------------------------------------------------------------- /kattis/jackolanternjuxtaposition/solution.py: -------------------------------------------------------------------------------- 1 | import functools 2 | print(functools.reduce(lambda a,b:a*b, map(int, input().split()))) 3 | -------------------------------------------------------------------------------- /kattis/jumbojavelin/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | f = list(map(int, (input() for _ in range(n)))) 3 | print(sum(f) - len(f) + 1) 4 | -------------------------------------------------------------------------------- /kattis/deathknight/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | lines = [input() for _ in range(n)] 3 | print(len([s for s in lines if 'CD' not in s])) 4 | -------------------------------------------------------------------------------- /kattis/fallingapart/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = sorted(map(int, input().split()), reverse=True) 3 | print(sum(s[::2]), sum(s[1::2])) 4 | -------------------------------------------------------------------------------- /kattis/lostmap/notes.md: -------------------------------------------------------------------------------- 1 | A minimum spanning tree problem. Simply apply Prim's (there are too many edges for Kruskal's). Runs in 2.74s in python3. 2 | -------------------------------------------------------------------------------- /kattis/railroad2/solution.py: -------------------------------------------------------------------------------- 1 | x, y = map(int, input().split()) 2 | if y % 2 == 0: 3 | print('possible') 4 | else: 5 | print('impossible') 6 | -------------------------------------------------------------------------------- /kattis/treasurehunt/notes.md: -------------------------------------------------------------------------------- 1 | Simply follow the simulation as stated. You know the player is Lost if they revisit a previously reached position. 2 | -------------------------------------------------------------------------------- /rosalind/bioinf_stronghold/rna/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | if __name__=='__main__': 4 | s = sys.stdin.readline() 5 | print s.replace("T", "U") -------------------------------------------------------------------------------- /advent_of_code/2017/01/part1.py: -------------------------------------------------------------------------------- 1 | s = input().strip() 2 | n = [s[i] for i in range(len(s)-1, -1, -1) if s[i] == s[i-1]] 3 | print(sum(map(int, n))) 4 | -------------------------------------------------------------------------------- /advent_of_code/2019/01/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | ans = 0 4 | for line in sys.stdin.readlines(): 5 | ans += int(line)//3 - 2 6 | print(ans) 7 | -------------------------------------------------------------------------------- /hacker.org/024/solution.py: -------------------------------------------------------------------------------- 1 | for s in input().strip().split('%'): 2 | if len(s) == 0: 3 | continue 4 | print(chr(int(s, 16)), end='') 5 | print() 6 | -------------------------------------------------------------------------------- /hacker.org/087/solution.md: -------------------------------------------------------------------------------- 1 | Look up the table of Netscape colours at https://www.cs.duke.edu/courses/fall01/cps004/resources/faq/browser_colors.html 2 | -------------------------------------------------------------------------------- /kattis/deathstar/notes.md: -------------------------------------------------------------------------------- 1 | A bit manipulation problem. Can simply take the bitwise OR of all of the input numbers in each line. Runs in 0.06s in C++. 2 | -------------------------------------------------------------------------------- /kattis/heimavinna/solution.py: -------------------------------------------------------------------------------- 1 | print(sum(1 if '-' not in s else (1 + int(s.split('-')[1]) - int(s.split('-')[0])) for s in input().split(';'))) 2 | 3 | -------------------------------------------------------------------------------- /kattis/houselawn/notes.md: -------------------------------------------------------------------------------- 1 | An ad-hoc problem. Just check if each mower is suitable, and output those with the lowest price. Runs in 0.02s in python3. 2 | -------------------------------------------------------------------------------- /kattis/islandhopping/notes.md: -------------------------------------------------------------------------------- 1 | A minimum spanning tree problem. Because it's a highly connected graph, should use Prim's. Runs in 1.76s in python3. 2 | -------------------------------------------------------------------------------- /kattis/nikola/notes.md: -------------------------------------------------------------------------------- 1 | A graph problem, ish. Could probably be solved by dynamic programming. Solved here by a priority queue. Runs in 0.34s in py3. 2 | -------------------------------------------------------------------------------- /kattis/terraces/notes.md: -------------------------------------------------------------------------------- 1 | Do a BFS from each point to every point at the same height as it. If any point is lower, then this area will not pool water. 2 | -------------------------------------------------------------------------------- /kattis/whichbase/notes.md: -------------------------------------------------------------------------------- 1 | Can just use standard library functions to parse number in base 8, 10, and 16. Some inputs won't be valid for all bases. 2 | -------------------------------------------------------------------------------- /advent_of_code/2017/01/part2.py: -------------------------------------------------------------------------------- 1 | s = input().strip() 2 | n = [s[i] for i in range(len(s)-1, -1, -1) if s[i] == s[i-len(s)//2]] 3 | print(sum(map(int, n))) 4 | -------------------------------------------------------------------------------- /kattis/perket/notes.md: -------------------------------------------------------------------------------- 1 | A complete search combinatorics problem. Can use a bitmask to figure out what to include in each step. Runs in 0.00s in C++ or C. 2 | -------------------------------------------------------------------------------- /hacker.org/157/solution.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | for i in range(len(s)//2): 3 | t = s[i*2:i*2 + 2] 4 | n = int(t, 16) 5 | print(chr(n^79), end='') 6 | print() 7 | -------------------------------------------------------------------------------- /kattis/codecleanups/notes.md: -------------------------------------------------------------------------------- 1 | A simulation problem. Iterate day-by-day, and if the dirtiness gets too much, clean up and rewind a day. Runs in 0.02s in python3. 2 | -------------------------------------------------------------------------------- /kattis/conversationlog/notes.md: -------------------------------------------------------------------------------- 1 | A strings problem. Simply keep a dict counting how many times each word was used, and sort. 2 | 3 | Runs in 0.21s in Python3. 4 | -------------------------------------------------------------------------------- /kattis/helpme/notes.md: -------------------------------------------------------------------------------- 1 | An annoying output formatting problem. Mostly consists of sorting things in arbitrary orders. Python makes this slightly less painful. 2 | -------------------------------------------------------------------------------- /kattis/ratingproblems/solution.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | tot = sum(int(input()) for _ in range(k)) 3 | print((tot -3*(n-k))/n, (tot +3*(n-k))/n) 4 | -------------------------------------------------------------------------------- /kattis/babybites/notes.md: -------------------------------------------------------------------------------- 1 | Trivial ad-hoc problem, just keep a count of the current token and see if it matches (ignore if input is `mumble`). Runs in 0s in C++11. 2 | -------------------------------------------------------------------------------- /kattis/boatparts/notes.md: -------------------------------------------------------------------------------- 1 | Simply add each item to a set, and as soon as the set size is equal to the number of parts of the boat, we know we have found our answer. 2 | -------------------------------------------------------------------------------- /kattis/faktor/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t, f; 5 | scanf("%d %d", &t, &f); 6 | printf("%d\n", t*(f-1) + 1); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /kattis/tajna/notes.md: -------------------------------------------------------------------------------- 1 | A simple matrix rotation. Fill up the matrix in backwards order, then read it in forwards order (or vice-versa, but I found this way clearer). 2 | -------------------------------------------------------------------------------- /4clojure/029.clj: -------------------------------------------------------------------------------- 1 | (fn caps [s] 2 | (clojure.string/join 3 | (filter 4 | #(contains? (set "ABCDEFGHIJKLMNOPQRSTUVWXYZ") %) 5 | s))) 6 | -------------------------------------------------------------------------------- /enigmagroup/basics/js/1/solution.md: -------------------------------------------------------------------------------- 1 | # JS 1 2 | 3 | Inspect element on the dropdown, and alter one of the entries (eg "James") to "Jane" instead, then just hit `Log in`. 4 | -------------------------------------------------------------------------------- /kattis/pebblesolitaire/notes.md: -------------------------------------------------------------------------------- 1 | Easy to do an exhaustive search. Probably not necessary, but I added memoisation of results, as there may be a lot of repeat lookups. 2 | -------------------------------------------------------------------------------- /kattis/pripreme/solution.py: -------------------------------------------------------------------------------- 1 | input() 2 | d = list(map(int, input().split())) 3 | a = sum(d) 4 | b = max(d) 5 | if b > (a-b): 6 | print(b*2) 7 | else: 8 | print(a) 9 | -------------------------------------------------------------------------------- /kattis/r2/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | printf("%d\n", 2*b-a); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /kattis/slatkisi/solution.py: -------------------------------------------------------------------------------- 1 | value, zeroes = map(int, input().split()) 2 | value /= 10**zeroes 3 | value = round(value, 0) 4 | value *= 10**zeroes 5 | print(int(value)) 6 | -------------------------------------------------------------------------------- /kattis/sodaslurper/solution.py: -------------------------------------------------------------------------------- 1 | a, b, c = map(int, input().split()) 2 | a += b 3 | tot = 0 4 | while a >= c: 5 | a -= c 6 | tot += 1 7 | a += 1 8 | print(tot) 9 | -------------------------------------------------------------------------------- /kattis/variablearithmetic/notes.md: -------------------------------------------------------------------------------- 1 | An output formatting problem, mostly. Just keep a dictionary of variable values, update when told, and output expressions when told. 2 | -------------------------------------------------------------------------------- /spoj/GEOPROB/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | for i in range(n): 4 | b,c,d = [int(x) for x in input().split(' ')] 5 | c1 = c-b 6 | d1 = d-b 7 | print(c1+c1-d1) 8 | -------------------------------------------------------------------------------- /enigmagroup/basics/sql/1/solution.md: -------------------------------------------------------------------------------- 1 | # SQLi 1 2 | 3 | Entering `"'` causes an SQL error. This shows there is an SQLi opportunity. `' OR '1'='1' --` will win the challenge. 4 | -------------------------------------------------------------------------------- /enigmagroup/cryptography/3/solution.md: -------------------------------------------------------------------------------- 1 | Given the hash `AE9345C2B5050F20AC66C7CB08C37A14`, I tried putting it into an MD5 rainbow table, and got back the answer `md5easy`. 2 | -------------------------------------------------------------------------------- /kattis/areal/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | double n; 6 | scanf("%lf", &n); 7 | printf("%.7lf\n", sqrt(n)*4); 8 | } 9 | -------------------------------------------------------------------------------- /kattis/baloni/notes.md: -------------------------------------------------------------------------------- 1 | An ad-hoc arrays problem. Could be solved much faster, but this naive solution (continuously aiming towards the leftmost arrow) runs in 1.73s in C++. 2 | -------------------------------------------------------------------------------- /kattis/boundingrobots/notes.md: -------------------------------------------------------------------------------- 1 | A simple simulation problem. Run the inputs twice, once ignoring walls and once paying attention to them, and output the differing results. 2 | -------------------------------------------------------------------------------- /kattis/stickykeys/solution.py: -------------------------------------------------------------------------------- 1 | d = input() 2 | s = '' 3 | c = '' 4 | for e in d: 5 | if e == c: 6 | continue 7 | s += e 8 | c = e 9 | print(s) 10 | -------------------------------------------------------------------------------- /kattis/temperatureconfusion/notes.md: -------------------------------------------------------------------------------- 1 | A simple maths problem. Just do the conversion, and put the fraction in its lowest terms by finding the GCD. Runs in 0.02s in Python 3. 2 | -------------------------------------------------------------------------------- /kattis/countingclauses/solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int d; 5 | scanf("%d", &d); 6 | printf("%ssatisfactory\n", d < 8 ? "un" : ""); 7 | } 8 | -------------------------------------------------------------------------------- /kattis/pizza2/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int r, c; 5 | scanf("%d %d", &r, &c); 6 | printf("%lf\n", 100*((c-r)*(c-r) + 0.0)/(r*r)); 7 | } 8 | -------------------------------------------------------------------------------- /kattis/recenice/notes.md: -------------------------------------------------------------------------------- 1 | We can prebuild a list of the strings for all numbers up to 1000, and then just iterate this list to find a matching solution. Runs in 0.02s in python3. 2 | -------------------------------------------------------------------------------- /hacker.org/106/solution.py: -------------------------------------------------------------------------------- 1 | nums = map(int, input().split(',')) 2 | 3 | s = '' 4 | for num in nums: 5 | t = bin(num)[2:].zfill(8) 6 | s += t 7 | 8 | print(int(s, 2)) 9 | 10 | -------------------------------------------------------------------------------- /kattis/batterup/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = map(int, input().split()) 3 | 4 | v = [] 5 | for m in s: 6 | if m >= 0: 7 | v.append(m) 8 | 9 | print(sum(v)/len(v)) 10 | -------------------------------------------------------------------------------- /kattis/eulersnumber/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | from math import factorial 3 | n = int(input()) 4 | print(sum(1/factorial(i) for i in range(min(n+1, 20)))) 5 | 6 | main() 7 | -------------------------------------------------------------------------------- /kattis/helpaphd/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for _ in range(n): 3 | s = input() 4 | if s == 'P=NP': 5 | print('skipped') 6 | else: 7 | print(eval(s)) 8 | -------------------------------------------------------------------------------- /kattis/krizaljka/notes.md: -------------------------------------------------------------------------------- 1 | A string output formatting problem. Simply iterate over A, and find the character matching in B, then format in the specified way. Runs in 0.00s in C++11. 2 | -------------------------------------------------------------------------------- /kattis/oddecho/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | for i in range(n): 4 | s = input() 5 | if i % 2 == 0: 6 | print(s) 7 | 8 | main() 9 | -------------------------------------------------------------------------------- /kattis/robotprotection/notes.md: -------------------------------------------------------------------------------- 1 | A geometry-ish problem. Run Graham scan to get the convex hull, then use the shoelace formula to get the area of this polygon. Runs in 0.79s in python3. 2 | -------------------------------------------------------------------------------- /kattis/shopaholic/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | v = sorted(map(int, input().split()), reverse=True) 3 | ans = 0 4 | for i in range(2, len(v), 3): 5 | ans += v[i] 6 | print(ans) 7 | -------------------------------------------------------------------------------- /kattis/trollhunt/solution.py: -------------------------------------------------------------------------------- 1 | from math import ceil 2 | b, k, g = map(int, input().split()) 3 | gr = k//g 4 | ans = 0 5 | while b > 1: 6 | b -= gr 7 | ans += 1 8 | print(ans) 9 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Feel free to file issues if you find something astray! But the code here consists of my submissions to various archives, so it doesn't really make sense to make any PRs. 2 | -------------------------------------------------------------------------------- /hacker.org/136/solution.py: -------------------------------------------------------------------------------- 1 | s = ['0' if c == 'a' else '1' for c in input()] 2 | 3 | for i in range(len(s)//8): 4 | t = ''.join(s[i*8:(i+1)*8]) 5 | n = int(t,2) 6 | print(chr(n), end='') 7 | -------------------------------------------------------------------------------- /kattis/empleh/notes.md: -------------------------------------------------------------------------------- 1 | Simply fill in an 8x8 grid with the requested symbols, and then output it. A grid square background is black if its 0-indexed x and y positions sum to an odd number. 2 | -------------------------------------------------------------------------------- /kattis/everywhere/solution.py: -------------------------------------------------------------------------------- 1 | n_tests = int(input()) 2 | for test in range(n_tests): 3 | n = int(input()) 4 | d = set() 5 | for _ in range(n): 6 | d.add(input()) 7 | print(len(d)) 8 | -------------------------------------------------------------------------------- /kattis/heartrate/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for _ in range(n): 3 | x, t = input().split() 4 | x = int(x) 5 | t = float(t) 6 | print((x-1)*60/t, x*60/t, (x+1)*60/t) 7 | -------------------------------------------------------------------------------- /kattis/ofugsnuid/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | from sys import stdin, stdout 3 | write = stdout.write 4 | for x in stdin.readlines()[:0:-1]: 5 | write(x) 6 | 7 | main() 8 | -------------------------------------------------------------------------------- /kattis/skener/solution.py: -------------------------------------------------------------------------------- 1 | oh, ow, nh, nw = map(int, input().split()) 2 | for y in range(oh): 3 | s = input() 4 | for j in range(nh): 5 | print(''.join([c*nw for c in s])) 6 | -------------------------------------------------------------------------------- /kattis/svada/notes.md: -------------------------------------------------------------------------------- 1 | A binary search problem. You can test if time T will allow the first group of monkeys to pick the same number as the second group will break. Runs in 0.00s in C++. 2 | -------------------------------------------------------------------------------- /spoj/CRDS/notes.md: -------------------------------------------------------------------------------- 1 | A simple number sequences problem. On paper, draw the pyramid up to a few levels, and see the pattern for `d[N]` is `d[N-1] + N + (N+1)*2`. 2 | 3 | Runs in 0.02s in C++. 4 | -------------------------------------------------------------------------------- /uva/10071/solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long int a, b; 5 | while (scanf("%ld %ld", &a, &b) != EOF) { 6 | printf("%ld\n", 2*a*b); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /4clojure/023.clj: -------------------------------------------------------------------------------- 1 | (fn rev [coll] 2 | (if (= (next coll) nil) 3 | (list (first coll)) 4 | (concat 5 | (rev (next coll)) 6 | (list (first coll))))) 7 | -------------------------------------------------------------------------------- /kattis/flowshop/notes.md: -------------------------------------------------------------------------------- 1 | A DP -ish problem. For point (x, y) in the grid, the quickest time it can be finished is `cost(x, y) + max(time(x-1,y), time(x, y-1)`. 2 | 3 | Runs in 0.08s in C++. 4 | -------------------------------------------------------------------------------- /kattis/hailstone/solution.py: -------------------------------------------------------------------------------- 1 | def h(n): 2 | if n == 1: 3 | return 1 4 | if n % 2 == 0: 5 | return n + h(n//2) 6 | return n + h(3*n + 1) 7 | 8 | print(h(int(input()))) 9 | -------------------------------------------------------------------------------- /kattis/meowfactor/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | ans = 1 3 | for m in range(int(n**(1/9.0)) + 1, 0, -1): 4 | if n % m**9 == 0: 5 | ans = m 6 | break 7 | 8 | print(ans) 9 | -------------------------------------------------------------------------------- /kattis/multiplicationgame/notes.md: -------------------------------------------------------------------------------- 1 | An annoying ad-hoc prime factoring problem. We know if there are at least 3 different prime factors, then the opponent can cause a draw. Runs in 0.35s in C++. 2 | -------------------------------------------------------------------------------- /kattis/nizovi/notes.md: -------------------------------------------------------------------------------- 1 | Simple string formatting problem. Doing it in 2 passes to remove empty lines/hanging commas seems like an easier approach than formatting it like that in the beginning. 2 | -------------------------------------------------------------------------------- /kattis/simonsays/solution.py: -------------------------------------------------------------------------------- 1 | ORDER = 'Simon says' 2 | n = int(input()) 3 | for t in range(n): 4 | line = input() 5 | if (line[:len(ORDER)] == ORDER): 6 | print(line[len(ORDER):]) 7 | -------------------------------------------------------------------------------- /kattis/tripletexting/solution.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | n = len(s)//3 3 | out = [] 4 | for i in range(n): 5 | x = [s[i], s[i+n], s[i+n+n]] 6 | out.append(sorted(x)[1]) 7 | print(''.join(out)) 8 | -------------------------------------------------------------------------------- /advent_of_code/2017/02/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | d = [] 4 | for line in sys.stdin.readlines(): 5 | v = list(map(int, line.strip().split())) 6 | d.append(max(v) - min(v)) 7 | 8 | print(sum(d)) 9 | -------------------------------------------------------------------------------- /advent_of_code/2020/01/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | nums = [int(n) for n in sys.stdin.readlines()] 3 | numset = set(nums) 4 | for n in nums: 5 | if 2020-n in numset: 6 | print(n * (2020-n)) 7 | -------------------------------------------------------------------------------- /kattis/finalexam2/solution2.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(raw_input()) 3 | ans = [raw_input() for _ in range(n)] 4 | print(sum(1 for i in xrange(n-1) if ans[i] == ans[i+1])) 5 | 6 | main() 7 | -------------------------------------------------------------------------------- /kattis/judgingmoose/solution.py: -------------------------------------------------------------------------------- 1 | a, b = map(int, input().split()) 2 | if a + b == 0: 3 | print('Not a moose') 4 | elif a == b: 5 | print('Even', a+b) 6 | else: 7 | print('Odd', max(a,b)*2) 8 | -------------------------------------------------------------------------------- /kattis/kutevi/notes.md: -------------------------------------------------------------------------------- 1 | Build a queue of angles as you find new ones you can create. For each item in the queue, add any new angles you can create by combining this angle and one of the given ones. 2 | -------------------------------------------------------------------------------- /uva/00458/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | for line in sys.stdin: 5 | print(''.join(chr(ord(c)-7) for c in line.strip())) 6 | 7 | if __name__ == '__main__': 8 | main() 9 | -------------------------------------------------------------------------------- /enigmagroup/cryptography/1/solution.md: -------------------------------------------------------------------------------- 1 | # Cryptography 1 2 | 3 | The string `EbGguvEgrRavfNcvRprbsShpXvatCvFf` is given. 4 | 5 | This is ROT13-d. Deciphered, it says `RoTthiRteEnisApiEceofFucKingPiSs`. 6 | -------------------------------------------------------------------------------- /interview_training/cracking_coding_interview/p1.9.py: -------------------------------------------------------------------------------- 1 | def isrot(s1, s2): 2 | return s1 in (s2 + s2) 3 | 4 | if __name__ == '__main__': 5 | s1, s2 = input().split() 6 | print(isrot(s1, s2)) 7 | -------------------------------------------------------------------------------- /kattis/brexit/notes.md: -------------------------------------------------------------------------------- 1 | A graph traversal problem. Any time a country leaves, add all of its unleft neighbours to a FIFO queue, to decide if they should leave themselves. 2 | 3 | Took 1.83 seconds in py3. 4 | -------------------------------------------------------------------------------- /kattis/fbiuniversal/notes.md: -------------------------------------------------------------------------------- 1 | Just compute the checksum, mod it by 27, if the answer is not the same as the check digit, the answer is invalid. Then if the check is correct, convert the input to base 10. 2 | -------------------------------------------------------------------------------- /rosalind/bioinf_stronghold/revc/solution.lisp: -------------------------------------------------------------------------------- 1 | (princ 2 | (map 'string 3 | #'(lambda (x) (if (char= x #\A) #\T (if (char= x #\G) #\C (if (char= x #\C) #\G #\A)))) 4 | (reverse 5 | (string (read))))) -------------------------------------------------------------------------------- /hacker.org/139/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | for line in sys.stdin.readlines(): 4 | for c in line: 5 | if c in "., !?-0123456789()\"'": 6 | continue 7 | if c == c.upper(): 8 | print(c, end='') 9 | -------------------------------------------------------------------------------- /kattis/breakingbranches/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | if n % 2 == 0: 4 | print('Alice') 5 | print(1) 6 | else: 7 | print('Bob') 8 | 9 | main() 10 | -------------------------------------------------------------------------------- /kattis/cats/notes.md: -------------------------------------------------------------------------------- 1 | A minimum spanning tree problem. Just apply Prim's and see if the total MST cost is <= `m - c`. Kruskal's will be too slow, as the graph is completely connected. Runs in 5.12s in Python3. 2 | -------------------------------------------------------------------------------- /kattis/hothike/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | day = list(map(int, input().split())) 3 | best = min((i for i in range(n-2)), key=lambda i: max(day[i], day[i+2])) 4 | print(best+1, max(day[best], day[best+2])) 5 | -------------------------------------------------------------------------------- /kattis/islands/notes.md: -------------------------------------------------------------------------------- 1 | O(n^2) solution to this sequence problem. 2 | 3 | Simply find the minimum value in the subsequence from `i` to `j`, and see if the values on either side are less than this minimum. 4 | -------------------------------------------------------------------------------- /kattis/quiteaproblem/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | lines = sys.stdin.readlines() 4 | for line in lines: 5 | if 'problem' in line.lower(): 6 | print('yes') 7 | else: 8 | print('no') 9 | -------------------------------------------------------------------------------- /leetcode/reverse-string/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseString(self, s): 3 | """ 4 | :type s: str 5 | :rtype: str 6 | """ 7 | return s[::-1] 8 | 9 | -------------------------------------------------------------------------------- /kattis/flyingsafely/solution.py: -------------------------------------------------------------------------------- 1 | num_tests = int(input()) 2 | for _ in range(num_tests): 3 | cities, pilots = map(int, input().split()) 4 | print(cities-1) 5 | for _ in range(pilots): 6 | input() 7 | -------------------------------------------------------------------------------- /kattis/threepowers/notes.md: -------------------------------------------------------------------------------- 1 | A combinatorics / bit manipulation problem. The `n`th combination will have a binary representation `x`. the `m`th bit of `x` will be 1 if the `m`th power of 3 should be in the output. 2 | -------------------------------------------------------------------------------- /kattis/bestcompression/solution.py: -------------------------------------------------------------------------------- 1 | a, b = [int(x) for x in raw_input().split()] 2 | 3 | maxn = 0 4 | for i in xrange(b+1): 5 | maxn += 2**i 6 | 7 | if maxn >= a: 8 | print 'yes' 9 | else: 10 | print 'no' 11 | -------------------------------------------------------------------------------- /kattis/greedilyincreasing/solution.py: -------------------------------------------------------------------------------- 1 | input() 2 | s = list(map(int, input().split())) 3 | 4 | a = [s[0]] 5 | for c in s[1:]: 6 | if c > a[-1]: 7 | a.append(c) 8 | 9 | print(len(a)) 10 | print(*a) 11 | -------------------------------------------------------------------------------- /kattis/limbo1/notes.md: -------------------------------------------------------------------------------- 1 | Number theory. The ans = `triangle(a+b)+1+b`, where `triangle(n)` is the nth triangular number (`(n + (n+1))//2`). That this is the answer can be seen from some inspection. Runs in 0.02s in py3. 2 | -------------------------------------------------------------------------------- /kattis/temperature/solution.py: -------------------------------------------------------------------------------- 1 | z, d = map(int, input().split()) 2 | 3 | if z == 0 and d == 1: 4 | print('ALL GOOD') 5 | elif d == 1: 6 | print('IMPOSSIBLE') 7 | else: 8 | print ('{:.7f}'.format(z/(1-d))) 9 | -------------------------------------------------------------------------------- /project_euler/013/prog.py: -------------------------------------------------------------------------------- 1 | total = 0 2 | 3 | with open("nums.txt", 'r') as file: 4 | lines = file.readlines() 5 | for line in lines: 6 | num = int(line) 7 | total += num 8 | 9 | print str(total)[0:10] 10 | -------------------------------------------------------------------------------- /kattis/aboveaverage/solution.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | for _ in range(t): 3 | n, *s = map(int, input().split()) 4 | avg = sum(s)/n 5 | a = len([i for i in s if i > avg]) 6 | print('{:.3f}%'.format(100*a/n)) 7 | -------------------------------------------------------------------------------- /kattis/climbingworm/solution.py: -------------------------------------------------------------------------------- 1 | a, b, h = map(int, input().split()) 2 | 3 | ans = 0 4 | c = 0 5 | while True: 6 | ans += 1 7 | c += a 8 | if c >= h: 9 | break 10 | c -= b 11 | print(ans) 12 | -------------------------------------------------------------------------------- /kattis/electricaloutlets/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | for _ in range(n): 4 | k, *o = map(int, input().split()) 5 | ans = 1 - len(o) + sum(o) 6 | print(ans) 7 | 8 | main() 9 | -------------------------------------------------------------------------------- /kattis/quadrant/solution.py: -------------------------------------------------------------------------------- 1 | x = int(input()) 2 | y = int(input()) 3 | 4 | if x > 0: 5 | if y > 0: 6 | print(1) 7 | else: 8 | print(4) 9 | else: 10 | if y > 0: 11 | print(2) 12 | else: 13 | print(3) 14 | -------------------------------------------------------------------------------- /kattis/runningmom/notes.md: -------------------------------------------------------------------------------- 1 | A graph problem. Just do a DFS (will not work with BFS!) from each start point, and see if you ever come across the same place twice. Memoise for added speed. 2 | 3 | Runs in 0.03s in Python3. 4 | -------------------------------------------------------------------------------- /kattis/shoppinglist/solution.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | items = set(input().split()) 3 | for _ in range(n-1): 4 | items &= set(input().split()) 5 | 6 | print(len(items)) 7 | print('\n'.join(sorted(items))) 8 | -------------------------------------------------------------------------------- /kattis/stringmatching/notes.md: -------------------------------------------------------------------------------- 1 | String searching problem. A naive implementation is too slow, so use Knuth-Morris-Pratt algorithm. My byte-for-byte identical program runs in 0.17s in Python2, and TLE's with >1s in Python3. 2 | -------------------------------------------------------------------------------- /4clojure/027.clj: -------------------------------------------------------------------------------- 1 | (fn pal [s] 2 | (cond 3 | (= (count s) 0) true 4 | (= (count s) 1) true 5 | :else (and 6 | (= (first s) (last s)) 7 | (pal (take (- (count s) 2) (rest s)))))) 8 | -------------------------------------------------------------------------------- /kattis/different/solution.py: -------------------------------------------------------------------------------- 1 | s = raw_input() 2 | while s is not None: 3 | parts = s.split() 4 | a = int(parts[0]) 5 | b = int(parts[1]) 6 | print abs(a-b) 7 | try: 8 | s = raw_input() 9 | except: 10 | break 11 | -------------------------------------------------------------------------------- /kattis/ostgotska/solution.py: -------------------------------------------------------------------------------- 1 | words = input().split() 2 | t = len(words) 3 | f = len([w for w in words if 'ae' in w]) 4 | if f*10/t >= 4: 5 | print('dae ae ju traeligt va') 6 | else: 7 | print('haer talar vi rikssvenska') 8 | -------------------------------------------------------------------------------- /kattis/shoppinglisteasy/solution.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | items = set(input().split()) 3 | for _ in range(n-1): 4 | items &= set(input().split()) 5 | 6 | print(len(items)) 7 | print('\n'.join(sorted(items))) 8 | -------------------------------------------------------------------------------- /kattis/sortofsorting/solution.py: -------------------------------------------------------------------------------- 1 | while True: 2 | n = int(input()) 3 | if n == 0: 4 | break 5 | s = [input() for _ in range(n)] 6 | s.sort(key=lambda x:x[0:2]) 7 | print('\n'.join(s)) 8 | print() 9 | -------------------------------------------------------------------------------- /kattis/teacherevaluation/notes.md: -------------------------------------------------------------------------------- 1 | We only need to know the number of students added, not the scores that were added, so keep adding perfect scores until the average is > P. 2 | 3 | It will only be impossible if P equals 100. 4 | -------------------------------------------------------------------------------- /kattis/timeloop/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n; 5 | scanf("%d", &n); 6 | int i; 7 | for (i = 1; i <= n; ++i) { 8 | printf("%d Abracadabra\n", i); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /kattis/vivoparc/notes.md: -------------------------------------------------------------------------------- 1 | A graph colouring problem. Simply find any arrangement that fits the given requirement of no node being adjacent to a node of the same colour. An exhaustive search will do this, and runs in 0.32s in C. 2 | -------------------------------------------------------------------------------- /spoj/TEST/Main.scala: -------------------------------------------------------------------------------- 1 | object Main extends App { 2 | var ok = true 3 | while(ok){ 4 | val line = scala.io.StdIn.readLine() 5 | if (line == "42") { 6 | ok = false 7 | }else{ 8 | println(line) 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /kattis/finalexam2/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | import sys 3 | n = int(sys.stdin.readline()) 4 | ans = [sys.stdin.readline() for _ in range(n)] 5 | print(sum(1 for i in range(n-1) if ans[i] == ans[i+1])) 6 | 7 | main() 8 | -------------------------------------------------------------------------------- /kattis/honey/notes.md: -------------------------------------------------------------------------------- 1 | Given that N < 15, it is easy to precompute the answers to all possible inputs, in a slow exhaustive search. However, I didn't do this, because the answers follow [OEIS sequence A002898](http://oeis.org/A002898). 2 | -------------------------------------------------------------------------------- /kattis/polarexpress/notes.md: -------------------------------------------------------------------------------- 1 | A sorting problem. Must find a strategy to sort the given list number by number, with putting each number into its sorted place taking <= 5 swaps to fit the solution constraint. Runs in 0.03s in python3. 2 | -------------------------------------------------------------------------------- /kattis/sith/solution.py: -------------------------------------------------------------------------------- 1 | name = input() 2 | a = int(input()) 3 | b = int(input()) 4 | c = int(input()) 5 | if a - b > 0: 6 | print('VEIT EKKI') 7 | elif a - b == c: 8 | print('JEDI') 9 | else: 10 | print('SITH') 11 | -------------------------------------------------------------------------------- /enigmagroup/basics/js/5/solution.md: -------------------------------------------------------------------------------- 1 | # JS 5 2 | 3 | The password is shown in the source to be `%41%53%43%49%49%2D%43%68%61%72%74`. Running it through `unescape()` in a JS interpreter returns `ASCII-Chart`, which is the correct password. 4 | -------------------------------------------------------------------------------- /kattis/fruitbaskets/notes.md: -------------------------------------------------------------------------------- 1 | A numbersy/combinatoricsy question. I take the sum of all possible combinations, and then subtract the ones which are below 200 by finding every set of 3 that is less than 200 (as every set of 4 will be >=200). 2 | -------------------------------------------------------------------------------- /kattis/nine/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | for _ in range(n): 4 | d = int(input()) 5 | d -= 1 6 | a = (8 * pow(9, d, 1000000007)) % 1000000007 7 | print(a) 8 | 9 | main() 10 | -------------------------------------------------------------------------------- /kattis/oddgnome/solution.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | for _ in range(t): 3 | _, *g = list(map(int, input().split())) 4 | for i in range(1, len(g) - 1): 5 | if g[i] != g[i-1]+1: 6 | print(i+1) 7 | break 8 | -------------------------------------------------------------------------------- /kattis/wizardofodds/notes.md: -------------------------------------------------------------------------------- 1 | A maths problem. Optimal questions will result in a binary search for the answer, so just check if 2**k >= n. Can avoid calculating any 2**k if that would be larger than the max input. Runs in 0.02s in Python3. 2 | -------------------------------------------------------------------------------- /kattis/batteries/solution.py: -------------------------------------------------------------------------------- 1 | MAX = 4715 2 | dp = [0, 0] 3 | i = 1 4 | while len(dp) < MAX: 5 | dp += [i]*i 6 | i += 1 7 | 8 | while True: 9 | n = int(input()) 10 | if n == 0: 11 | break 12 | print(dp[n]) 13 | -------------------------------------------------------------------------------- /kattis/nodup/solution.py: -------------------------------------------------------------------------------- 1 | s = input().split() 2 | v = set() 3 | ok = True 4 | for w in s: 5 | if w in v: 6 | ok = False 7 | break 8 | v.add(w) 9 | if ok: 10 | print('yes') 11 | else: 12 | print('no') 13 | -------------------------------------------------------------------------------- /kattis/prinova/notes.md: -------------------------------------------------------------------------------- 1 | The solution to this problem will either be (an odd number < difference 1 away from) `A`, `B`, or the midpoint between two elements in the given sequence (after being sorted). This runs in `O(n)` (0.02s in Python 3). 2 | -------------------------------------------------------------------------------- /kattis/tutorial/notes.md: -------------------------------------------------------------------------------- 1 | Simply performing the given operation will cause a TLE for large factorials. You can speed it up by not performing the operation for any number obviously bigger than the limit, and just declaring that a TLE offhand. 2 | -------------------------------------------------------------------------------- /rosalind/bioinf_stronghold/fib/solution.py: -------------------------------------------------------------------------------- 1 | def getNumRabbits(desMonth, mult): 2 | a = 1 3 | b = 1 4 | c = 4 5 | for x in range(3, desMonth): 6 | a, b = b, c 7 | c = a*mult+b 8 | return c 9 | 10 | 11 | print getNumRabbits(35, 3) -------------------------------------------------------------------------------- /enigmagroup/basics/js/2/solution.md: -------------------------------------------------------------------------------- 1 | # JS 2 2 | 3 | A prompt box appears asking for a password when you load the page. If you look at the page source, you'll see the entered string is compared against `skriptkid`. Just enter that into the prompt. 4 | -------------------------------------------------------------------------------- /kattis/differentdistances/solution.py: -------------------------------------------------------------------------------- 1 | while True: 2 | s = input().split() 3 | if len(s) == 1: 4 | break 5 | x1, y1, x2, y2, p = map(float, s) 6 | a = (abs(x1-x2)**p + abs(y1-y2)**p)**(1/p) 7 | print('{:.8f}'.format(a)) 8 | -------------------------------------------------------------------------------- /kattis/reachableroads/notes.md: -------------------------------------------------------------------------------- 1 | A graph problem to find the disjoint sets / unconnected components. Union find (here implemented with path compression) works, however the python recursion limit must be increased. 2 | 3 | Runs in 0.16s in python3. 4 | -------------------------------------------------------------------------------- /kattis/thinkingofanumber/notes.md: -------------------------------------------------------------------------------- 1 | Takes in a bit of number theory, as iterating every number within the range given is too slow in python. Instead, find the LCM, and iterate by the LCM. 2 | 3 | An annoying implementation problem mostly, though. 4 | -------------------------------------------------------------------------------- /uva/10055/solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long int a, b; 5 | while (scanf("%ld %ld", &a, &b) != EOF) { 6 | if (a > b) printf("%ld\n", a-b); 7 | else printf("%ld\n", b-a); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /advent_of_code/2017/05/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | g = list(map(int, sys.stdin.readlines())) 4 | i = 0 5 | count = 0 6 | 7 | while i >= 0 and i < len(g): 8 | j = i + g[i] 9 | g[i] += 1 10 | i = j 11 | count += 1 12 | 13 | print(count) 14 | -------------------------------------------------------------------------------- /advent_of_code/2017/17/part1.py: -------------------------------------------------------------------------------- 1 | buf = [0] 2 | prev = 0 3 | step = int(input()) 4 | for num in range(1, 2018): 5 | prev += step 6 | prev %= len(buf) 7 | prev += 1 8 | buf = buf[:prev] + [num] + buf[prev:] 9 | print(buf[(prev+1)%len(buf)]) 10 | -------------------------------------------------------------------------------- /advent_of_code/2021/01/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | ans = 0 4 | prev = int(sys.stdin.readline()) 5 | for line in sys.stdin.readlines(): 6 | curr = int(line) 7 | if curr > prev: 8 | ans += 1 9 | prev = curr 10 | print(ans) 11 | -------------------------------------------------------------------------------- /hacker.org/158/solution.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | t = [] 3 | for i in range(len(s)//2): 4 | n = int(s[i*2:i*2+2], 16) 5 | t.append(n) 6 | 7 | for k in range(0, 257): 8 | o = ''.join(map(lambda x:chr(x^k), t)) 9 | if 'submit' in o: 10 | print(o) 11 | -------------------------------------------------------------------------------- /kattis/bishops/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long int n; 5 | while (scanf("%ld", &n) != EOF) { 6 | if (n == 0) printf("0\n"); 7 | else if (n == 1) printf("1\n"); 8 | else printf("%ld\n", n+n-2); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /kattis/dasblinkenlights/notes.md: -------------------------------------------------------------------------------- 1 | A number theory / ad hoc question. Figure out if N*p == M*q for any N and M such that (N*p, M*q < s). The limits are small enough to put all such N*p and M*q in sets, and find the intersection. Runs in 0.02s in python3. 2 | -------------------------------------------------------------------------------- /kattis/enlarginghashtables/notes.md: -------------------------------------------------------------------------------- 1 | A prime numbers/number theory problem. Do a sieve of Eratosthenes up to some N, and for numbers larger than this N, do a pretty basic primality check with a speedup to cover multiples of 2 and 3. Runs in 0.03s in C++. 2 | -------------------------------------------------------------------------------- /project_euler/001/Solution.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | def main(args : Array[String]) { 3 | var sum = 0 4 | for (i <- 1 to 999) { 5 | if((i % 3 == 0) || (i % 5 == 0)){ 6 | sum += i 7 | } 8 | } 9 | println(sum) 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spoj/EIGHTS/solution.py: -------------------------------------------------------------------------------- 1 | def quick_find(k): 2 | d = [192, 442, 692, 942] 3 | t = int((k-1)/4) 4 | j =(k-1) % 4 5 | return int(t*1000 + d[j]) 6 | 7 | n = int(input()) 8 | for i in range(n): 9 | k = int(input()) 10 | print(quick_find(k)) 11 | -------------------------------------------------------------------------------- /enigmagroup/basics/pre/2/solution.md: -------------------------------------------------------------------------------- 1 | # Basics 2 2 | 3 | If you load the URL `http://challenges.enigmagroup.org/basics/pre/2/dontlookinhere/`, you'll see a directory listing showing the file `password.inc`, which contains the username (`admin`) and password. 4 | -------------------------------------------------------------------------------- /kattis/basketballoneonone/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | s = input() 3 | score = {'A': 0, 'B': 0} 4 | for i in range(0, len(s), 2): 5 | score[s[i]] += int(s[i+1]) 6 | print(max(score, key=lambda x:score[x])) 7 | 8 | 9 | main() 10 | -------------------------------------------------------------------------------- /kattis/ceremony/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | m = list(map(int, input().split())) 4 | m.sort() 5 | b = n 6 | for i, a in enumerate(m): 7 | b = min(b, a + n - i - 1) 8 | print(b) 9 | 10 | main() 11 | -------------------------------------------------------------------------------- /kattis/egypt/solution.py: -------------------------------------------------------------------------------- 1 | while True: 2 | a = sorted(list(map(int, input().split()))) 3 | if a[0] == 0 and a[2] == 0: 4 | break 5 | if a[0]*a[0] + a[1]*a[1] == a[2]*a[2]: 6 | print('right') 7 | else: 8 | print('wrong') 9 | -------------------------------------------------------------------------------- /kattis/ivana/notes.md: -------------------------------------------------------------------------------- 1 | If Ivana takes one integer, what is left is an integer chain from A to B. The next player will always need to take an integer from the end, leaving a chain of either A+1 to B, or A to B-1. We can do dynamic programming based on this. 2 | -------------------------------------------------------------------------------- /kattis/redrover/solution.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | b = len(s) 3 | for i in range(len(s)-1): 4 | for j in range(i+1, len(s)): 5 | m = s[i:j] 6 | t = s.replace(m, 'm') 7 | v = len(t) + len(m) 8 | b = min(b, v) 9 | print(b) 10 | -------------------------------------------------------------------------------- /dailyprogrammer/2015/11/02/easy.py: -------------------------------------------------------------------------------- 1 | def threes(n): 2 | if n == 1: 3 | print n 4 | else: 5 | m = n % 3 6 | x = 0 7 | if m == 1: 8 | x = -1 9 | elif m == 2: 10 | x = +1 11 | 12 | print n, x 13 | threes((n + x)/3) 14 | 15 | threes(31337357) -------------------------------------------------------------------------------- /enigmagroup/basics/js/4/solution.md: -------------------------------------------------------------------------------- 1 | # JS 4 2 | 3 | If you enter a wrong password into the prompt, it redirects to `lndex.php`, which is a different page to `index.php`, the original page with the prompt. `index.php` shows the password in the source: `shifted`. 4 | -------------------------------------------------------------------------------- /kattis/anthonyanddiablo/solution.py: -------------------------------------------------------------------------------- 1 | from math import pi 2 | 3 | a, n = map(float, input().split()) 4 | 5 | rad = n / (2*pi) 6 | area = pi * (rad**2) 7 | 8 | if area >= a: 9 | print('Diablo is happy!') 10 | else: 11 | print('Need more materials!') 12 | -------------------------------------------------------------------------------- /kattis/catalan/solution.py: -------------------------------------------------------------------------------- 1 | from math import factorial 2 | 3 | def nCr(n, r): 4 | return factorial(n) // (factorial(r) * factorial(n-r)) 5 | 6 | n = int(input()) 7 | for _ in range(n): 8 | x = int(input()) 9 | print(nCr(x*2, x) // (x + 1)) 10 | -------------------------------------------------------------------------------- /kattis/flexible/solution.py: -------------------------------------------------------------------------------- 1 | w, n = map(int, input().split()) 2 | m = list(map(int, input().split())) + [w] 3 | s = set(m) 4 | for i in range(n): 5 | for j in range(i+1, n+1): 6 | s.add(m[j]-m[i]) 7 | 8 | print(' '.join([str(c) for c in sorted(s)])) 9 | -------------------------------------------------------------------------------- /kattis/limbo1/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | for _ in range(n): 4 | a, b = map(int, input().split()) 5 | x = ((a+b) * (a+b+1))//2 6 | print(x + 1 + b) 7 | 8 | if __name__ == '__main__': 9 | main() 10 | -------------------------------------------------------------------------------- /kattis/npuzzle/notes.md: -------------------------------------------------------------------------------- 1 | Build a dictionary mapping each letter to its desired (x, y) tuple. Just look up each letter in this dictionary as you read it in from the input, and add the manhattan distances together. 2 | 3 | Don't forget to ignore '.', as I forgot to. 4 | -------------------------------------------------------------------------------- /kattis/polygonarea/notes.md: -------------------------------------------------------------------------------- 1 | A geometry problem. Calculate the area using the Shoelace formula. However, if you don't use an `abs()`, the sign of the result will tell you if the points were in clockwise or anticlockwise order. 2 | 3 | Runs in 0.03s in Python 3. 4 | -------------------------------------------------------------------------------- /kattis/shortestpath2/notes.md: -------------------------------------------------------------------------------- 1 | Similar to shortestpath1. A BFS or DFS will create a table of the shortest time to each node. At a node, if you cannot travel immediately, just simulate moving to the next node after having waited the required time. 2 | Runs in 0.65s. 3 | -------------------------------------------------------------------------------- /kattis/sibice/solution.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | n, w, h = [int(q) for q in input().split()] 4 | hyp = math.sqrt(w*w + h*h) + 0.01 5 | 6 | for _ in range(n): 7 | if int(input()) < hyp: 8 | print('DA') 9 | else: 10 | print('NE') 11 | -------------------------------------------------------------------------------- /kattis/tolower/solution.py: -------------------------------------------------------------------------------- 1 | probs, tests = map(int, input().split()) 2 | tot = 0 3 | for _ in range(probs): 4 | s = [input().strip() for _ in range(tests)] 5 | if all(t[0].lower() + t[1:] == t.lower() for t in s): 6 | tot += 1 7 | 8 | print(tot) 9 | -------------------------------------------------------------------------------- /advent_of_code/2017/04/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def valid(s): 4 | t = s.strip().split() 5 | return len(set(t)) == len(t) 6 | 7 | ans = 0 8 | for line in sys.stdin.readlines(): 9 | if valid(line): 10 | ans += 1 11 | 12 | print(ans) 13 | -------------------------------------------------------------------------------- /kattis/chineseremainder/notes.md: -------------------------------------------------------------------------------- 1 | Same solution as generalchineseremainder. 2 | 3 | I struggled through the end of [this PDF](http://math.mit.edu/~shor/18.310/algebra2.pdf) and [this page](https://brilliant.org/wiki/chinese-remainder-theorem/) and idk, this came out. 4 | -------------------------------------------------------------------------------- /kattis/erase/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = input() 3 | b = input() 4 | trans = str.maketrans('01', '10') 5 | if n % 2 == 1: 6 | a = a.translate(trans) 7 | if a == b: 8 | print('Deletion succeeded') 9 | else: 10 | print('Deletion failed') 11 | -------------------------------------------------------------------------------- /kattis/icpcawards/solution.py: -------------------------------------------------------------------------------- 1 | used = set() 2 | 3 | i = 0 4 | n = int(input()) 5 | for _ in range(n): 6 | uni, team = input().split() 7 | if i >= 12 or uni in used: 8 | continue 9 | i += 1 10 | used.add(uni) 11 | print(uni, team) 12 | -------------------------------------------------------------------------------- /kattis/lineup/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | names = [input() for _ in range(n)] 3 | 4 | if sorted(names) == names: 5 | print('INCREASING') 6 | elif sorted(names, reverse=True) == names: 7 | print('DECREASING') 8 | else: 9 | print('NEITHER') 10 | -------------------------------------------------------------------------------- /kattis/nastyhacks/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for _ in range(n): 3 | a, b, c = map(int, input().split()) 4 | p = b - c 5 | if a == p: 6 | print('does not matter') 7 | elif a > p: 8 | print('do not advertise') 9 | else: 10 | print('advertise') 11 | -------------------------------------------------------------------------------- /kattis/pripreme/notes.md: -------------------------------------------------------------------------------- 1 | If it takes longer to teach the slowest team than to teach every other team summed together, then the answer is clearly twice that team's length. 2 | 3 | Otherwise, the answer is the sum of all team times, and no teacher will ever be idle. 4 | -------------------------------------------------------------------------------- /kattis/sumoftheothers/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | for line in sys.stdin.readlines(): 4 | nums = [int(x) for x in line.split()] 5 | tot = sum(nums) 6 | for n in nums: 7 | if n == tot - n: 8 | print (n) 9 | break 10 | -------------------------------------------------------------------------------- /kattis/carrots/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, p; 5 | scanf("%d %d", &n, &p); 6 | int i; 7 | char c[1000]; 8 | for (i = 0; i < n; ++i) { 9 | scanf("%s", c); 10 | } 11 | printf("%d\n", p); 12 | } 13 | -------------------------------------------------------------------------------- /kattis/mapcolouring/notes.md: -------------------------------------------------------------------------------- 1 | A graph colouring problem, simply test if the graph can be coloured with [1, 2, 3, 4] colours, and if not, print "many". Made very easy by an absurdly high running time limit of 9 seconds. This backtracking solution runs in 0.04s in python3. 2 | -------------------------------------------------------------------------------- /kattis/palindromicpassword/notes.md: -------------------------------------------------------------------------------- 1 | An ad-hoc numbers problem. Simply iterate over `i` and test `m-i` and then `m+i` for each `m` given to see if that is a palindrome, and output the first one that is. `100000` is a special case. Runs in 0.00s in C++ according to Kattis. 2 | -------------------------------------------------------------------------------- /kattis/password/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | probabilities = [float(input().split()[1]) for _ in range(n)] 3 | probabilities = sorted(probabilities, reverse=True) 4 | ans = 0 5 | for i in range(1, len(probabilities)+1): 6 | ans += i*probabilities[i-1] 7 | print(ans) 8 | -------------------------------------------------------------------------------- /kattis/simplicity/solution.py: -------------------------------------------------------------------------------- 1 | s = input().strip() 2 | 3 | d = {} 4 | for c in s: 5 | d[c] = s.count(c) 6 | 7 | e = sorted(d, key=lambda x:d[x]) 8 | tot = 0 9 | i = 0 10 | while len(e[i:]) > 2: 11 | tot += d[e[i]] 12 | i += 1 13 | 14 | print(tot) 15 | -------------------------------------------------------------------------------- /kattis/sok/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | x = list(map(int, input().split())) 3 | y = list(map(int, input().split())) 4 | minq = min(x[i]/y[i] for i in range(3)) 5 | print(*(x[i] - minq*y[i] for i in range(3))) 6 | 7 | if __name__ == '__main__': 8 | main() 9 | -------------------------------------------------------------------------------- /kattis/twostones/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long int n; 5 | scanf("%lld", &n); 6 | if (n % 2 == 0) { 7 | printf("Bob\n"); 8 | } else { 9 | printf("Alice\n"); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /kattis/froshweek2/notes.md: -------------------------------------------------------------------------------- 1 | An ad-hoc greedy scheduling problem. Sort the task lengths and interval lengths when you read them. Then take the largest task and the largest interval, see if it fits, if not move onto the next smaller task, and so on greedily. Runs in 0.04s in C++. 2 | -------------------------------------------------------------------------------- /kattis/sretan/solution.py: -------------------------------------------------------------------------------- 1 | # We must add 1 so that we get 0s handled properly. 2 | n = int(input()) + 1 3 | # Take the binary repr and ignore the most significant bit 4 | b = bin(n)[3:] 5 | # Convert all zeroes to 4s and ones to 7s 6 | print(''.join('47'[int(c)] for c in b)) 7 | -------------------------------------------------------------------------------- /advent_of_code/2019/01/part2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | ans = 0 4 | for line in sys.stdin.readlines(): 5 | mass = int(line) 6 | fuel_req = mass//3 - 2 7 | while fuel_req > 0: 8 | ans += fuel_req 9 | fuel_req = fuel_req//3 - 2 10 | 11 | print(ans) 12 | -------------------------------------------------------------------------------- /enigmagroup/steganography/1/solution.md: -------------------------------------------------------------------------------- 1 | # Steganography 1 2 | 3 | If you open the given file in a text editor, near the bottom you'll see the string `egpw:fg3t4a0fvzcy3` ("egpw" = "enigma group password" I guess). If you un-ROT13 `fg3t4a0fvzcy3`, you get `st3g4n0simpl3`, the answer. 4 | -------------------------------------------------------------------------------- /kattis/bits/solution.py: -------------------------------------------------------------------------------- 1 | t = int(raw_input()) 2 | 3 | for tc in xrange(t): 4 | s = raw_input() 5 | n = '' 6 | m = 0 7 | for i in xrange(len(s)): 8 | n += s[i] 9 | bins = bin(int(n))[2:] 10 | ones = sum([int(x) for x in bins]) 11 | m = max(ones, m) 12 | print m 13 | -------------------------------------------------------------------------------- /kattis/guess/solution.py: -------------------------------------------------------------------------------- 1 | lo = 1 2 | hi = 1001 3 | 4 | while True: 5 | mid = (lo+hi)//2 6 | print(mid) 7 | s = input() 8 | if s == 'correct': 9 | break 10 | if s == 'lower': 11 | hi = mid 12 | elif s == 'higher': 13 | lo = mid 14 | -------------------------------------------------------------------------------- /kattis/justaminute/solution.py: -------------------------------------------------------------------------------- 1 | i, j = 0, 0 2 | n = int(input()) 3 | for _ in range(n): 4 | a, b = map(int, input().split()) 5 | a *= 60 6 | i += a 7 | j += b 8 | if j/i <= 1: 9 | print('measurement error') 10 | else: 11 | print('{:.8f}'.format(j/i)) 12 | -------------------------------------------------------------------------------- /kattis/missingnumbers/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | nums = set([int(input()) for _ in range(n)]) 3 | maxn = max(nums) 4 | if maxn == len(nums): 5 | print('good job') 6 | else: 7 | for i in range(1, maxn): 8 | if i not in nums: 9 | print(i) 10 | -------------------------------------------------------------------------------- /kattis/numberfun/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for _ in range(n): 3 | a, b, c = map(int, input().split()) 4 | if a + b == c or a - b == c or b - a == c or a * c == b or b * c == a or a * b == c: 5 | print('Possible') 6 | else: 7 | print('Impossible') 8 | -------------------------------------------------------------------------------- /4clojure/026.clj: -------------------------------------------------------------------------------- 1 | (fn fib [n] 2 | (cond 3 | (= n 1) '(1) 4 | (= n 2) '(1 1) 5 | :else (let [p (fib (- n 1))] 6 | (concat p 7 | (list (+ 8 | (last p) 9 | (nth p (- (count p) 2)))))))) 10 | -------------------------------------------------------------------------------- /advent_of_code/2020/10/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | nums = [0] + list(sorted(int(s) for s in sys.stdin.readlines())) 3 | diffbucket = [-100, 0, 0, 0] 4 | for i in range(1, len(nums)): 5 | diffbucket[nums[i] - nums[i-1]] += 1 6 | diffbucket[3] += 1 7 | print(diffbucket[1]*diffbucket[3]) 8 | -------------------------------------------------------------------------------- /kattis/coloring/notes.md: -------------------------------------------------------------------------------- 1 | A graph colouring problem, to find the minimal number of colours to fill the graph. The problem is NP-complete, but can be sped up by manually inserting the first 2 colours in the graph at a random node and another node connected to it. Runs in 0.47s in python3. 2 | -------------------------------------------------------------------------------- /kattis/countingtriangles/notes.md: -------------------------------------------------------------------------------- 1 | A geometry problem. Using formula @ http://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line, we can figure out if two line segments cross. Simply enumerate all lines that cross that create triangles. Runs in 0.25s in Py3. 2 | -------------------------------------------------------------------------------- /kattis/herman/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | long int r; 6 | scanf("%ld", &r); 7 | long double ca = M_PI*r*r; 8 | printf("%.6Lf\n", ca); 9 | long double sa = (r+r)*(r+r)/2; 10 | printf("%.6Lf\n", sa); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /kattis/ladder/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define PI 3.14159265 4 | 5 | int main() { 6 | int h, a; 7 | scanf("%d %d", &h, &a); 8 | double rad = a*(PI/180.0); 9 | printf("%ld\n", (long int) ceil(h/sin(rad))); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /project_euler/057/solution.py: -------------------------------------------------------------------------------- 1 | n = 3 2 | d = 2 3 | old_n = 3 4 | 5 | 6 | num = 0 7 | 8 | for i in xrange(1, 1000): 9 | n = old_n + 2*d 10 | 11 | d = d + old_n 12 | 13 | old_n = n 14 | if len(str(n)) > len(str(d)): 15 | num += 1 16 | 17 | print num 18 | -------------------------------------------------------------------------------- /kattis/aaah/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | char able[1001], req[1001]; 6 | scanf("%s", able); 7 | scanf("%s", req); 8 | if (strlen(able) >= strlen(req)) { 9 | printf("go\n"); 10 | } else { 11 | printf("no\n"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /kattis/calculator/notes.md: -------------------------------------------------------------------------------- 1 | Convert the given infix expression to postfix, and then evaluate the postfix. 2 | 3 | Some care has to be taken with unary minus operators, but you can put in a different symbol for these, and then negate the top of the stack when you are evaluating the postfix. 4 | -------------------------------------------------------------------------------- /kattis/mixedfractions/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | while (1) { 5 | long long int n, d; 6 | scanf("%lld %lld", &n, &d); 7 | if (n == 0 && d == 0) { 8 | break; 9 | } 10 | printf("%lld %lld / %lld\n", n/d, n%d, d); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /kattis/wizardofodds/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n, k = map(int, input().split()) 3 | if (k >= 400) or (2**k >= n): 4 | print('Your wish is granted!') 5 | else: 6 | print('You will become a flying monkey!') 7 | 8 | if __name__ == '__main__': 9 | main() 10 | -------------------------------------------------------------------------------- /project_euler/097/solution.py: -------------------------------------------------------------------------------- 1 | b = 2 2 | m = 10**10 3 | i = 1 4 | while i < 7830457: 5 | i += 1 6 | #b = (b * 2) % m 7 | b = (b << 1) % m 8 | 9 | print "exponent:",i 10 | print "result of power of 2:", b 11 | 12 | n = (28433 * b + 1) 13 | s = str(n) 14 | print "answer:",s[len(s) - 10:] -------------------------------------------------------------------------------- /kattis/avion/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | nums = [(i+1) for i,line in enumerate(sys.stdin.readlines()) if 'FBI' in line] 5 | if len(nums) == 0: 6 | print('HE GOT AWAY!') 7 | else: 8 | print(' '.join(str(n) for n in nums)) 9 | 10 | main() 11 | -------------------------------------------------------------------------------- /kattis/breakingbad/notes.md: -------------------------------------------------------------------------------- 1 | The problem asks to figure out if the given graph is bipartite. A graph colouring solution works, marking nodes the opposite colour to all that they are connected with, and then seeing if you try to colour a node two different colours. If so, the graph is not bipartite. 2 | -------------------------------------------------------------------------------- /kattis/irepeatmyself/notes.md: -------------------------------------------------------------------------------- 1 | A string comparison and prefix problem. Assume a string of length 1 can be repeated to create the end string, then test this theory. If it is valid, output that as an answer, otherwise increment the number and try again. Repeat. 2 | 3 | Runs in 0.00s in C, supposedly. 4 | -------------------------------------------------------------------------------- /kattis/register/solution.py: -------------------------------------------------------------------------------- 1 | reg_sizes = [2, 3, 5, 7, 11, 13, 17, 19, 5] 2 | vals = [1, 2] 3 | for size in reg_sizes[1:]: 4 | vals.append(vals[-1]*size) 5 | max_val = 9699689 6 | n = list(map(int, input().split())) + [0] 7 | m = sum(n[i]*vals[i] for i in range(len(n))) 8 | print(max_val - m) 9 | -------------------------------------------------------------------------------- /kattis/roberthood/notes.md: -------------------------------------------------------------------------------- 1 | A geometry problem. The points with the largest distances will inevitably be on the convex hull. Thus, run graham scan to get the convex hull, then just use `O(n^2)` iteration to find the largest distance between any two of these points. 2 | 3 | Runs in 0.51s in Python3. 4 | -------------------------------------------------------------------------------- /kattis/rot/notes.md: -------------------------------------------------------------------------------- 1 | An implementation and string formatting problem. Annoying. 2 | 3 | Worth remembering the python shorthand for rotating a list 90 degrees: 4 | 5 | ```python 6 | def rotate(g): 7 | return list(zip(*g[::-1])) 8 | ``` 9 | 10 | This solution runs in 0.02s in python3. 11 | -------------------------------------------------------------------------------- /kattis/savingforretirement/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () { 4 | int a, b, c, d, e; 5 | scanf("%d %d %d %d %d", &a, &b, &c, &d, &e); 6 | int des = c*(b-a); 7 | int req = des/e; 8 | if (req * e <= des) ++req; 9 | printf("%d\n", d + req); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /kattis/stickysituation/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = sorted(map(int, input().split())) 3 | ok = False 4 | for i in range(n-2): 5 | if s[i] + s[i+1] > s[i+2]: 6 | ok = True 7 | break 8 | if ok: 9 | print('possible') 10 | else: 11 | print('impossible') 12 | -------------------------------------------------------------------------------- /leetcode/length-of-last-word/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLastWord(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | t = s.split() 8 | if not len(t): 9 | return 0 10 | return len(t[-1]) 11 | -------------------------------------------------------------------------------- /advent_of_code/2017/05/part2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | g = list(map(int, sys.stdin.readlines())) 4 | i = 0 5 | count = 0 6 | 7 | while i >= 0 and i < len(g): 8 | j = i + g[i] 9 | if g[i] >= 3: 10 | g[i] -= 1 11 | else: 12 | g[i] += 1 13 | i = j 14 | count += 1 15 | 16 | print(count) 17 | -------------------------------------------------------------------------------- /advent_of_code/2018/01/part2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | m = list(map(int, sys.stdin)) 3 | f = 0 4 | s = set() 5 | ok = False 6 | while not ok: 7 | for n in m: 8 | f += n 9 | if f in s: 10 | print(f) 11 | ok = True 12 | break 13 | s.add(f) 14 | -------------------------------------------------------------------------------- /dailyprogrammer/2015/11/02/easy.hs: -------------------------------------------------------------------------------- 1 | threes :: Int -> String 2 | threes 1 = "1\n" 3 | threes n = (show n) ++ (if n `mod` 3 == 0 then (" 0\n" ++ threes(n `div` 3)) else (if n `mod` 3 == 1 then (" -1\n" ++ threes((n-1) `div` 3)) else (" +1\n" ++ (threes((n+1) `div` 3))))) 4 | 5 | main = putStr $ threes 31337357 -------------------------------------------------------------------------------- /kattis/akcija/solution.py: -------------------------------------------------------------------------------- 1 | n = int(raw_input()) 2 | 3 | c = sorted([int(raw_input()) for i in xrange(n)], reverse=True) 4 | tot = 0 5 | 6 | i = 0 7 | while i + 2 < len(c): 8 | tot += c[i] + c[i+1] 9 | i += 3 10 | 11 | while i < len(c): 12 | tot += c[i] 13 | i += 1 14 | 15 | print tot 16 | -------------------------------------------------------------------------------- /kattis/hardware/notes.md: -------------------------------------------------------------------------------- 1 | `req` function modifies an array of digit counts with the new digits required for a given number. 2 | 3 | For ranges of digits, take them one by one and run them through this function. For single digits, just put them through this function and move on. 4 | 5 | Runs in 0.02s. 6 | -------------------------------------------------------------------------------- /kattis/janitortroubles/solution.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | # Bretschneider's formula, lifted from https://keisan.casio.com/exec/system/1322718508 4 | d = list(map(int, input().split())) 5 | s = sum(d)/2 6 | print(math.sqrt((s-d[0])*(s-d[1])*(s-d[2])*(s-d[3]) - d[0]*d[1]*d[2]*d[3]*(math.cos(math.pi/2)**2))) 7 | -------------------------------------------------------------------------------- /kattis/lostlineup/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | v = list(map(int, input().split())) 4 | k = [(m, i) for i, m in enumerate(v)] 5 | k.sort() 6 | print('1', end=' ') 7 | for d in k: 8 | print(d[1] + 2, end=' ') 9 | print() 10 | 11 | main() 12 | -------------------------------------------------------------------------------- /kattis/snappereasy/solution.py: -------------------------------------------------------------------------------- 1 | num_tests = int(input()) 2 | for test in range(1, num_tests+1): 3 | length, num = map(int, input().split()) 4 | if num % (2 ** length) == (2 ** length)-1: 5 | print('Case #{}: ON'.format(test)) 6 | else: 7 | print('Case #{}: OFF'.format(test)) 8 | -------------------------------------------------------------------------------- /kattis/snapperhard/solution.py: -------------------------------------------------------------------------------- 1 | num_tests = int(input()) 2 | for test in range(1, num_tests+1): 3 | length, num = map(int, input().split()) 4 | if num % (2 ** length) == (2 ** length)-1: 5 | print('Case #{}: ON'.format(test)) 6 | else: 7 | print('Case #{}: OFF'.format(test)) 8 | -------------------------------------------------------------------------------- /kattis/splat/notes.md: -------------------------------------------------------------------------------- 1 | Given a query (X,Y), iterate through the paint splats in reverse order and find the nearest one where `dist(X, Y, splatX, splatY) < splatRadius`. This lookup can also be avoided if `abs(X-splatX) > splatRadius || abs(Y-splatY) > splatRadius`. If no splat is found, print `white`. 2 | -------------------------------------------------------------------------------- /kattis/wheels/notes.md: -------------------------------------------------------------------------------- 1 | A graph problem. Two nodes are connected if the distance between them is equal to the sum of their radii. 2 | 3 | Find the distance from element #0 to all other elements (I used BFS). A cog rotates clockwise if the distance is even. 4 | 5 | The speed of cog `i` is `R[0]/R[i]`. 6 | -------------------------------------------------------------------------------- /advent_of_code/2017/04/part2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def valid(s): 4 | t = s.strip().split() 5 | u = set(str(sorted(v)) for v in t) 6 | return len(u) == len(t) 7 | 8 | ans = 0 9 | for line in sys.stdin.readlines(): 10 | if valid(line): 11 | ans += 1 12 | 13 | print(ans) 14 | -------------------------------------------------------------------------------- /kattis/babelfish/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | d = {} 3 | 4 | s = raw_input() 5 | while s != '': 6 | t = s.split() 7 | d[t[1]] = t[0] 8 | s = raw_input() 9 | 10 | lines = sys.stdin.readlines() 11 | for line in lines: 12 | if line[:-1] in d: 13 | print d[line[:-1]] 14 | else: 15 | print 'eh' 16 | -------------------------------------------------------------------------------- /kattis/beautifulprimes/notes.md: -------------------------------------------------------------------------------- 1 | Number theory problem. 2 | 3 | Only consider the primes 2 and 11. A number should gain at least 1 digit if you multiply it by 11. Given an original list of N 2s, keep removing a 2 and adding an 11 until the product is the desired length. 4 | 5 | Runs in 0.04s in python3. 6 | -------------------------------------------------------------------------------- /kattis/bijele/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int d[] = {1, 1, 2, 2, 2, 8}; 5 | int h[6]; 6 | scanf("%d %d %d %d %d %d", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5]); 7 | int i; 8 | for (i = 0; i < 6; ++i) { 9 | printf("%d ", d[i] - h[i]); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /kattis/keytocrypto/solution.py: -------------------------------------------------------------------------------- 1 | A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 2 | 3 | cypher = input() 4 | key = input() 5 | ans = '' 6 | for i in range(len(cypher)): 7 | c = A.find(cypher[i]) 8 | k = A.find(key[i]) 9 | j = c - k % len(A) 10 | ans += A[j] 11 | key += A[j] 12 | print(ans) 13 | -------------------------------------------------------------------------------- /kattis/loorolls/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | l, n = map(int, input().split()) 3 | ans = 1 4 | while True: 5 | k = l % n 6 | if k == 0: 7 | break 8 | n = n-k 9 | ans += 1 10 | print(ans) 11 | 12 | if __name__ == '__main__': 13 | main() 14 | -------------------------------------------------------------------------------- /kattis/pauleigon/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, p, q; 5 | scanf("%d %d %d", &n, &p, &q); 6 | int t = p + q; 7 | if (t/n % 2 == 0) { 8 | printf("paul\n"); 9 | } else { 10 | printf("opponent\n"); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /kattis/teacherevaluation/solution.py: -------------------------------------------------------------------------------- 1 | n, p = map(int, input().split()) 2 | tot = sum(map(int, input().split())) 3 | 4 | if p == 100: 5 | print('impossible') 6 | else: 7 | ans = 0 8 | while tot/n < p: 9 | tot += 100 10 | n += 1 11 | ans += 1 12 | print(ans) 13 | -------------------------------------------------------------------------------- /kattis/trollhunt/notes.md: -------------------------------------------------------------------------------- 1 | The problem does not ask for how many days until the troll is killed, only how many until it is found. 2 | 3 | Number of groups of knights = `gr` = `k // g`. 4 | 5 | Every day, subtract `gr` from `b`, until `b <= 1`. 6 | 7 | The number of days it takes to do this is the answer. 8 | -------------------------------------------------------------------------------- /spoj/SAMER08F/solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | while (1) { 7 | long long int n; 8 | scanf("%lld", &n); 9 | if (n == 0) break; 10 | printf("%lld\n", (n*(n+1)*(2*n + 1))/6); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /advent_of_code/2021/07/part1.py: -------------------------------------------------------------------------------- 1 | def calc_cost(nums, d): 2 | return sum(abs(n-d) for n in nums) 3 | 4 | def main(): 5 | nums = list(map(int, input().split(','))) 6 | lo = min(nums) 7 | hi = max(nums) 8 | 9 | print(min(calc_cost(nums, d) for d in range(lo, hi+1))) 10 | 11 | main() 12 | -------------------------------------------------------------------------------- /hacker.org/001/solution.py: -------------------------------------------------------------------------------- 1 | a = 'abcdefghijklmnopqrstuvwxyz' 2 | A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 3 | s = input() 4 | for c in s: 5 | if c in a: 6 | print(a[(a.find(c)+13)%len(a)], end='') 7 | elif c in A: 8 | print(A[(A.find(c)+13)%len(A)], end='') 9 | else: 10 | print(c, end='') 11 | print() 12 | -------------------------------------------------------------------------------- /kattis/cd/solution.py: -------------------------------------------------------------------------------- 1 | while True: 2 | n, m = map(int, raw_input().split()) 3 | if n == 0 and m == 0: 4 | break 5 | a = set(int(raw_input()) for _ in range(n)) 6 | ans = 0 7 | for _ in range(m): 8 | if int(raw_input()) in a: 9 | ans += 1 10 | print(ans) 11 | -------------------------------------------------------------------------------- /kattis/compass/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define abs(a) ((a)>0?(-a):(a)) 3 | 4 | int main() { 5 | int start; 6 | int desired; 7 | scanf("%d\n%d", &start, &desired); 8 | int ans = ((360-start) + desired); 9 | while (ans > 180) ans -= 360; 10 | printf("%d\n", ans); 11 | } 12 | -------------------------------------------------------------------------------- /kattis/frosting/notes.md: -------------------------------------------------------------------------------- 1 | An ad-hoc arrays implementation problem. The `O(n^2)` trivial solution is too slow. Instead, you must realise that you can do it in `O(2*n)`, if you sum together all of the grid heights at the beginning in 3 buckets, and cycle them while computing the totals. 2 | 3 | Runs in 0.02s in C. 4 | -------------------------------------------------------------------------------- /kattis/redistribution/solution.py: -------------------------------------------------------------------------------- 1 | n = int(raw_input()) 2 | 3 | size = [int(x) for x in raw_input().split()] 4 | 5 | b = sorted(range(0, n), key = lambda x: -size[x]) 6 | 7 | if size[b[0]] > sum([size[i] for i in b[1:]]): 8 | print 'impossible' 9 | else: 10 | print ' '.join([str(i+1) for i in b]) 11 | -------------------------------------------------------------------------------- /kattis/romans/solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | double d; 9 | cin >> d; 10 | 11 | double ans = (d*1000*5280.0)/4854.0; 12 | 13 | cout << (long long int) (ans+0.5) << "\n"; 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /advent_of_code/2021/01/part2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | depths = list(map(int, sys.stdin.readlines())) 4 | tot = sum(depths[:3]) 5 | ans = 0 6 | for i in range(3, len(depths)): 7 | new_tot = tot - depths[i-3] + depths[i] 8 | if new_tot > tot: 9 | ans += 1 10 | tot = new_tot 11 | print(ans) 12 | -------------------------------------------------------------------------------- /kattis/boatparts/solution.py: -------------------------------------------------------------------------------- 1 | p, n = map(int, input().split()) 2 | 3 | r = set() 4 | ans = - 1 5 | for i in range(1, n+1): 6 | s = input().strip() 7 | r.add(s) 8 | if len(r) == p and ans < 0: 9 | ans = i 10 | if ans < 0: 11 | print('paradox avoided') 12 | else: 13 | print(ans) 14 | -------------------------------------------------------------------------------- /kattis/cold/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n; 5 | scanf("%d", &n); 6 | int i; 7 | int ans = 0; 8 | for (i = 0; i < n; ++i) { 9 | long int m; 10 | scanf("%ld", &m); 11 | if (m < 0) ++ans; 12 | } 13 | printf("%d\n", ans); 14 | } 15 | -------------------------------------------------------------------------------- /kattis/communication/solution.py: -------------------------------------------------------------------------------- 1 | n = int(raw_input()) 2 | 3 | m = [int(x) for x in raw_input().split()] 4 | 5 | ans = {} 6 | 7 | for x in xrange(256): 8 | shifted = x << 1 9 | if shifted >= 256: 10 | shifted -= 256; 11 | y = x^int(shifted) 12 | ans[y] = x 13 | 14 | for y in m: 15 | print ans[y], 16 | -------------------------------------------------------------------------------- /kattis/listgame/notes.md: -------------------------------------------------------------------------------- 1 | Th problem is simply asking for the count of prime factors of the given number. 2 | 3 | The worst case input is 999999937. You need to iterate up from 2..n to find numbers that divide evenly. You can quit when your number >= sqrt(n), and it is necessary to do this to fit in the time limit. 4 | -------------------------------------------------------------------------------- /kattis/ones/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | for line in sys.stdin: 5 | p = int(line) 6 | d = 1%p 7 | ans = 1 8 | while d: 9 | d = (d*10+1)%p 10 | ans += 1 11 | print(ans) 12 | 13 | if __name__ == '__main__': 14 | main() 15 | -------------------------------------------------------------------------------- /kattis/stopwatch/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | on = False 3 | prev = 0 4 | ans = 0 5 | for _ in range(n): 6 | t = int(input()) 7 | 8 | if on: 9 | ans += t-prev 10 | 11 | on = not on 12 | prev = t 13 | 14 | if on: 15 | print('still running') 16 | else: 17 | print(ans) 18 | -------------------------------------------------------------------------------- /kattis/telephones/notes.md: -------------------------------------------------------------------------------- 1 | Seeing as there are fewer queries than entries, it is not worth storing the telephone call entries smartly. For each query, just run through all calls and see if any fall in the right bounds. 2 | 3 | Getting less-than vs. less-than-or-equal-to in duration checks was just trial and error >:( 4 | -------------------------------------------------------------------------------- /project_euler/002/Solution.scala: -------------------------------------------------------------------------------- 1 | object Scala{ 2 | def main(args : Array[String]){ 3 | var a = 1 4 | var b = 1 5 | var sum = 0 6 | while (b < 4000000){ 7 | var c = a + b 8 | if (c % 2 == 0) { 9 | sum += c 10 | } 11 | a = b 12 | b = c 13 | } 14 | println(sum) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /project_euler/006/solution.hs: -------------------------------------------------------------------------------- 1 | square :: Integer -> Integer 2 | square n = n*n 3 | 4 | normalSum :: Integer -> Integer 5 | normalSum n = foldl (+) 0 [0..n] 6 | 7 | squareSum :: Integer -> Integer 8 | squareSum n = foldl (+) 0 (map square [0..n]) 9 | 10 | main = print ((square $ normalSum 100) - (squareSum 100)) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # solutions 2 | Solutions to online programming problems 3 | 4 | Please do not use my solutions as your own. I use this repository to organise my answers, to track my progress, and to link to friends to compare solutions; it is not here to help anyone cheat. 5 | 6 | I just really enjoy solving these problems. 7 | -------------------------------------------------------------------------------- /enigmagroup/steganography/2/solution.md: -------------------------------------------------------------------------------- 1 | # Steganography 2 2 | 3 | I opened the file in a hex editor, and at the end noticed some bytes indicating it was a LAME encoded file. I renamed it to `download.mp3`, and it opened fine in iTunes. The words `screaming metal death robot` could just about be heard. This is the answer. 4 | -------------------------------------------------------------------------------- /kattis/awkwardparty/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | s = map(int, input().split()) 4 | d = {} 5 | minn = n 6 | for i, lang in enumerate(s): 7 | if lang in d: 8 | minn = min(minn, i - d[lang]) 9 | d[lang] = i 10 | print(minn) 11 | 12 | main() 13 | -------------------------------------------------------------------------------- /kattis/convexhull/notes.md: -------------------------------------------------------------------------------- 1 | A convex hull problem. Just implement the Graham scan algorithm. I also sped up input by not only using `sys.stdin.readline()` instead of `input()`, but also aliasing it as `read`, so that a search of the global scope doesn't need to be performed whenever it is called. Runs in 0.55s in python3. 2 | -------------------------------------------------------------------------------- /kattis/dicecup/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | if (b < a) { 7 | int c = a; 8 | a = b; 9 | b = c; 10 | } 11 | int i; 12 | for (i = a+1; i < b+2; ++i) { 13 | printf("%d\n", i); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /kattis/qaly/solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n; 5 | double a, b; 6 | scanf("%d", &n); 7 | double r = 0; 8 | for (int i = 0; i < n; ++i) { 9 | scanf("%lf %lf", &a, &b); 10 | r += a*b; 11 | } 12 | printf("%lf\n", r); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /kattis/soylent/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int n; 6 | scanf("%d", &n); 7 | int i; 8 | for (i = 0; i < n; ++i) { 9 | int m; 10 | scanf("%d", &m); 11 | printf("%d\n", (int) ceil(m/400.0)); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /kattis/spavanac/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int h, m; 5 | scanf("%d %d", &h, &m); 6 | m -= 45; 7 | if (m < 0) { 8 | m += 60; 9 | --h; 10 | if (h < 0) { 11 | h += 24; 12 | } 13 | } 14 | printf("%d %d\n", h, m); 15 | } 16 | -------------------------------------------------------------------------------- /kattis/znanstvenik/notes.md: -------------------------------------------------------------------------------- 1 | 1. Create a list of the columns, with each one reversed 2 | 2. Sort this list. 3 | 3. You can now see if it is possible to delete up to a certain point efficiently. 4 | 4. Binary search the point up to which you can delete. 5 | 5. Profit. 6 | 7 | Runs in 0.16s in python3 (or 0.34s in python2?). 8 | -------------------------------------------------------------------------------- /leetcode/valid-palindrome/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, s): 3 | """ 4 | :type s: str 5 | :rtype: bool 6 | """ 7 | a = set('abcdefghijklmnopqrstuvwxyz0123456789') 8 | t = [c for c in s.lower() if c in a] 9 | return t == t[::-1] 10 | -------------------------------------------------------------------------------- /project_euler/003/solution.hs: -------------------------------------------------------------------------------- 1 | factors :: Int -> [Int] 2 | --factors n :: Integer -> Integer[] 3 | factors n = (filter 4 | (\x -> n `mod` x == 0) 5 | [2,3..((floor $ sqrt $ fromIntegral n)+1)]) 6 | 7 | isPrime n = (length $ factors n) == 0 8 | 9 | main = print $ last $ filter isPrime (factors 600851475143) 10 | -------------------------------------------------------------------------------- /advent_of_code/2022/01/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | elves = [] 4 | curr = [] 5 | for line in sys.stdin.readlines(): 6 | if line.strip() == '': 7 | elves.append(curr) 8 | curr = [] 9 | else: 10 | curr.append(int(line)) 11 | elves.append(curr) 12 | 13 | print(max(sum(c) for c in elves)) 14 | -------------------------------------------------------------------------------- /enigmagroup/basics/pre/3/solution.md: -------------------------------------------------------------------------------- 1 | # Basics 3 2 | 3 | Tip is "robots". The challenge URL has a `/robots.txt` with the following content: 4 | 5 | ``` 6 | User-agent: * 7 | Disallow: /f0rk/ 8 | ``` 9 | 10 | Dir `f0rk` has directory listing showing a file `password.inc`, containing the username and password. 11 | -------------------------------------------------------------------------------- /hacker.org/127/solution.md: -------------------------------------------------------------------------------- 1 | I put the problem input and a copy of the americanese gettysburg address 2 | (pilfered from here: http://www.abrahamlincolnonline.org/lincoln/speeches/gettysburg.htm) 3 | into a diff tool (specifically this one: https://www.diffchecker.com/diff), 4 | and the first word in the output was the solution. 5 | -------------------------------------------------------------------------------- /kattis/haypoints/solution.py: -------------------------------------------------------------------------------- 1 | m, n = map(int, input().split()) 2 | d = {} 3 | for _ in range(m): 4 | s, v = input().split() 5 | d[s] = int(v) 6 | for _ in range(n): 7 | s = None 8 | ans = 0 9 | while s != '.': 10 | s = input() 11 | for t in s.split(): 12 | if t in d: 13 | ans += d[t] 14 | print(ans) 15 | -------------------------------------------------------------------------------- /kattis/incognito/notes.md: -------------------------------------------------------------------------------- 1 | A combinatorics problem. Each category will start with 1 item (ie. wearing one from that category), and then get +1 for each new item introduced into that category. Your answer will be the product of the numbers in all categories, less one (ie. wearing no disguise at all). Runs in 0.02s in python3. 2 | -------------------------------------------------------------------------------- /kattis/piperotation/notes.md: -------------------------------------------------------------------------------- 1 | A graph problem type thing. Keep a matrix noting whether each square requires an open pipe on the right and on the bottom. Check each type of pipe individually against the needs of the pipe above and to the left, and as soon as there's any inconsistency, return Impossible. Runs in 0.03s in python3. 2 | -------------------------------------------------------------------------------- /kattis/putovanje/solution.py: -------------------------------------------------------------------------------- 1 | n, c = map(int, input().split()) 2 | f = list(map(int, input().split())) 3 | b = 0 4 | for i in range(len(f)): 5 | t = 0 6 | m = 0 7 | for j in range(i, len(f)): 8 | if t + f[j] <= c: 9 | t += f[j] 10 | m += 1 11 | b = max(b, m) 12 | print(b) 13 | -------------------------------------------------------------------------------- /kattis/whatdoesthefoxsay/solution.py: -------------------------------------------------------------------------------- 1 | t = int(raw_input()) 2 | for u in xrange(t): 3 | sounds = raw_input().split() 4 | figured = set() 5 | while True: 6 | parts = raw_input().split() 7 | if len(parts) > 3: 8 | break 9 | figured.add(parts[2]) 10 | print ' '.join([s for s in sounds if s not in figured]) 11 | -------------------------------------------------------------------------------- /advent_of_code/2020/01/part2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | nums = [int(n) for n in sys.stdin.readlines()] 3 | numset = set(nums) 4 | 5 | for i in range(len(nums)): 6 | a = nums[i] 7 | for j in range(i+1, len(nums)): 8 | b = nums[j] 9 | c = 2020 - a - b 10 | if c in numset: 11 | print(a*b*c) 12 | -------------------------------------------------------------------------------- /advent_of_code/2020/03/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | x = 0 4 | y = 0 5 | ans = 0 6 | 7 | grid = [s.strip() for s in sys.stdin.readlines()] 8 | height = len(grid) 9 | width = len(grid[0]) 10 | while y < height: 11 | if grid[y][x % width] == '#': 12 | ans += 1 13 | 14 | x += 3 15 | y += 1 16 | print(ans) 17 | -------------------------------------------------------------------------------- /kattis/airlinehub/notes.md: -------------------------------------------------------------------------------- 1 | This problem requires calculating the distance between all pairs of points. However, the distance function must be a [great-circle distance](https://en.wikipedia.org/wiki/Great-circle_distance). I used the calculation of Delta-delta (for the central angle) from this link. My solution ran in 0.34s in C. 2 | -------------------------------------------------------------------------------- /kattis/estimatingtheareaofacircle/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | while (1) { 6 | double r; 7 | long int m, c; 8 | scanf("%lf %ld %ld", &r, &m, &c); 9 | if (r == 0) break; 10 | 11 | printf("%.7lf %.7lf\n", M_PI*r*r, 4*r*r*((c+0.0)/m)); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /kattis/limbo2/notes.md: -------------------------------------------------------------------------------- 1 | An annoying number theory ish problem. Drawing out the grid helps a lot. You just need to do some base-2 stuff to figure out at which level the given coordinates will be added in, and then figure out the exact value at those points. 2 | A lot of off-by-ones (and off-by-twos) involved. Runs in 0.02s in python3. 3 | -------------------------------------------------------------------------------- /kattis/smallestmultiple/solution.py: -------------------------------------------------------------------------------- 1 | import functools 2 | import sys 3 | 4 | def gcd(a, b): 5 | if b == 0: 6 | return a 7 | return gcd(b, a % b) 8 | 9 | def lcm(a, b): 10 | return a*b//gcd(a, b) 11 | 12 | for line in sys.stdin.readlines(): 13 | print(functools.reduce(lcm, map(int, line.split()))) 14 | -------------------------------------------------------------------------------- /kattis/symmetricorder/solution.py: -------------------------------------------------------------------------------- 1 | c = 1 2 | while True: 3 | n = int(input()) 4 | if n == 0: 5 | break 6 | w = [input() for _ in range(n)] 7 | print ("SET", c) 8 | for i in range(0, n, 2): 9 | print(w[i]) 10 | for i in range(n-(n%2)-1, -1, -2): 11 | print(w[i]) 12 | c += 1 13 | -------------------------------------------------------------------------------- /kattis/yinyangstones/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () { 4 | long long w = 0; 5 | long long b = 0; 6 | while (1) { 7 | int c = getchar(); 8 | if (c == '\n') { 9 | break; 10 | } 11 | if (c == 'W') ++w; 12 | if (c == 'B') ++b; 13 | } 14 | printf((w==b)?"1\n":"0\n"); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/remove-duplicates-from-sorted-array/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeDuplicates(self, nums: List[int]) -> int: 3 | start = 0 4 | while start < len(nums) - 1: 5 | if nums[start] == nums[start+1]: 6 | del nums[start] 7 | else: 8 | start += 1 9 | -------------------------------------------------------------------------------- /advent_of_code/2020/02/part2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | ans = 0 4 | for line in sys.stdin.readlines(): 5 | policy, pw = line.split(':') 6 | pos, letter = policy.split(' ') 7 | i, j= map(int, pos.split('-')) 8 | if (pw[i] == letter or pw[j] == letter) and pw[i] != pw[j]: 9 | ans += 1 10 | 11 | print(ans) 12 | -------------------------------------------------------------------------------- /kattis/quickbrownfox/solution.py: -------------------------------------------------------------------------------- 1 | n = int(raw_input()) 2 | t = 'abcdefghijklmnopqrstuvwxyz' 3 | for i in xrange(n): 4 | s = set([c for c in raw_input().lower() if c not in set('0123456789.,?!\'" ')]) 5 | if len(s) == len(t): 6 | print 'pangram' 7 | else: 8 | print 'missing', ''.join([c for c in t if c not in s]) 9 | 10 | -------------------------------------------------------------------------------- /kattis/textureanalysis/solution.py: -------------------------------------------------------------------------------- 1 | i = 1 2 | while True: 3 | s = input().strip() 4 | if s == 'END': 5 | break 6 | 7 | u = s.split('*') 8 | if len(u) > 2: 9 | u = u[1:-1] 10 | if len(set(u)) != 1: 11 | print(i, 'NOT EVEN') 12 | else: 13 | print(i, 'EVEN') 14 | i += 1 15 | -------------------------------------------------------------------------------- /project_euler/005/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func gcd(a, b int) int { 6 | for b != 0 { 7 | a, b = b, a % b 8 | } 9 | return a 10 | } 11 | 12 | func main() { 13 | r := 1 14 | for i := 20; i > 1; i -- { 15 | if r % i != 0 { 16 | r *= i/gcd(r, i) 17 | } 18 | } 19 | fmt.Println(r) 20 | } -------------------------------------------------------------------------------- /advent_of_code/2017/17/part2.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | 4 | step = int(input()) 5 | curr = 0 6 | after = None 7 | for buffer_length in range(1, 50000000 + 1): 8 | curr = ((curr + step + 1) % buffer_length) 9 | if curr == 0: 10 | # Goes after the zero 11 | after = buffer_length 12 | 13 | print(buffer) 14 | -------------------------------------------------------------------------------- /advent_of_code/2021/02/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | x = 0 3 | y = 0 4 | for line in sys.stdin.readlines(): 5 | command, dist = line.split() 6 | dist = int(dist) 7 | if command == 'forward': 8 | x += dist 9 | elif command == 'up': 10 | y -= dist 11 | else: 12 | y += dist 13 | 14 | print(x*y) 15 | -------------------------------------------------------------------------------- /hacker.org/103/solution.py: -------------------------------------------------------------------------------- 1 | # According to http://www.pictaculous.com, the image colour is #9C54C6 2 | # Other sites gave different values, apparently they're wrong 3 | s = '9C54C6' 4 | a = '' 5 | for i in range(3): 6 | f = s[i*2:i*2+2] 7 | n = int(s[i*2:i*2 + 2], 16) 8 | t = bin(n)[2:].zfill(8) 9 | a += t 10 | print(int(a, 2)) 11 | -------------------------------------------------------------------------------- /kattis/brokenswords/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | tb, lr = 0, 0 3 | for _ in range(n): 4 | s = [int(c) - 1 for c in input()] 5 | tb -= s[0] 6 | tb -= s[1] 7 | lr -= s[2] 8 | lr -= s[3] 9 | 10 | tot = 0 11 | while tb >= 2 and lr >= 2: 12 | tb -= 2 13 | lr -= 2 14 | tot += 1 15 | print(tot, tb, lr) 16 | -------------------------------------------------------------------------------- /kattis/gcpc/notes.md: -------------------------------------------------------------------------------- 1 | Can keep a set `better` of all teams ahead of team 1 in the standings. If team 1 solves a new problem, remove all members of `better` that are now worse than team 1. If another team solves a new problem, see if it is now ahead of team 1, and add it to the set. 2 | 3 | The position of team 1 is always `len(better) + 1`. 4 | -------------------------------------------------------------------------------- /kattis/sodasurpler/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int e, f, cost; 5 | scanf("%d %d %d", &e, &f, &cost); 6 | int empty = e + f; 7 | int ans = 0; 8 | while (empty >= cost) { 9 | empty -= cost; 10 | ++ans; 11 | ++empty; 12 | } 13 | printf("%d\n", ans); 14 | } 15 | -------------------------------------------------------------------------------- /kattis/tarifa/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int x, n; 5 | scanf("%d\n%d\n", &x, &n); 6 | long int g = x; 7 | int i, j; 8 | for (i = 0; i < n; ++i) { 9 | scanf("%d", &j); 10 | g += x; 11 | g -= j; 12 | } 13 | printf("%ld\n", g); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /codeforces/952/f/solution.py: -------------------------------------------------------------------------------- 1 | s = input().strip() + '+' 2 | ans = 0 3 | 4 | curr = 0 5 | sign = 1 6 | for c in s: 7 | if c in '-+': 8 | ans += sign*curr 9 | curr = 0 10 | if c == '-': 11 | sign = -1 12 | elif c == '+': 13 | sign = 1 14 | curr = curr*10 + (ord(c) - ord('0')) 15 | print(ans) 16 | -------------------------------------------------------------------------------- /hacker.org/132/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | a = sys.stdin.readlines() 4 | b = '' 5 | 6 | for i in range(192): 7 | s = a[i].strip() 8 | d = len(a[i]) - len(s) 9 | if d % 2 == 0: 10 | b += '0' 11 | else: 12 | b += '1' 13 | 14 | for i in range(len(b)//8): 15 | s = b[i*8:(i+1)*8] 16 | n = int(s, 2) 17 | print(chr(n), end='') 18 | -------------------------------------------------------------------------------- /hackerrank/challenges/a-very-big-sum/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n; 5 | scanf("%d", &n); 6 | 7 | long long ans = 0; 8 | long long d; 9 | int i; 10 | for (i = 0; i < n; ++i) { 11 | scanf("%lld", &d); 12 | ans += d; 13 | } 14 | printf("%lld\n", ans); 15 | } 16 | -------------------------------------------------------------------------------- /kattis/architecture/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | r, c = map(int, input().split()) 3 | east = list(map(int, input().split())) 4 | north = list(map(int, input().split())) 5 | poss = max(east) == max(north) 6 | if poss: 7 | print('possible') 8 | else: 9 | print('impossible') 10 | 11 | main() 12 | -------------------------------------------------------------------------------- /kattis/cakeymccakeface/notes.md: -------------------------------------------------------------------------------- 1 | An ad-hoc problem. Simply keep a count (here in a `defaultdict`) of each interval, and then output the interval with the highest frequency in the inputs. Stable sorting in python came in useful here. 2 | 3 | Took me a long time to figure out: If there's no valid answer, output 0. 4 | 5 | Runs in 3.64s in python3. 6 | -------------------------------------------------------------------------------- /kattis/engineeringenglish/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | d = set() 4 | 5 | for line in sys.stdin.readlines(): 6 | s = [] 7 | for word in line.split(): 8 | if word.lower() in d: 9 | s.append('.') 10 | else: 11 | s.append(word) 12 | d.add(word.lower()) 13 | print(' '.join(s)) 14 | -------------------------------------------------------------------------------- /kattis/fiftyshades/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | import sys 3 | n = int(input()) 4 | ans = [1 for line in sys.stdin.readlines() if 'pink' in line.lower() or 'rose' in line.lower()] 5 | if len(ans) == 0: 6 | print('I must watch Star Wars with my daughter') 7 | else: 8 | print(len(ans)) 9 | 10 | main() 11 | -------------------------------------------------------------------------------- /kattis/inflation/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | m = sorted([int(c) for c in input().split()]) 3 | 4 | ok = True 5 | mmax = 1 6 | for i in range(1, n+1): 7 | if m[i-1] > i: 8 | ok = False 9 | break 10 | mmax = min(mmax, m[i-1]/(i)) 11 | 12 | if ok: 13 | print(mmax) 14 | else: 15 | print('impossible') 16 | -------------------------------------------------------------------------------- /kattis/recount/solution.py: -------------------------------------------------------------------------------- 1 | d = {'duine eigin': -10} 2 | while True: 3 | s = input() 4 | if s == '***': 5 | break 6 | if s in d: 7 | d[s] += 1 8 | else: 9 | d[s] = 1 10 | 11 | v = sorted(d, key=lambda x:d[x]) 12 | if d[v[-1]] == d[v[-2]]: 13 | print('Runoff!') 14 | else: 15 | print(v[-1]) 16 | -------------------------------------------------------------------------------- /kattis/veci/solution.py: -------------------------------------------------------------------------------- 1 | def get_digits(n): 2 | return sorted(str(n)) 3 | 4 | n = int(input()) 5 | digs = get_digits(n) 6 | dig_n = len(digs) 7 | i = n + 1 8 | while True: 9 | new_digs = get_digits(i) 10 | if new_digs == digs: 11 | print (i) 12 | break 13 | elif len(new_digs) > dig_n: 14 | print (0) 15 | break 16 | i += 1 17 | -------------------------------------------------------------------------------- /spoj/SAMER08F/notes.md: -------------------------------------------------------------------------------- 1 | A number theory problem. If you do the first couple of examples on paper, you can see that the number of squares in an `N*N` grid = `N**2 + (N-1)**2 + (N-2)**2 + ... + 2**2 + 1**2`; ie. the sum of all squares up to `N`. However, there is a closed form solution for this: `(N*(N+1)*(2*N + 1))/6`. 2 | 3 | Runs in 0.00s in C++. 4 | -------------------------------------------------------------------------------- /advent_of_code/2021/08/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | ans = 0 5 | for line in sys.stdin.readlines(): 6 | parts = [part.strip().split() for part in line.split('|')] 7 | for num in parts[1]: 8 | if len(num) in [2, 3, 4, 7]: 9 | ans += 1 10 | 11 | print(ans) 12 | 13 | main() 14 | -------------------------------------------------------------------------------- /codeforces/1066/a/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | t = int(input()) 3 | for _ in range(t): 4 | L, v, l, r = map(int, input().split()) 5 | numbefore = (l-1)//v 6 | numduring = r//v 7 | numafter = L//v 8 | print (numafter - (numduring - numbefore)) 9 | 10 | if __name__ == '__main__': 11 | main() 12 | -------------------------------------------------------------------------------- /kattis/apaxiaaans/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | char c[251]; 6 | scanf("%s", c); 7 | int len = strlen(c); 8 | printf("%c", c[0]); 9 | int i; 10 | for (i = 1; i < len; ++i) { 11 | if (c[i] != c[i-1]) printf("%c", c[i]); 12 | } 13 | printf("\n"); 14 | } 15 | -------------------------------------------------------------------------------- /kattis/billiard/solution.py: -------------------------------------------------------------------------------- 1 | import math 2 | while True: 3 | a, b, s, m, n = map(int, input().split()) 4 | if a == b == s == n == m == 0: 5 | break 6 | x = a*m 7 | y = b*n 8 | h = math.hypot(x, y) 9 | vel = h/s 10 | angle = math.degrees(math.atan2(y, x)) 11 | print('{:.2f} {:.2f}'.format(angle, vel)) 12 | -------------------------------------------------------------------------------- /kattis/bookingaroom/solution.py: -------------------------------------------------------------------------------- 1 | a, b = [int(x) for x in raw_input().split()] 2 | 3 | booked = set() 4 | for i in xrange(b): 5 | n = int(raw_input()) 6 | booked.add(n) 7 | 8 | found = False 9 | for i in xrange(1, a+1): 10 | if i not in booked: 11 | print i 12 | found = True 13 | break 14 | 15 | if not found: 16 | print 'too late' 17 | -------------------------------------------------------------------------------- /kattis/cetvrta/solution.py: -------------------------------------------------------------------------------- 1 | xs = set() 2 | ys = set() 3 | for i in range(3): 4 | p = [int(n) for n in input().split()] 5 | if p[0] in xs: 6 | xs.remove(p[0]) 7 | else: 8 | xs.add(p[0]) 9 | if p[1] in ys: 10 | ys.remove(p[1]) 11 | else: 12 | ys.add(p[1]) 13 | 14 | print(xs.pop(), ys.pop()) 15 | -------------------------------------------------------------------------------- /kattis/sifferprodukt/solution.py: -------------------------------------------------------------------------------- 1 | import operator 2 | from functools import lru_cache, reduce 3 | 4 | @lru_cache(maxsize=None) 5 | def go(s): 6 | return reduce(operator.mul, (int(c) for c in s if c != '0')) 7 | 8 | def main(): 9 | s = input() 10 | while len(s) > 1: 11 | s = str(go(s)) 12 | print(s) 13 | 14 | main() 15 | -------------------------------------------------------------------------------- /project_euler/006/Solution.scala: -------------------------------------------------------------------------------- 1 | object Solution{ 2 | def main(args: Array[String]) = { 3 | var n = 100 4 | var sumOfSquares = 0 5 | var squareOfSums = 0 6 | for(i <- 1 to n) { 7 | sumOfSquares += i*i 8 | squareOfSums += i 9 | } 10 | squareOfSums *= squareOfSums 11 | println(squareOfSums - sumOfSquares) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /enigmagroup/cryptography/2/solution.md: -------------------------------------------------------------------------------- 1 | `VGhpcyBvbmUgd2FzIGJhc2U2NC4gSXQncyBhIGxpdHRsZSBoYXJkZXIgdGhhbiB0aGUgcHJldmlvdXMgbWlzc2lvbi4gVGhlID09IG9wZXJhdG9ycyBnaXZlIGJhc2U2NCBhd2F5Lg==` is clearly base64 encoded. When b64 decoded, it produces the answer: `This one was base64. It's a little harder than the previous mission. The == operators give base64 away.`. 2 | -------------------------------------------------------------------------------- /kattis/listgame/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | ans = 0 4 | while n % 2 == 0: 5 | n /= 2 6 | ans += 1 7 | a = 3 8 | m = n 9 | while a*a <= n: 10 | while a*a <= n and m % a > 0: 11 | a += 2 12 | if m % a != 0: 13 | break 14 | m //= a 15 | ans += 1 16 | if m > 1: 17 | ans += 1 18 | print(ans) 19 | -------------------------------------------------------------------------------- /leetcode/min-cost-climbing-stairs/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minCostClimbingStairs(self, cost): 3 | """ 4 | :type cost: List[int] 5 | :rtype: int 6 | """ 7 | tot = cost + [0] 8 | for i in range(2, len(tot)): 9 | tot[i] += min(tot[i-1], tot[i-2]) 10 | return tot[-1] 11 | -------------------------------------------------------------------------------- /spoj/ONP/notes.md: -------------------------------------------------------------------------------- 1 | A classic problem. Use the shunting yard algorithm to convert brackets notation to RPN. ie: 2 | 3 | 1. If you come across a variable, print it immediately. 4 | 2. If you come across an operation, add it to the stack. 5 | 3. If you come across a closing bracket, print the operation at the top of the stack. 6 | 7 | Runs in 0.00s in C++. 8 | -------------------------------------------------------------------------------- /hacker.org/162/solution.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | t = [] 3 | for i in range(len(s)//2): 4 | n = int(s[i*2:i*2+2], 16) 5 | t.append(n) 6 | 7 | for k in range(256): 8 | o = [] 9 | b = k 10 | for e in t: 11 | o.append(e^b) 12 | b = e 13 | f = ''.join(map(chr, o)) 14 | try: 15 | print(''.join(map(chr, o))) 16 | except Exception as e: 17 | pass 18 | -------------------------------------------------------------------------------- /kattis/bond/notes.md: -------------------------------------------------------------------------------- 1 | Assign bonds one by one. 2 | 3 | If bonds 0..M are assigned to M missions, it does not matter in which order they were assigned when considering where to assign bond M+1. The probability of completing the next mission does not depend on this. This means that we can memoise based on this set of assigned missions (implemented as a bitmask). 4 | -------------------------------------------------------------------------------- /kattis/bootstrappingnumber/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | 4 | lo = 1 5 | hi = 10 6 | 7 | while hi - lo > 0.00000001: 8 | mid = lo + (hi-lo)/2 9 | midn = mid**mid 10 | if midn > n: 11 | hi = mid 12 | else: 13 | lo = mid 14 | 15 | print(lo) 16 | 17 | main() 18 | -------------------------------------------------------------------------------- /kattis/cardtrick2/solution.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | for _ in range(int(input())): 4 | n = int(input()) 5 | d = deque() 6 | d.append(n) 7 | for i in range(n-1, 0, -1): 8 | for _ in range(i): 9 | d.appendleft(d.pop()) 10 | d.appendleft(i) 11 | d.appendleft(d.pop()) 12 | print(*d) 13 | -------------------------------------------------------------------------------- /kattis/minimumscalar/solution.py: -------------------------------------------------------------------------------- 1 | numtests = int(input()) 2 | 3 | for test in range(1, numtests+1): 4 | n = int(input()) 5 | a = sorted(map(int, input().split())) 6 | b = sorted(map(int, input().split()), reverse=True) 7 | tot = 0 8 | for i in range(n): 9 | tot += a[i]*b[i] 10 | print('Case #{}: {}'.format(test, tot)) 11 | -------------------------------------------------------------------------------- /kattis/rijeci/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n; 5 | scanf("%d", &n); 6 | long long int a = 1, b = 0; 7 | while (n > 0) { 8 | long long int na = b; 9 | long long int nb = a+b; 10 | a = na; 11 | b = nb; 12 | --n; 13 | } 14 | printf("%lld %lld\n", a, b); 15 | } 16 | -------------------------------------------------------------------------------- /kattis/datum/solution.py: -------------------------------------------------------------------------------- 1 | from datetime import date 2 | 3 | days = [ 4 | 'Monday', 5 | 'Tuesday', 6 | 'Wednesday', 7 | 'Thursday', 8 | 'Friday', 9 | 'Saturday', 10 | 'Sunday', 11 | ] 12 | day, month = [int(s) for s in input().split()] 13 | date = date(2009, month, day) 14 | print(days[date.weekday()]) 15 | -------------------------------------------------------------------------------- /kattis/drunkvigenere/solution.py: -------------------------------------------------------------------------------- 1 | alpha = 'abcdefghijklmnopqrstuvwxyz'.upper() 2 | d = {a:alpha.index(a) for a in alpha} 3 | 4 | encrypted = input() 5 | key = input() 6 | 7 | ans = '' 8 | for i, c in enumerate(encrypted): 9 | opp = -1 if (i%2 == 0) else 1 10 | a = alpha[(d[c] + opp*d[key[i]]) % len(alpha)] 11 | ans += a 12 | 13 | print(ans) 14 | -------------------------------------------------------------------------------- /kattis/hangingout/solution.py: -------------------------------------------------------------------------------- 1 | size, num = map(int, input().split()) 2 | curr = 0 3 | ans = 0 4 | for _ in range(num): 5 | action, n = input().split() 6 | n = int(n) 7 | if action[0] == 'e': 8 | if curr + n > size: 9 | ans += 1 10 | else: 11 | curr += n 12 | else: 13 | curr -= n 14 | print(ans) 15 | -------------------------------------------------------------------------------- /kattis/intergalacticbidding/notes.md: -------------------------------------------------------------------------------- 1 | A greedy problem. Because each bid[i] >= 2*bid[i-1], we know that the sum of all bids below bid[i] won't be large enough to reach the target, therefore bid[i] must be used. Keep recursing until either the target is reached, or we try to add the same bid twice (ie. it won't be possible to reach the goal). Runs in 0.05s in python3. 2 | -------------------------------------------------------------------------------- /kattis/shatteredcake/solution.py: -------------------------------------------------------------------------------- 1 | from sys import stdin 2 | 3 | def main(): 4 | read = stdin.readline 5 | w = int(read()) 6 | n = int(read()) 7 | tot = 0 8 | for _ in range(n): 9 | a = [int(x) for x in read().split()] 10 | tot += a[0]*a[1] 11 | 12 | print(tot//w) 13 | 14 | if __name__ == '__main__': 15 | main() 16 | -------------------------------------------------------------------------------- /project_euler/028/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | total := 1 9 | curr := 1 10 | for length := 3; length <= 1001; length += 2 { 11 | for side := 1; side <= 4; side ++ { 12 | curr += (length-1) 13 | total += curr 14 | //fmt.Println(curr) 15 | } 16 | } 17 | fmt.Println("total", total) 18 | } -------------------------------------------------------------------------------- /kattis/apaxianparent/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | y, p = input().split() 3 | if y[-1] == 'e': 4 | print('{}x{}'.format(y, p)) 5 | elif y[-1] in 'aeiou': 6 | print('{}ex{}'.format(y[:-1], p)) 7 | elif y.endswith('ex'): 8 | print('{}{}'.format(y, p)) 9 | else: 10 | print('{}ex{}'.format(y, p)) 11 | 12 | main() 13 | -------------------------------------------------------------------------------- /kattis/beavergnaw/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | while (1) { 6 | int D; 7 | long long int v; 8 | scanf("%d %lld", &D, &v); 9 | if (D == 0) break; 10 | double d3 = D*D*D - 6*v/M_PI; 11 | double d = pow(d3, 1/3.0); 12 | printf("%.7lf\n", d); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /kattis/cetiri/solution.py: -------------------------------------------------------------------------------- 1 | n = sorted(list(map(int, input().split()))) 2 | 3 | if n[2] - n[1] == n[1] - n[0]: 4 | # goes before or after 5 | print(n[2] + abs(n[2]-n[1])) 6 | elif n[2] - n[1] > n[1] - n[0]: 7 | # goes between 1 and 2 8 | print(n[1] + abs(n[1]-n[0])) 9 | else: 10 | # goes between 0 and 1 11 | print(n[0] + abs(n[2]-n[1])) 12 | -------------------------------------------------------------------------------- /kattis/chanukah/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | import sys 3 | read = sys.stdin.readline 4 | n = int(read()) 5 | results = [] 6 | for _ in range(n): 7 | id_, v = read().split() 8 | v = int(v) 9 | ans = v*(v+1)//2 + v 10 | results.append(f'{id_} {ans}') 11 | sys.stdout.write('\n'.join(results)) 12 | 13 | main() 14 | -------------------------------------------------------------------------------- /kattis/kitten/solution.py: -------------------------------------------------------------------------------- 1 | d = {} 2 | 3 | k = int(input()) 4 | 5 | while True: 6 | p = list(map(int, input().split())) 7 | if len(p) == 1: 8 | break 9 | for j in p[1:]: 10 | d[j] = p[0] 11 | 12 | while True: 13 | print(k, end=' ') 14 | if k in d: 15 | k = d[k] 16 | else: 17 | print() 18 | break 19 | -------------------------------------------------------------------------------- /leetcode/powx-n/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def myPow(self, x: float, n: int) -> float: 3 | if n == 0: 4 | return 1 5 | if n < 0: 6 | return 1/self.myPow(x, -n) 7 | tmp = self.myPow(x, n//2) 8 | if n % 2 == 0: 9 | return tmp * tmp 10 | else: 11 | return x * tmp * tmp 12 | -------------------------------------------------------------------------------- /advent_of_code/2020/02/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | ans = 0 4 | for line in sys.stdin.readlines(): 5 | policy, pw = line.split(':') 6 | freq, letter = policy.split(' ') 7 | minf, maxf = map(int, freq.split('-')) 8 | letter_count = pw.count(letter) 9 | if letter_count >= minf and letter_count <= maxf: 10 | ans += 1 11 | 12 | print(ans) 13 | -------------------------------------------------------------------------------- /advent_of_code/2022/01/part2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | elves = [] 4 | curr = [] 5 | for line in sys.stdin.readlines(): 6 | if line.strip() == '': 7 | elves.append(curr) 8 | curr = [] 9 | else: 10 | curr.append(int(line)) 11 | elves.append(curr) 12 | 13 | tot = list(sorted((sum(e) for e in elves), reverse=True)) 14 | print(sum(tot[:3])) 15 | -------------------------------------------------------------------------------- /advent_of_code/2022/04/part2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | ans = 0 5 | for line in sys.stdin.readlines(): 6 | a, b = line.strip().split(',') 7 | i, j = map(int, a.split('-')) 8 | p, q = map(int, b.split('-')) 9 | if j < p or q < i: 10 | continue 11 | ans += 1 12 | print(ans) 13 | 14 | main() 15 | -------------------------------------------------------------------------------- /kattis/cups/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | digs = set('0123456789') 4 | cols = {} 5 | 6 | for i in range(n): 7 | parts = input().split() 8 | if all([c in digs for c in parts[0]]): 9 | cols[parts[1]] = int(parts[0])//2 10 | else: 11 | cols[parts[0]] = int(parts[1]) 12 | 13 | print('\n'.join(sorted(cols, key=lambda x: cols[x]))) 14 | -------------------------------------------------------------------------------- /kattis/friday/solution.py: -------------------------------------------------------------------------------- 1 | num_tests = int(input()) 2 | for _ in range(num_tests): 3 | days, months = map(int, input().split()) 4 | num_so_far = 0 5 | ans = 0 6 | 7 | for m in map(int, input().split()): 8 | if m >= 13: 9 | if (num_so_far + 13) % 7 == 6: 10 | ans += 1 11 | num_so_far += m 12 | print(ans) 13 | -------------------------------------------------------------------------------- /kattis/heliocentric/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | t = 1 4 | for line in sys.stdin.readlines(): 5 | e, m = map(int, line.split()) 6 | n = 0 7 | if e != 0: 8 | n = 365 - e 9 | m = (m+n)%687 10 | e = 0 11 | while m != 0: 12 | m = (m+365)%687 13 | n += 365 14 | print('Case {}: {}'.format(t, n)) 15 | t += 1 16 | -------------------------------------------------------------------------------- /kattis/kleptography/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | alpha = 'abcdefghijklmnopqrstuvwxyz' 3 | n, m = map(int, input().split()) 4 | last = input() 5 | b = input() 6 | 7 | a = list(' '*(m-n) + last) 8 | for i in range(m-1, n-1, -1): 9 | a[i-n] = chr(ord('a') + (26 + ord(b[i]) - ord(a[i])) % 26) 10 | print(''.join(a)) 11 | 12 | main() 13 | -------------------------------------------------------------------------------- /kattis/triangleornaments/notes.md: -------------------------------------------------------------------------------- 1 | A triginometry and geometry problem. 2 | 3 | First, find the centroid of the triangle. You can take the point opposite to line C to be the origin. 4 | 5 | Then find the angle that the line C is at to this point (the centroid). 6 | 7 | Find the width of the line C at this angle with cos(). This is the answer of the width of each triangle. 8 | -------------------------------------------------------------------------------- /leetcode/sum-of-square-numbers/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def judgeSquareSum(self, c: int) -> bool: 3 | squares = set() 4 | i = 0 5 | while i*i <= c: 6 | squares.add(i*i) 7 | i += 1 8 | for n in squares: 9 | if (c - n) in squares: 10 | return True 11 | return False 12 | -------------------------------------------------------------------------------- /kattis/lektira/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | s = input().strip() 3 | ans = 'zzzzzzzzzzzzzz' 4 | for i in range(1, len(s)): 5 | for j in range(i+1, len(s)): 6 | a = s[:i][::-1] 7 | b = s[i:j][::-1] 8 | c = s[j:][::-1] 9 | t = a + b + c 10 | ans = min(t, ans) 11 | print(ans) 12 | 13 | main() 14 | -------------------------------------------------------------------------------- /kattis/oddmanout/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | for i in range(n): 4 | m = int(input()) 5 | v = list(map(int, input().split())) 6 | d = set() 7 | poss = set(v) 8 | for j in v.copy(): 9 | if j in d: 10 | poss.remove(j) 11 | d.remove(j) 12 | d.add(j) 13 | print('Case #{}: {}'.format(i+1, poss.pop())) 14 | -------------------------------------------------------------------------------- /kattis/whichbase/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for _ in range(n): 3 | a, v = input().split() 4 | x, y, z = 0, 0, 0 5 | try: 6 | x = int(v, 8) 7 | except: 8 | pass 9 | try: 10 | y = int(v) 11 | except: 12 | pass 13 | try: 14 | z = int(v, 16) 15 | except: 16 | pass 17 | print(a, x, y, z) 18 | -------------------------------------------------------------------------------- /project_euler/002/solution.hs: -------------------------------------------------------------------------------- 1 | -- memoized fibonacci 2 | fib :: Int -> Int 3 | fib x = if x < 1 4 | then 0 5 | else if x == 1 then 1 6 | else fibs!!(x-1)+fibs!!(x-2) 7 | where 8 | fibs = map fib[0..] 9 | 10 | fibs n = [(fib x) | x <- [1..n]] 11 | 12 | evenFibs n = [fib x | x <- [3,6..n]] 13 | 14 | main = print (foldl (+) 0 (filter (\n -> n < 4000000) (evenFibs 34))) -------------------------------------------------------------------------------- /uva/10878/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | ans = '' 3 | input() 4 | while True: 5 | s = input() 6 | if s[1] == '_': 7 | break 8 | s = s.replace('o', '1').replace(' ', '0').replace('.', '')[1:-1] 9 | n = int(s, 2) 10 | ans += chr(n) 11 | print(ans, end='') 12 | 13 | if __name__ == '__main__': 14 | main() 15 | -------------------------------------------------------------------------------- /kattis/esej/notes.md: -------------------------------------------------------------------------------- 1 | A strings problem. If all words outputted are unique, then you don't have to worry about keeping track of previous words. You can do this by generating words in alphabetical order. There are much more 4-letter words possible than the maximum output size of the question, so by iterating four pointers across the alphabet, you can output enough unique words easily. 2 | -------------------------------------------------------------------------------- /kattis/oddities/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n; 5 | scanf("%d", &n); 6 | int i; 7 | for (i = 0; i < n; ++i) { 8 | int x; 9 | scanf("%d", &x); 10 | if (x % 2 == 0) { 11 | printf("%d is even\n", x); 12 | } else { 13 | printf("%d is odd\n", x); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /kattis/sidewayssorting/solution.py: -------------------------------------------------------------------------------- 1 | while True: 2 | n, d = map(int, input().split()) 3 | if n == d == 0: 4 | break 5 | g = [""]*d 6 | for i in range(n): 7 | s = input() 8 | for j in range(d): 9 | g[j] += (s[j]) 10 | 11 | g = sorted(g, key = lambda x: x.lower()) 12 | for i in range(n): 13 | for j in range(d): 14 | print(g[j][i], end='') 15 | print() 16 | 17 | -------------------------------------------------------------------------------- /advent_of_code/2021/02/part2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | x = 0 3 | y = 0 4 | aim = 0 5 | for line in sys.stdin.readlines(): 6 | command, dist = line.split() 7 | dist = int(dist) 8 | if command == 'forward': 9 | x += dist 10 | y += aim*dist 11 | elif command == 'up': 12 | aim -= dist 13 | else: # 'down' 14 | aim += dist 15 | 16 | print(x*y) 17 | -------------------------------------------------------------------------------- /kattis/downtime/solution.py: -------------------------------------------------------------------------------- 1 | from math import ceil 2 | 3 | num_req, rps = map(int, input().split()) 4 | heap = [] 5 | for _ in range(num_req): 6 | n = int(input()) 7 | heap.append((n, 1)) 8 | heap.append((n+1000, -1)) 9 | heap = sorted(heap) 10 | top = 0 11 | curr = 0 12 | for _, d in heap: 13 | curr += d 14 | top = max(top, curr) 15 | 16 | print(ceil(top/rps)) 17 | -------------------------------------------------------------------------------- /kattis/triangle/solution.py: -------------------------------------------------------------------------------- 1 | # num triangles for each step in seq = 3^(n) 2 | # perimeter of each triangle = 3/(2^(n-1)) 3 | # mult these = (3^(n+1))/(2^n) 4 | 5 | import sys 6 | 7 | curr = 1 8 | for line in sys.stdin.readlines(): 9 | n = int(line) 10 | val = (3**(n+1))//(2**n) 11 | ans = len(str(val)) 12 | print('Case {}: {}'.format(curr, ans)) 13 | curr += 1 14 | -------------------------------------------------------------------------------- /leetcode/group-anagrams/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def groupAnagrams(self, strs): 3 | """ 4 | :type strs: List[str] 5 | :rtype: List[List[str]] 6 | """ 7 | from collections import defaultdict 8 | d = defaultdict(list) 9 | for s in strs: 10 | d[str(sorted(s))].append(s) 11 | return [d[e] for e in d] 12 | -------------------------------------------------------------------------------- /primers/art_optimal/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | w, h = map(int, input().split(',')) 3 | lines = [[(c == '#') for c in input().strip()] for _ in range(h)] 4 | for j in range(h): 5 | #print(''.join(('#' if c else '*') for c in lines[j])) 6 | for i in range(w): 7 | if lines[j][i]: 8 | print(f'FILL,{i},{j},1') 9 | 10 | main() 11 | -------------------------------------------------------------------------------- /project_euler/091/solution.py: -------------------------------------------------------------------------------- 1 | from fractions import gcd 2 | 3 | n = 50 4 | 5 | # num with two edges of triangle on edges of graph 6 | ans = n*n*3 7 | 8 | # num with one edge of triangle on edge of graph 9 | for x in xrange(1, n+1): 10 | for y in xrange(1, n+1): 11 | mov = gcd(x,y) 12 | a = (y*mov)/x 13 | b = ((n-x)*mov)//y 14 | ans += min(a, b)*2 15 | 16 | print ans 17 | -------------------------------------------------------------------------------- /kattis/artichoke/solution.py: -------------------------------------------------------------------------------- 1 | from math import sin, cos 2 | 3 | p, a, b, c, d, n = map(int, input().split()) 4 | 5 | def price(k): 6 | return p*(sin(a*k + b) + cos(c*k + d) + 2) 7 | 8 | biggest_drop = 0 9 | maxx = 0 10 | for ni in range(1, n+1): 11 | v = price(ni) 12 | maxx = max(v, maxx) 13 | biggest_drop = max(maxx - v, biggest_drop) 14 | 15 | print(biggest_drop) 16 | -------------------------------------------------------------------------------- /kattis/humancannonball2/solution.py: -------------------------------------------------------------------------------- 1 | from math import cos, sin, radians 2 | 3 | n = int(input()) 4 | for _ in range(n): 5 | vel, angle, x, h1, h2 = map(float, input().split()) 6 | r = radians(angle) 7 | t = x/(vel*cos(r)) 8 | y = (vel*t*sin(r)) - (0.5*(9.81)*t*t) 9 | if h1 + 1 <= y <= h2 - 1: 10 | print('Safe') 11 | else: 12 | print('Not Safe') 13 | -------------------------------------------------------------------------------- /kattis/musicyourway/solution.py: -------------------------------------------------------------------------------- 1 | attrs = input().split() 2 | n_songs = int(input()) 3 | songs = [input().split() for _ in range(n_songs)] 4 | n = int(input()) 5 | for _ in range(n): 6 | k = input().strip() 7 | v = attrs.index(k) 8 | songs = sorted(songs, key=lambda x:x[v]) 9 | print(' '.join(attrs)) 10 | for s in songs: 11 | print(' '.join(s)) 12 | print() 13 | -------------------------------------------------------------------------------- /kattis/thanos/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | for _ in range(n): 4 | a, b, c = map(int, input().split()) 5 | ans = 0 6 | while True: 7 | if a > c: 8 | print(ans) 9 | break 10 | a *= b 11 | ans += 1 12 | 13 | 14 | if __name__ == '__main__': 15 | main() 16 | -------------------------------------------------------------------------------- /hackerrank/w26/game-with-cells/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | * ... 5 | * .XX 6 | * .XX 7 | */ 8 | 9 | int main() { 10 | long int n, m; 11 | scanf("%ld %ld", &n, &m); 12 | long int num_across = n/2; 13 | if (n % 2 == 1) ++num_across; 14 | long int num_down = m/2; 15 | if (m % 2 == 1) ++num_down; 16 | printf("%ld\n", num_across*num_down); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /kattis/emergency/solution.py: -------------------------------------------------------------------------------- 1 | def calc(a, b, k): 2 | ans = b-a 3 | if ans <= 1: 4 | return ans 5 | naa = k # Nearest multiple of k above a 6 | nbb = (b//k)*k # Nearest multiple of k below b 7 | if nbb == 0: 8 | nbb += k 9 | ans = min(ans, 1 + (naa-a) + (b-nbb)) 10 | return ans 11 | 12 | b, k = map(int, input().split()) 13 | print(calc(0, b-1, k)) 14 | -------------------------------------------------------------------------------- /kattis/fodelsedagsmemorisering/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | d = {} # (name, val) 4 | for _ in range(n): 5 | name, val, bday = input().split() 6 | val = int(val) 7 | if bday not in d or d[bday][1] < val: 8 | d[bday] = (name, val) 9 | print(len(d)) 10 | print('\n'.join(sorted(d[b][0] for b in d))) 11 | 12 | main() 13 | -------------------------------------------------------------------------------- /kattis/walrusweights/notes.md: -------------------------------------------------------------------------------- 1 | Iterate through the numbers, summing each one and every other number you have ever seen before (a set called `seen`), and making this your new `seen` set. However, this will run out of time. But you can throw away any numbers that are obviously too large and will not get you any closer to your goal of `1000`. This solution runs in 0.05s in python2, and 0.25s in python3. 2 | -------------------------------------------------------------------------------- /rosalind/bioinf_stronghold/hamm/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | char a[2000], b[2000]; 6 | scanf("%s", a); 7 | scanf("%s", b); 8 | int i; 9 | int n = strlen(a); 10 | int ans = 0; 11 | for (i = 0; i < n; ++i) { 12 | if (a[i] != b[i]) ++ans; 13 | } 14 | printf("%d\n", ans); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /advent_of_code/2017/15/part1.py: -------------------------------------------------------------------------------- 1 | PREFIX = '0'*16 2 | 3 | def getbin(c): 4 | s = PREFIX + bin(c)[2:] 5 | return s[-16:] 6 | 7 | a = int(input().split()[-1]) 8 | b = int(input().split()[-1]) 9 | 10 | ans = 0 11 | for step in range(40000000): 12 | a = a*16807 % 2147483647 13 | b = b*48271 % 2147483647 14 | if getbin(a) == getbin(b): 15 | ans += 1 16 | 17 | print(ans) 18 | -------------------------------------------------------------------------------- /advent_of_code/2025/01/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | ans = 0 5 | dial = 50 6 | for line in sys.stdin.readlines(): 7 | d, n = line[0], int(line[1:]) 8 | if d == 'L': 9 | dial -= n 10 | else: 11 | dial += n 12 | dial %= 100 13 | if dial == 0: 14 | ans += 1 15 | print(ans) 16 | 17 | main() 18 | -------------------------------------------------------------------------------- /kattis/polymul1/solution.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | for _ in range(t): 3 | n = int(input()) + 1 4 | p = list(map(int, input().split())) 5 | m = int(input()) + 1 6 | q = list(map(int, input().split())) 7 | order = [0 for _ in range(n+m-1)] 8 | for i in range(n): 9 | for j in range(m): 10 | order[i+j] += p[i]*q[j] 11 | print(n+m-2) 12 | print(*order) 13 | -------------------------------------------------------------------------------- /kattis/primesieve/notes.md: -------------------------------------------------------------------------------- 1 | A prime sieve problem. The Sieve of Eratosthenes will work. A bitset should be used, to cut down on memory usage. The speedup of only considering odd numbers in this sieve cuts the program runtime in half. The attached C++ implementation runs in 0.55s. Integrating more wheels into the sieve (eg. cutting out the calc of powers of 3) would undoubtedly make it more efficient too. 2 | -------------------------------------------------------------------------------- /leetcode/two-sum/solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def twoSum(self, nums, target): 3 | """ 4 | :type nums: List[int] 5 | :type target: int 6 | :rtype: List[int] 7 | """ 8 | for i in range(len(nums)-1): 9 | for j in range(i+1, len(nums)): 10 | if nums[i] + nums[j] == target: 11 | return [i, j] 12 | -------------------------------------------------------------------------------- /project_euler/005/Solution.scala: -------------------------------------------------------------------------------- 1 | object Solution{ 2 | def gcd(x : Int, y : Int) : Int = { 3 | var a = x 4 | var b = y 5 | while(b != 0){ 6 | var tmp = b 7 | b = a % b 8 | a = tmp 9 | } 10 | return a 11 | } 12 | 13 | def main(args: Array[String]) = { 14 | var prod = 1 15 | for(i <- 2 to 19){ 16 | prod *= i/gcd(prod, i) 17 | } 18 | println(prod) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /kattis/drmmessages/solution.py: -------------------------------------------------------------------------------- 1 | ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 2 | 3 | def rotate(t): 4 | val = sum(ALPHABET.find(c) for c in t) 5 | return ''.join(ALPHABET[(ALPHABET.find(c)+val)%len(ALPHABET)] for c in t) 6 | 7 | s = input() 8 | a, b = rotate(s[:len(s)//2]), rotate(s[len(s)//2:]) 9 | print(''.join(ALPHABET[(ALPHABET.find(c) + ALPHABET.find(d))%len(ALPHABET)] for c,d in zip(a,b))) 10 | -------------------------------------------------------------------------------- /kattis/islands/solution.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | ans = 0 5 | 6 | ti, *d = map(int, input().split()) 7 | d = list(d) 8 | 9 | for i in range(1, len(d)-1): 10 | m = d[i] 11 | for j in range(i+1, len(d)): 12 | m = min(m, d[j-1]) 13 | if d[i-1] < m and d[j] < m: 14 | ans += 1 15 | 16 | print(ti, ans) 17 | -------------------------------------------------------------------------------- /kattis/lost/notes.md: -------------------------------------------------------------------------------- 1 | It is sufficient to do a breadth first search (with a priority queue), minimising the number of steps across languages, and secondarily minimising cost, keeping a global table of these values, and never entering a node if the number of steps it has taken you > the number of steps it took you before, or if the number of steps is equal, if the cost it took you is higher than the cost before. 2 | -------------------------------------------------------------------------------- /kattis/synchronizinglists/solution.py: -------------------------------------------------------------------------------- 1 | while True: 2 | n = int(input()) 3 | if n == 0: 4 | break 5 | a = [int(input()) for _ in range(n)] 6 | b = [int(input()) for _ in range(n)] 7 | sa = sorted(a) 8 | sb = sorted(b) 9 | c = list(zip(sa, sb)) 10 | #d = sorted(c, key=lambda x: a.index(x[0])) 11 | c.sort(key=lambda x: a.index(x[0])) 12 | print('\n'.join([str(e[1]) for e in c])) 13 | print() 14 | -------------------------------------------------------------------------------- /advent_of_code/2015/04/part1.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "crypto/md5" 6 | //"encoding/hex" 7 | "strconv" 8 | ) 9 | 10 | func main() { 11 | 12 | key := "yzbqklnj" 13 | for i := 1; ; i ++ { 14 | k := key + strconv.Itoa(i) 15 | 16 | hexa := fmt.Sprintf("%x", md5.Sum([]byte(k))) 17 | 18 | if hexa[0:5] == "00000" { 19 | fmt.Println(i, hexa) 20 | return 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /hacker.org/159/solution.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | t = [] 3 | for i in range(len(s)//2): 4 | n = int(s[i*2:i*2+2], 16) 5 | t.append(n) 6 | 7 | for k in range(256): 8 | for x in range(256): 9 | o = [] 10 | b = k 11 | for e in t: 12 | o.append(e^b) 13 | b = (b + x) % 256 14 | f = ''.join(map(chr, o)) 15 | if 'th' in f: 16 | try: 17 | print(f) 18 | except Exception as e: 19 | pass 20 | -------------------------------------------------------------------------------- /kattis/airconditioned/solution.py: -------------------------------------------------------------------------------- 1 | n = int(raw_input()) 2 | 3 | ms = [] 4 | for i in xrange(n): 5 | s = raw_input().split() 6 | m = (int(s[0]), int(s[1])) 7 | ms.append(m) 8 | 9 | ms = sorted(ms, key=lambda x: x[1]) 10 | 11 | ans = 0 12 | while len(ms): 13 | ans += 1 14 | t = ms[0][1] 15 | x = 0 16 | while x < len(ms) and (ms[x][0] <= t <= ms[x][1]): 17 | x += 1 18 | ms = ms[x:] 19 | print ans 20 | -------------------------------------------------------------------------------- /kattis/allpairspath/notes.md: -------------------------------------------------------------------------------- 1 | An all-pairs shortest-paths is best solved by Floyd-Warshall. However, this one also includes negative edges. In this case, complete a normal Floyd-Warshall, and then afterwards try to find negative loops. Any node with a negative loop from itself to itself, will mark all nodes with edges from it as infinitely looping too. 2 | 3 | Too slow for python3, but in python2 it runs in 0.93s. 4 | -------------------------------------------------------------------------------- /kattis/grassseed/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double cost; 5 | int lawns; 6 | scanf("%lf", &cost); 7 | scanf("%d", &lawns); 8 | int lawn; 9 | double area = 0; 10 | for (lawn = 0; lawn < lawns; ++lawn) { 11 | double w, h; 12 | scanf("%lf %lf", &w, &h); 13 | area += w*h; 14 | } 15 | printf("%.8lf\n", area*cost); 16 | } 17 | -------------------------------------------------------------------------------- /kattis/mathhomework/solution.py: -------------------------------------------------------------------------------- 1 | *legs, tot = map(int, input().split()) 2 | 3 | found = False 4 | for i in range(tot//legs[0] + 1): 5 | for j in range(tot//legs[1] + 1): 6 | for k in range(tot//legs[2] + 1): 7 | if legs[0]*i + legs[1]*j + legs[2]*k == tot: 8 | found |= True 9 | print(i, j, k) 10 | 11 | if not found: 12 | print('impossible') 13 | 14 | -------------------------------------------------------------------------------- /kattis/modulo/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main () { 5 | int found [1000]; 6 | memset(found, 0, sizeof(found)); 7 | int i; 8 | for (i = 0; i < 10; ++i) { 9 | int n; 10 | scanf("%d", &n); 11 | found[n%42] = 1; 12 | } 13 | int tot = 0; 14 | for (i = 0; i < 1000; ++i) { 15 | if (found[i] > 0) ++tot; 16 | } 17 | printf("%d\n", tot); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /kattis/pet/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int best = 0; 5 | short besti = -1; 6 | short i; 7 | for (i = 1; i < 6; ++i) { 8 | int a, b, c, d; 9 | scanf("%d %d %d %d", &a, &b, &c, &d); 10 | if (a+b+c+d > best) { 11 | best = a+b+c+d; 12 | besti = i; 13 | } 14 | } 15 | printf("%hd %d", besti, best); 16 | } 17 | -------------------------------------------------------------------------------- /kattis/segmentdistance/notes.md: -------------------------------------------------------------------------------- 1 | [This stackoverflow question](https://stackoverflow.com/questions/2824478/shortest-distance-between-two-line-segments) helped. 2 | 3 | 1. If the line segments intersect, the answer is 0. 4 | 2. Otherwise, the answer is the shortest distance from either point on one line to the other line segment. 5 | - If the lines are parallel, collinear, then (1) will not work, but (2) will. 6 | -------------------------------------------------------------------------------- /advent_of_code/2015/04/part2.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "crypto/md5" 6 | //"encoding/hex" 7 | "strconv" 8 | ) 9 | 10 | func main() { 11 | 12 | key := "yzbqklnj" 13 | for i := 1; ; i ++ { 14 | k := key + strconv.Itoa(i) 15 | 16 | hexa := fmt.Sprintf("%x", md5.Sum([]byte(k))) 17 | 18 | if hexa[0:6] == "000000" { 19 | fmt.Println(i, hexa) 20 | return 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /kattis/subseqhard/solution.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | ntests = int(input()) 4 | for test in range(ntests): 5 | input(), input() 6 | m = map(int, input().split()) 7 | d = defaultdict(lambda: 0) 8 | tot = 0 9 | ans = 0 10 | for v in m: 11 | d[tot] += 1 12 | tot += v 13 | if tot - 47 in d: 14 | ans += d[tot-47] 15 | print(ans) 16 | -------------------------------------------------------------------------------- /advent_of_code/2020/13/part1.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | target = int(input()) 3 | bus = [None if c == 'x' else int(c) for c in input().split(',')] 4 | active_bus = [b for b in bus if b is not None] 5 | 6 | t = target 7 | while True: 8 | for b in active_bus: 9 | if t % b == 0: 10 | print((t - target)*b) 11 | return 12 | t += 1 13 | 14 | main() 15 | -------------------------------------------------------------------------------- /advent_of_code/2025/09/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def area(a, b, x, y): 4 | return (1+abs(a-x))*(1+abs(b-y)) 5 | 6 | def main(): 7 | g = [] 8 | ans = 0 9 | for line in sys.stdin.readlines(): 10 | a, b = map(int, line.strip().split(',')) 11 | for x, y in g: 12 | ans = max(ans, area(a, b, x, y)) 13 | g.append((a, b)) 14 | 15 | print(ans) 16 | 17 | main() 18 | -------------------------------------------------------------------------------- /kattis/classy/notes.md: -------------------------------------------------------------------------------- 1 | Ad-hoc sorting problem. Took ages to get right. 2 | 3 | Convert ["higher", "middle", "lower"] to [3, 2, 1] in each array of classes inputted. Reverse each person's list of numbers to get the order of precedence right. Then right-pad this list with 2s so all names have the same length of classes. Stable-sort these first by name, and then backwards by this class array. 4 | 5 | Runs in 0.3s in python3. 6 | -------------------------------------------------------------------------------- /kattis/lastfactorialdigit/solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int last[11]; 5 | last[1] = 1; 6 | for (int i = 2; i < 11; ++i) { 7 | last[i] = (last[i-1]*i)%10; 8 | } 9 | int n; 10 | scanf("%d", &n); 11 | for (int i = 0; i < n; ++i) { 12 | int m; 13 | scanf("%d", &m); 14 | printf("%d\n", last[m]); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /kattis/tritiling/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long int dp[31]; 5 | int i; 6 | for (i = 0; i < 31; ++i) { 7 | dp[i] = 0; 8 | } 9 | dp[0] = 1; 10 | dp[2] = 3; 11 | for (i = 4; i < 31; ++i) { 12 | dp[i] = 4*dp[i-2] - dp[i-4]; 13 | } 14 | while (1) { 15 | int n; 16 | scanf("%d", &n); 17 | if (n < 0) break; 18 | printf("%lld\n", dp[n]); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /advent_of_code/2017/03/part1.py: -------------------------------------------------------------------------------- 1 | def calc(n): 2 | highest_square = 1 3 | while (highest_square+2)**2 < n: 4 | highest_square += 2 5 | side = highest_square+2 6 | progress = n - (highest_square**2) 7 | dist_from_corner = (progress) % (side-1) 8 | 9 | dist_from_centre = abs(dist_from_corner - (side//2)) 10 | radius = side//2 11 | return dist_from_centre + radius 12 | 13 | n = int(input()) 14 | print(calc(n)) 15 | -------------------------------------------------------------------------------- /hackerrank/challenges/compare-the-triplets/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () { 4 | int a = 0; 5 | int b = 0; 6 | 7 | int i, j, k; 8 | int l, m, n; 9 | 10 | scanf("%d %d %d", &i, &j, &k); 11 | scanf("%d %d %d", &l, &m, &n); 12 | 13 | if (i > l) ++a; 14 | if (j > m) ++a; 15 | if (k > n) ++a; 16 | if (i < l) ++b; 17 | if (j < m) ++b; 18 | if (k < n) ++b; 19 | printf("%d %d\n", a, b); 20 | } 21 | -------------------------------------------------------------------------------- /kattis/fizzbuzz/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, x, y; 5 | scanf("%d %d %d", &x, &y, &n); 6 | int i; 7 | for(i = 1; i <= n; ++i) { 8 | if (i % x == 0) { 9 | if (i % y == 0) { 10 | printf("FizzBuzz\n"); 11 | } else { 12 | printf("Fizz\n"); 13 | } 14 | } else if (i % y == 0) { 15 | printf("Buzz\n"); 16 | } else { 17 | printf("%d\n", i); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /kattis/halfacookie/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from math import * 3 | 4 | for line in sys.stdin.readlines(): 5 | r, x, y = map(float, line.split()) 6 | d = (x**2 + y**2)**0.5 7 | if d > r: 8 | print('miss') 9 | continue 10 | circle_area = pi * (r**2) 11 | theta = 2*acos(d/r) 12 | segment_area = (r**2)*(theta - sin(theta))/2 13 | print(circle_area - segment_area, segment_area) 14 | -------------------------------------------------------------------------------- /kattis/memorymatch/notes.md: -------------------------------------------------------------------------------- 1 | An ad-hoc problem. 2 | 3 | It is trivial to match cards where both locations of the same card is known. 4 | If for every other card type, you know where one of the pair is, it is possible to solve every card by turning over one of the unknown locations and matching it with wherever its known match is. 5 | Otherwise, if there are only 2 cards left, they must match. 6 | 7 | Runs in 0.03s in Python3. 8 | -------------------------------------------------------------------------------- /kattis/rotatecut/solution.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, s = input().strip().split() 5 | n = int(n) 6 | m = len(s) 7 | flip = False 8 | for _ in range(n): 9 | d = (m//4) 10 | if d == 0: 11 | break 12 | flip = not flip 13 | m -= d 14 | s = s[::-1][:m] 15 | if flip: 16 | s = s[::-1] 17 | print(s) 18 | 19 | -------------------------------------------------------------------------------- /kattis/stararrangements/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | print('{}:'.format(n)) 3 | for i in range(2, n): 4 | for j in range(max(i-1, 1), i+1): 5 | a = n//i 6 | for k in range(1, a+1): 7 | left = n - k*i 8 | if left % j != 0: 9 | continue 10 | b = left//j 11 | if k == b or k == b+1: 12 | print('{},{}'.format(i, j)) 13 | 14 | -------------------------------------------------------------------------------- /kattis/backspace/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | char in[1000000]; 6 | char out[1000000]; 7 | scanf("%s", in); 8 | int i = 0, j = 0; 9 | int n = strlen(in); 10 | for (; i < n; ++i) { 11 | if (in[i] == '<') { 12 | if(j > 0) j--; 13 | } else { 14 | out[j] = in[i]; 15 | ++j; 16 | } 17 | } 18 | out[j] = '\0'; 19 | printf("%s\n", out); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /kattis/commercials/solution.py: -------------------------------------------------------------------------------- 1 | def max_subarray(a): 2 | # kadane's algorithm 3 | max_ending_here = a[0] 4 | max_so_far = 0 5 | for x in a[1:]: 6 | max_ending_here = max(x, max_ending_here + x) 7 | max_so_far = max(max_so_far, max_ending_here) 8 | return max_so_far 9 | 10 | n, p = map(int, input().split()) 11 | a = list(map(lambda x: int(x) - p, input().split())) 12 | print(max_subarray(a)) 13 | -------------------------------------------------------------------------------- /kattis/tajna/solution.py: -------------------------------------------------------------------------------- 1 | s = input().strip() 2 | n = len(s) 3 | 4 | h, w = 0, 0 5 | for i in range(n-1, 0, -1): 6 | h = i 7 | w = (n//h) 8 | if h > w: 9 | continue 10 | if h*w == n: 11 | break 12 | 13 | t = [[] for _ in range(h)] 14 | k = 0 15 | for i in range(w): 16 | for j in range(h): 17 | t[j].append(s[k]) 18 | k += 1 19 | for u in t: 20 | print(''.join(u), end='') 21 | -------------------------------------------------------------------------------- /kattis/zoo/solution.py: -------------------------------------------------------------------------------- 1 | listnum = 1 2 | while True: 3 | d = {} 4 | n = int(input()) 5 | if n == 0: 6 | break 7 | for _ in range(n): 8 | s = input().split()[-1].lower() 9 | if s in d: 10 | d[s] += 1 11 | else: 12 | d[s] = 1 13 | print('List {}:'.format(listnum)) 14 | for a in sorted(d.keys()): 15 | print(a, '|', d[a]) 16 | listnum += 1 17 | -------------------------------------------------------------------------------- /uva/00673/notes.md: -------------------------------------------------------------------------------- 1 | A stack problem. Whenever you come across an opening bracket while iterating the sequence, add it to the stack. Whenever you come across a closing bracket, check the top of the stack, and if it matches, continue on to the next bracket in the sequence. If it does not match, or if there is nothing in the stack, or if the stack still contains items after the sequence has been iterated, then the sequence is not valid. 2 | -------------------------------------------------------------------------------- /hackerrank/challenges/staircase/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n; 5 | scanf("%d", &n); 6 | 7 | int i; 8 | for (i = 0; i < n; ++i) { 9 | int j = 0; 10 | for (j = 0; j < (n-i)-1; ++j) { 11 | printf(" "); 12 | } 13 | for (; j < n; ++j) { 14 | printf("#"); 15 | } 16 | printf("\n"); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /kattis/compoundwords/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from functools import reduce 3 | 4 | words = reduce(list.__add__, 5 | [line.strip().split() for line in sys.stdin.readlines()]) 6 | 7 | added = set() 8 | for i, a in enumerate(words): 9 | for j, b in enumerate(words): 10 | if i == j: 11 | continue 12 | word = a + b 13 | if word in added: 14 | continue 15 | added.add(word) 16 | print('\n'.join(sorted(added))) 17 | 18 | -------------------------------------------------------------------------------- /kattis/doorman/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | v = list(map(lambda x: 1 if x == 'M' else -1, input())) 3 | 4 | curr = 0 5 | ans = 0 6 | while ans < len(v)-1: 7 | if abs(curr + v[ans+1]) < abs(curr + v[ans]): 8 | v[ans], v[ans+1] = v[ans+1], v[ans] 9 | if abs(curr + v[ans]) > n: 10 | break 11 | curr += v[ans] 12 | ans += 1 13 | if abs(curr + v[ans]) <= n: 14 | ans += 1 15 | 16 | print(ans) 17 | -------------------------------------------------------------------------------- /kattis/eventplanning/solution.py: -------------------------------------------------------------------------------- 1 | n, budget, hotels, weeks = map(int, input().split()) 2 | best = None 3 | for _ in range(hotels): 4 | c = int(input()) 5 | for w in map(int, input().split()): 6 | if w >= n: 7 | b = c*n 8 | if not best or b < best: 9 | best = b 10 | break 11 | if not best or best > budget: 12 | print('stay home') 13 | else: 14 | print(best) 15 | -------------------------------------------------------------------------------- /kattis/grandpabernie/solution.py: -------------------------------------------------------------------------------- 1 | num_trips = int(input()) 2 | 3 | d = {} 4 | for i in range(num_trips): 5 | dest, year = input().split() 6 | if dest in d: 7 | d[dest].append(int(year)) 8 | else: 9 | d[dest] = [int(year)] 10 | 11 | for k in d: 12 | d[k] = sorted(d[k]) 13 | 14 | num_q = int(input()) 15 | for i in range(num_q): 16 | dest, t = input().split() 17 | print(d[dest][int(t)-1]) 18 | -------------------------------------------------------------------------------- /kattis/luhnchecksum/solution.py: -------------------------------------------------------------------------------- 1 | def check(s): 2 | tot = 0 3 | for i in range(1, len(s) + 1): 4 | v = s[len(s) - i] 5 | if i % 2 == 0: 6 | v *= 2 7 | if v >= 10: 8 | v = (v//10) + (v%10) 9 | tot += v 10 | return tot % 10 == 0 11 | 12 | t = int(input()) 13 | for _ in range(t): 14 | s = [int(c) for c in input()] 15 | print(['FAIL', 'PASS'][check(s)]) 16 | -------------------------------------------------------------------------------- /kattis/server/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, t; 5 | scanf("%d %d", &n, &t); 6 | int i; 7 | int ans = 0; 8 | for (i = 0; i < n; ++i) { 9 | int m; 10 | scanf("%d", &m); 11 | if (t >= m) { 12 | t -= m; 13 | } else { 14 | break; 15 | } 16 | ++ans; 17 | } 18 | printf("%d\n", ans); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /hacker.org/021/solution.md: -------------------------------------------------------------------------------- 1 | By the title, I understood it was a substitution cypher. 2 | 3 | These can be solved by analysing letter frequencies and 4 | comparing against dictionary words. 5 | 6 | The tool at https://www.guballa.de/substitution-solver 7 | did this impeccably. 8 | 9 | It told me the mapping was: 10 | abcdefghijklmnopqrstuvwxyz This clear text ... 11 | iqpkylndojxsgtvebwahmucfzr ... maps to this cipher text 12 | -------------------------------------------------------------------------------- /kattis/bottledup/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long int n, a, b; 5 | scanf("%ld %ld %ld", &n, &a, &b); 6 | long int i = n/a; 7 | while (1) { 8 | long int diff = n - (i*a); 9 | if (diff % b == 0) { 10 | printf("%ld %ld\n", i, diff / b); 11 | return 0; 12 | } else if (i == 0) { 13 | printf("Impossible"); 14 | return 0; 15 | } else { 16 | --i; 17 | } 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /kattis/conformity/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | import sys 3 | from collections import defaultdict 4 | frosh = defaultdict(int) 5 | 6 | n = int(sys.stdin.readline()) 7 | for line in sys.stdin.readlines(): 8 | line = tuple(sorted(line.strip().split())) 9 | frosh[line] += 1 10 | maxn = max(frosh[x] for x in frosh) 11 | print(sum(frosh[y] for y in frosh if frosh[y] == maxn)) 12 | 13 | main() 14 | -------------------------------------------------------------------------------- /kattis/hangman/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | word = set(input()) 3 | alph = input() 4 | 5 | n = 0 6 | for c in alph: 7 | if c in word: 8 | word.remove(c) 9 | if len(word) == 0: 10 | print('WIN') 11 | return 12 | else: 13 | n += 1 14 | if n >= 10: 15 | print('LOSE') 16 | return 17 | 18 | main() 19 | -------------------------------------------------------------------------------- /kattis/judging/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | d = {} 3 | for i in range(n): 4 | s = input() 5 | if s in d: 6 | d[s] += 1 7 | else: 8 | d[s] = 1 9 | e = {} 10 | for i in range(n): 11 | s = input() 12 | if s in e: 13 | e[s] += 1 14 | else: 15 | e[s] = 1 16 | 17 | ans = 0 18 | for k in d: 19 | if k not in e: 20 | continue 21 | ans += min(d[k], e[k]) 22 | print(ans) 23 | -------------------------------------------------------------------------------- /kattis/luckynumber/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | 4 | nums = [i for i in range(1, 10)] 5 | for i in range(2, n+1): 6 | newnums = [] 7 | for num in nums: 8 | for j in range(10): 9 | newnum = (num*10)+j 10 | if newnum % i == 0: 11 | newnums.append(newnum) 12 | nums = newnums 13 | print(len(nums)) 14 | 15 | main() 16 | -------------------------------------------------------------------------------- /kattis/npuzzle/solution.py: -------------------------------------------------------------------------------- 1 | des = ['ABCD', 'EFGH', 'IJKL', 'MNO.'] 2 | d = {} 3 | for i in range(4): 4 | for j in range(4): 5 | c = des[i][j] 6 | d[c] = (i, j) 7 | 8 | a = 0 9 | for i in range(4): 10 | s = input() 11 | for j in range(4): 12 | c = s[j] 13 | if c == '.': 14 | continue 15 | e = d[c] 16 | b = abs(i-e[0]) + abs(j-e[1]) 17 | a += b 18 | print(a) 19 | -------------------------------------------------------------------------------- /kattis/shatteredcake/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long int n, w, l; 5 | long long int tot = 0; 6 | long long int i; 7 | scanf("%lld\n", &w); 8 | scanf("%lld\n", &n); 9 | long long int a, b; 10 | for (i = 0; i < n; ++i) { 11 | scanf("%lld %lld\n", &a, &b); 12 | tot += a*b; 13 | } 14 | l = tot/w; 15 | printf("%lld\n", l); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /kattis/pseudoprime/solution.py: -------------------------------------------------------------------------------- 1 | def is_prime(n): 2 | for i in range(2, int(n**0.5 + 1)): 3 | if n % i == 0: 4 | return False 5 | return True 6 | 7 | while True: 8 | p, a = map(int, input().split()) 9 | if a == p == 0: 10 | break 11 | print(a**p, (a**p)%p, a%p) 12 | r = pow(a, p, p) 13 | if r == a and not is_prime(p): 14 | print('yes') 15 | else: 16 | print('no') 17 | -------------------------------------------------------------------------------- /kattis/reducedidnumbers/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | nums = list(map(int, (input() for _ in range(n)))) 3 | 4 | m = len(nums) 5 | while True: 6 | found = True 7 | rems = set() 8 | for num in nums: 9 | rem = num % m 10 | if rem in rems: 11 | found = False 12 | break 13 | rems.add(rem) 14 | 15 | if found: 16 | print(m) 17 | break 18 | 19 | m += 1 20 | 21 | -------------------------------------------------------------------------------- /kattis/schoolspirit/solution.py: -------------------------------------------------------------------------------- 1 | def score_without(scores, i, n): 2 | others = [scores[j] for j in range(n) if j != i] 3 | score = 0.2*sum(others[j]*(0.8**j) for j in range(len(others))) 4 | return score 5 | 6 | def main(): 7 | n = int(input()) 8 | scores = [int(input()) for _ in range(n)] 9 | print(score_without(scores, -100, n)) 10 | print(sum(score_without(scores, i, n) for i in range(n))/n) 11 | 12 | main() 13 | -------------------------------------------------------------------------------- /kattis/taisformula/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | tot = 0 4 | prevx, prevy = map(float, input().split()) 5 | for i in range(1, n): 6 | x, y = map(float, input().split()) 7 | tot += min(y, prevy)*(x-prevx) 8 | tot += 0.5*(x-prevx)*(max(y, prevy)-min(y, prevy)) 9 | prevx = x 10 | prevy = y 11 | print(tot/1000) 12 | 13 | if __name__ == '__main__': 14 | main() 15 | -------------------------------------------------------------------------------- /kattis/2048/notes.md: -------------------------------------------------------------------------------- 1 | A painful simulation problem. 2 | 3 | I take each direction separately, as I couldn't see a clean way to generalise it. 4 | 5 | Whenever two numbers are merged, they cannot be merged again. I put the new number's index in a set, and don't let any merges involve a number in this set. If the number moves into the space of a zero, I remove the old index from the set and add a new one. I continue doing this until no more changes occur. 6 | -------------------------------------------------------------------------------- /kattis/freefood/solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int n; 8 | cin >> n; 9 | unordered_set m; 10 | for (int i = 0; i < n; ++i) { 11 | int a, b; 12 | cin >> a >> b; 13 | for (int j = a; j <= b; ++j) { 14 | m.emplace(j); 15 | } 16 | } 17 | 18 | cout << m.size() << "\n"; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /kattis/peasoup/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | found = False 4 | for _ in range(n): 5 | k = int(input()) 6 | name = input() 7 | meals = set(input() for _ in range(k)) 8 | if 'pea soup' in meals and 'pancakes' in meals: 9 | found = True 10 | print(name) 11 | break 12 | if not found: 13 | print('Anywhere is fine I guess') 14 | 15 | main() 16 | -------------------------------------------------------------------------------- /kattis/pokerhand/solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | map m; 8 | 9 | for (int i = 0; i < 5; ++i) { 10 | char a[2]; 11 | cin >> a; 12 | ++m[a[0]]; 13 | } 14 | 15 | int max = 0; 16 | for (auto const &x: m) { 17 | if (x.second > max) max = x.second; 18 | } 19 | cout << max << "\n"; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /kattis/rotatecut/notes.md: -------------------------------------------------------------------------------- 1 | Perform the given simulation, flipping and cutting the string, with the following additions: 2 | 3 | - If the change would do nothing (ie. `len(s)//4 == 0`), then quit, as no more changes can be made (and the input N is up to 10^9, so you need this speedup). 4 | - Keep track of the orientation of the string as you flip, as at the end if it is upside-down (ie. an odd number of flips), you'll need to re-flip it the right way up. 5 | -------------------------------------------------------------------------------- /kattis/shortestpath3/notes.md: -------------------------------------------------------------------------------- 1 | Bellman-Ford is the solution here. 2 | 3 | There is a small hiccup in that instead of quitting when you find a negative cycle, you just need to mark all nodes in that as -INFINITY distance. You can just keep looping and checking if any distance is smaller. If no such distance is, you're done. Otherwise, mark it as -INFINITY, and don't check it again. 4 | 5 | Solution is too slow in python3, but in python2 it runs in 0.64s. 6 | -------------------------------------------------------------------------------- /kattis/simon/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | char* simon = "simon says"; 6 | int n; 7 | scanf("%d\n", &n); 8 | int i; 9 | for (i = 0; i < n; ++i) { 10 | char c[1001]; 11 | fgets(c, 1001, stdin); 12 | if (strstr(c, simon) == c) { 13 | printf("%s\n", &c[11]); 14 | } else { 15 | printf("\n"); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /advent_of_code/2020/10/part2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | nums = list(sorted(int(s) for s in sys.stdin.readlines())) 3 | start = 0 4 | target = nums[-1] + 3 5 | memo = [0]*(target+1) 6 | memo[0] = 1 7 | for n in nums: 8 | prefixes = 0 9 | for i in range(1, 4): 10 | if n - i >= 0: 11 | prefixes += memo[n-i] 12 | print('ways to get to {}: {}'.format(n, prefixes)) 13 | memo[n] += prefixes 14 | print(sum(memo[target-3:target+1])) 15 | -------------------------------------------------------------------------------- /advent_of_code/2021/07/part2.py: -------------------------------------------------------------------------------- 1 | def step_cost(d): 2 | # Sum of d entries of linearly increasing arithmetic series from 1 to d 3 | return d*(1+d)//2 4 | 5 | def calc_cost(nums, d): 6 | return sum(step_cost(abs(n-d)) for n in nums) 7 | 8 | def main(): 9 | nums = list(map(int, input().split(','))) 10 | lo = min(nums) 11 | hi = max(nums) 12 | 13 | print(min(calc_cost(nums, d) for d in range(lo, hi+1))) 14 | 15 | main() 16 | -------------------------------------------------------------------------------- /codeforces/952/b/solution.py: -------------------------------------------------------------------------------- 1 | good = set(['great', 'great!', "don't think so", 'not bad', 'cool']) 2 | grumpy = set(['are you serious', 'are you serious?', 'go die in a hole', 'worse', 'terrible', 'no way', "don't even"]) 3 | 4 | for i in range(10): 5 | print(i) 6 | ans = input().lower().strip() 7 | if ans in good: 8 | print('normal') 9 | break 10 | elif ans in grumpy: 11 | print('grumpy') 12 | break 13 | -------------------------------------------------------------------------------- /kattis/anothercandies/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int t; 5 | scanf("%d", &t); 6 | int ct; 7 | for (ct = 0; ct < t; ct ++) { 8 | int n; 9 | scanf("%d", &n); 10 | unsigned long long c = 0; 11 | int i; 12 | for (i = 0; i < n; i ++) { 13 | unsigned long long d; 14 | scanf("%llu", &d); 15 | c += (d%n); 16 | c %= n; 17 | } 18 | if (c == 0) printf("YES\n"); 19 | else printf ("NO\n"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /kattis/barshelf/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | import sys 3 | read = sys.stdin.readline 4 | n = int(read()) 5 | 6 | ans = 0 7 | 8 | nums = list(map(int, read().split())) 9 | 10 | for i in range(n-2): 11 | for j in range(i+1, n-1): 12 | for k in range(j+1, n): 13 | if nums[i] >= 2*nums[j] and nums[j] >= 2*nums[k]: 14 | ans += 1 15 | 16 | print(ans) 17 | 18 | main() 19 | -------------------------------------------------------------------------------- /kattis/pervasiveheartmonitor/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | digs = set(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']) 4 | 5 | for line in sys.stdin.readlines(): 6 | name = [] 7 | tot = 0 8 | n = 0 9 | for word in line.split(): 10 | if word[0] in digs: 11 | tot += float(word) 12 | n += 1 13 | else: 14 | name.append(word) 15 | print('{:.4f}'.format(tot/n), ' '.join(name)) 16 | -------------------------------------------------------------------------------- /kattis/simplification/notes.md: -------------------------------------------------------------------------------- 1 | A geometry problem. Can be solved with a priority queue, keyed with the area of each triangle (and secondary key = the index of the middle point of the triangle). I did the comparisons in reverse here so the priority queue would have smallest entry first, instead of largest as is normal. I could have made it easier by using the triangle area times 2, and avoided floating point comparisons and conversions. Runs in 0.1s in C++. 2 | -------------------------------------------------------------------------------- /project_euler/018/triangle.txt: -------------------------------------------------------------------------------- 1 | 75 2 | 95 64 3 | 17 47 82 4 | 18 35 87 10 5 | 20 04 82 47 65 6 | 19 01 23 75 03 34 7 | 88 02 77 73 07 63 67 8 | 99 65 04 28 06 16 70 92 9 | 41 41 26 56 83 40 80 70 33 10 | 41 48 72 33 47 32 37 16 94 29 11 | 53 71 44 65 25 43 91 52 97 51 14 12 | 70 11 33 28 77 73 17 78 39 68 17 57 13 | 91 71 52 38 17 14 91 43 58 50 27 29 48 14 | 63 66 04 68 89 53 67 30 73 16 69 87 40 31 15 | 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 16 | -------------------------------------------------------------------------------- /advent_of_code/2019/04/part1.py: -------------------------------------------------------------------------------- 1 | def valid(num): 2 | num_str = str(num) 3 | double = False 4 | for i in range(len(num_str)-1): 5 | double = double or (num_str[i] == num_str[i+1]) 6 | if ord(num_str[i]) > ord(num_str[i+1]): 7 | return False 8 | return double 9 | 10 | 11 | def main(): 12 | lo, hi = map(int, input().split('-')) 13 | print(sum(1 for num in range(lo, hi+1) if valid(num))) 14 | 15 | main() 16 | -------------------------------------------------------------------------------- /hackerrank/challenges/countingsort1/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long int count[100]; 5 | int i; 6 | for (i = 0; i < 100; ++i) { 7 | count[i] = 0; 8 | } 9 | int n; 10 | scanf("%d", &n); 11 | for (i = 0; i < n; ++i) { 12 | int m; 13 | scanf("%d", &m); 14 | ++count[m]; 15 | } 16 | for (i = 0; i < 100; ++i) { 17 | printf("%ld ", count[i]); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /kattis/bing/solution.py: -------------------------------------------------------------------------------- 1 | from sys import stdin 2 | 3 | def main(): 4 | rl = stdin.readline 5 | d = {} 6 | n = int(rl()) 7 | for _ in range(n): 8 | word = rl().strip() 9 | e = d 10 | for c in word: 11 | if c in e: 12 | e[c][0] += 1 13 | else: 14 | e[c] = [0, {}] 15 | ans = e[c][0] 16 | e = e[c][1] 17 | print(ans) 18 | 19 | main() 20 | -------------------------------------------------------------------------------- /kattis/conundrum/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | char c[305]; 6 | scanf("%s", c); 7 | int n = strlen(c); 8 | int i; 9 | int ans = 0; 10 | for (i = 0; i < n; ++i) { 11 | if (i % 3 == 0 && c[i] != 'P') ++ans; 12 | if (i % 3 == 1 && c[i] != 'E') ++ans; 13 | if (i % 3 == 2 && c[i] != 'R') ++ans; 14 | } 15 | printf("%d\n", ans); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /kattis/incognito/solution.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | def main(): 4 | n = int(input()) 5 | for _ in range(n): 6 | m = int(input()) 7 | d = defaultdict(int) 8 | for _ in range(m): 9 | a, b = input().split() 10 | d[b] += 1 11 | tot = 1 12 | for e in d: 13 | tot *= d[e] + 1 14 | print(tot-1) 15 | 16 | if __name__ == '__main__': 17 | main() 18 | -------------------------------------------------------------------------------- /kattis/isithalloween/solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char d[3]; 5 | char a, b, c; 6 | int n; 7 | scanf("%s %d", d, &n); 8 | a = d[0]; 9 | b = d[1]; 10 | c = d[2]; 11 | if ((a == 'D' && b == 'E' && c == 'C' && n == 25) || 12 | (a == 'O' && b == 'C' && c == 'T' && n == 31)) { 13 | printf("yup\n"); 14 | return 0; 15 | } 16 | printf("nope\n"); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /advent_of_code/2017/02/part2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | d = [] 4 | for line in sys.stdin.readlines(): 5 | v = list(map(int, line.strip().split())) 6 | ok = False 7 | for i in range(0, len(v)-1): 8 | for j in range(i+1, len(v)): 9 | if v[i] % v[j] == 0: 10 | d.append(v[i]//v[j]) 11 | ok = True 12 | break 13 | elif v[j] % v[i] == 0: 14 | d.append(v[j]//v[i]) 15 | ok = True 16 | break 17 | if ok: 18 | break 19 | print(sum(d)) 20 | -------------------------------------------------------------------------------- /kattis/blockgame2/solution.py: -------------------------------------------------------------------------------- 1 | 'Returns True if win, False if lose' 2 | def bop(a, b): 3 | if a == 1: 4 | return True 5 | if b % a == 0: 6 | return True 7 | if b > 2*a: 8 | return True 9 | return not bop(b-a, a) 10 | 11 | 12 | def main(): 13 | a, b = map(int, input().split()) 14 | z = bop(min(a, b), max(a, b)) 15 | if z: 16 | print('win') 17 | else: 18 | print('lose') 19 | 20 | main() 21 | -------------------------------------------------------------------------------- /kattis/bus/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int ntests; 5 | scanf("%d", &ntests); 6 | int test; 7 | for (test = 0; test < ntests; ++test) { 8 | int n; 9 | scanf("%d", &n); 10 | long double b = 0; 11 | while (n > 0) { 12 | b += 0.5; 13 | b *= 2; 14 | --n; 15 | } 16 | printf("%lld\n", (long long int) b); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /kattis/combinationlock/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a, b, c, d; 5 | while (1) { 6 | scanf("%d %d %d %d", &a, &b, &c, &d); 7 | if (a == 0 && b == 0 && c == 0 && d == 0) break; 8 | a *= 9; 9 | b *= 9; 10 | c *= 9; 11 | d *= 9; 12 | long int ans = 720; // 2 full turns 13 | ans += (a-b+360)%360; 14 | ans += 360; 15 | ans += (c-b+360)%360; 16 | ans += (c-d+360)%360; 17 | printf("%ld\n", ans); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /project_euler/README.md: -------------------------------------------------------------------------------- 1 | I'm adding my project euler solutions here to get them all organised in one 2 | place. Some of the problems I solved in a REPL however. 3 | 4 | My project euler profile: 5 | 6 | ![ProjectEuler Profile](http://projecteuler.net/profile/iandioch.png) 7 | 8 | My friend key: 9 | `606734_ef9b41477e3c782264e98a3014b920dc` 10 | 11 | Following the updated instructions, I removed all solutions > ID 100, and put 12 | them in a private repo. 13 | -------------------------------------------------------------------------------- /advent_of_code/2020/06/part1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def count_qs(group): 4 | qs = set() 5 | for line in group: 6 | qs.update(line) 7 | return len(qs) 8 | 9 | 10 | ans = 0 11 | group = [] 12 | for line in sys.stdin.readlines(): 13 | line = line.strip() 14 | 15 | if len(line) == 0: 16 | ans += count_qs(group) 17 | group = [] 18 | else: 19 | group.append(line) 20 | 21 | ans += count_qs(group) 22 | print(ans) 23 | -------------------------------------------------------------------------------- /advent_of_code/2025/02/part1.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | ans = 0 3 | id_ranges = input().split(',') 4 | for id_range in id_ranges: 5 | p, q = id_range.split('-') 6 | p = int(p) 7 | q = int(q) 8 | for i in range(p, q+1): 9 | s = str(i) 10 | if len(s) % 2 == 1: 11 | continue 12 | if s[:len(s)//2] == s[len(s)//2:]: 13 | ans += i 14 | print(ans) 15 | 16 | main() 17 | -------------------------------------------------------------------------------- /kattis/harshadnumbers/solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool is_harshad(long long int n) { 6 | long long int digsum = 0; 7 | long long int m = n; 8 | while (m) { 9 | digsum += m%10; 10 | m/=10; 11 | } 12 | return !(n % digsum); 13 | } 14 | 15 | int main() { 16 | long long int n; 17 | cin >> n; 18 | while (!is_harshad(n)) ++n; 19 | cout << n << "\n"; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /kattis/juryjeopardy/notes.md: -------------------------------------------------------------------------------- 1 | Perform the simulation as given, walking the character through the maze. Keep all points visited in a set. After the route has finished, the height of the maze is `(maxy - miny + 1)`, with an extra `+ 2` for the top and bottom walls. The width is `(maxx - minx + 1)` with an extra `+ 1` for the right wall (as the left wall will already be in the path). 2 | 3 | Now just output `#` for everywhere not in the path, or `.` for everywhere in the path. 4 | -------------------------------------------------------------------------------- /kattis/mosquito/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | for line in sys.stdin.readlines(): 3 | *state, eggs, surv_larv, surv_pup, num_weeks = map(int, line.split()) 4 | for week in range(num_weeks): 5 | num_mosq, num_pup, num_larv = state 6 | 7 | new_pup = num_larv // surv_larv 8 | new_mosq = num_pup // surv_pup 9 | new_larv = num_mosq * eggs 10 | 11 | state = (new_mosq, new_pup, new_larv) 12 | print(state[0]) 13 | -------------------------------------------------------------------------------- /kattis/dasblinkenlights/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | p, q, s = map(int, input().split()) 3 | 4 | pset = set() 5 | qset = set() 6 | a = p 7 | while a <= s: 8 | pset.add(a) 9 | a += p 10 | a = q 11 | while a <= s: 12 | qset.add(a) 13 | a += q 14 | ans = (pset & qset) 15 | if len(ans): 16 | print('yes') 17 | else: 18 | print('no') 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /kattis/growlinggears/notes.md: -------------------------------------------------------------------------------- 1 | I initially thought of binary searching the curve to find the maximum. However, instead we can use calculus. Go maths! 2 | 3 | Given `y = -aX^2 + bX + c` (`X = R`, `Y = T`, if you use the variable names from the question), `dy/dx = -2aX + b = 0`. We can go from here to `X = b/2a`. We now know we'll reach the max at this `X` value, so we can sub it in to the original equation. We can simply do this for all inputs and output the one with the maximum. 4 | -------------------------------------------------------------------------------- /kattis/permutationencryption/solution.py: -------------------------------------------------------------------------------- 1 | while True: 2 | s = input() 3 | if s == '0': 4 | break 5 | keylen, *key = map(int, s.split()) 6 | message = input() 7 | output = '' 8 | while len(message) % keylen != 0: 9 | message += ' ' 10 | i = 0 11 | while i < len(message): 12 | t = message[i:i+keylen] 13 | output += ''.join([t[j-1] for j in key]) 14 | i += keylen 15 | print("'{}'".format(output)) 16 | -------------------------------------------------------------------------------- /kattis/quickestimate/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int n; 6 | scanf("%d\n", &n); 7 | int i; 8 | for (i = 0; i < n; ++i) { 9 | char c = ' '; 10 | long int m = 0; 11 | while (1) { 12 | scanf("%c", &c); 13 | if (c == '\n') { 14 | break; 15 | } 16 | ++m; 17 | } 18 | printf("%ld\n", m); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /kattis/rollcall/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | names = [line.split() for line in sys.stdin.readlines()] 4 | 5 | seen = set() 6 | repeat = set() 7 | 8 | for first, last in names: 9 | if first in seen: 10 | repeat.add(first) 11 | seen.add(first) 12 | 13 | names.sort(key=lambda x: x[0]) 14 | names.sort(key=lambda x: x[1]) 15 | for first, last in names: 16 | if first in repeat: 17 | print(first, last) 18 | else: 19 | print(first) 20 | -------------------------------------------------------------------------------- /kattis/tri/solution.py: -------------------------------------------------------------------------------- 1 | a, b, c = map(int, input().split()) 2 | 3 | ops = { 4 | '+': lambda x, y: x+y, 5 | '-': lambda x, y: x-y, 6 | '/': lambda x, y: x/y, 7 | '*': lambda x, y: x*y, 8 | } 9 | 10 | done = False 11 | 12 | for op in ops: 13 | if ops[op](a, b) == c: 14 | print ("{}{}{}={}".format(a, op, b, c)) 15 | done = True 16 | elif ops[op](b, c) == a: 17 | print ("{}={}{}{}".format(a, b, op, c)) 18 | done = True 19 | if done: 20 | break 21 | 22 | 23 | -------------------------------------------------------------------------------- /kattis/unlockpattern/solution.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | def dist(x1, y1, x2, y2): 3 | import math 4 | return math.sqrt((y2-y1)**2 + (x2-x1)**2) 5 | grid = [list(map(int, input().split())) for _ in range(3)] 6 | pos = {} 7 | for y in range(3): 8 | for x in range(3): 9 | pos[grid[y][x]] = (x, y) 10 | ans = 0 11 | for i in range(2, 10): 12 | ans += dist(*pos[i-1], *pos[i]) 13 | print(ans) 14 | 15 | main() 16 | -------------------------------------------------------------------------------- /project_euler/003/Solution.scala: -------------------------------------------------------------------------------- 1 | object Solution{ 2 | 3 | def max(a : Long, b : Long) : Long = { 4 | return (if (a > b) a else b) 5 | } 6 | 7 | def primeFact(n : Long) : Long = { 8 | var p = 2L 9 | while(p <= n) { 10 | if (n % p == 0) { 11 | println(p) 12 | return max(p, primeFact(n/p)) 13 | } 14 | p += 1 15 | } 16 | return 1 17 | } 18 | 19 | def main(args : Array[String]){ 20 | println(primeFact(600851475143L)) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /kattis/abc/solution.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int compare (const void * a, const void * b) { 5 | return (*(int*)a - *(int*)b); 6 | } 7 | 8 | int main() { 9 | int n[3]; 10 | scanf("%d %d %d", &n[0], &n[1], &n[2]); 11 | qsort(n, 3, sizeof(int), compare); 12 | char c[5]; 13 | scanf("%s", c); 14 | int i; 15 | for (i = 0; i < 3; ++i) { 16 | printf("%d ", n[c[i]-'A']); 17 | } 18 | printf("\n"); 19 | } 20 | -------------------------------------------------------------------------------- /kattis/bubbletea/solution.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | tea_prices = list(map(int, input().split())) 3 | input() 4 | topping_prices = list(map(int, input().split())) 5 | min_price = None 6 | for i in range(n): 7 | k, *p = list(map(int, input().split())) 8 | for q in p: 9 | price = tea_prices[i] + topping_prices[q-1] 10 | if not min_price or min_price > price: 11 | min_price = price 12 | num = int(input())//min_price 13 | print(max(num-1, 0)) 14 | -------------------------------------------------------------------------------- /kattis/iboard/solution.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def trapped(s): 4 | one, zero = True, True # ok to leave 5 | for c in s: 6 | b = bin(ord(c))[2:].zfill(7) 7 | for v in b[::-1]: 8 | if v == '1': 9 | one = not one 10 | else: 11 | zero = not zero 12 | return 'free' if (zero and one) else 'trapped' 13 | 14 | for line in sys.stdin.readlines(): 15 | line = line[:-1] 16 | print(trapped(line)) 17 | --------------------------------------------------------------------------------