├── .gitignore ├── AdventOfCode-2015 ├── 01.hs ├── 01.input ├── 02.hs ├── 02.input ├── 03.hs ├── 03.input ├── 04.hs ├── 05.hs ├── 05.input ├── 06.hs ├── 06.input ├── 07.hs ├── 07.input ├── 08.hs ├── 08.input ├── 09.hs ├── 09.input ├── 10.hs ├── 12.hs ├── 12.input ├── 13.hs ├── 13.input ├── 14.hs └── 14.input ├── BasicConcurrentServer └── BasicServer.hs ├── Boggle ├── Boggle.hs ├── README ├── testboard1.in ├── testboard2.in ├── testdictionary1.in └── testdictionary2.in ├── CodeEval ├── a_pile_of_bricks.hs ├── a_pile_of_bricks.in ├── age_distribution.hs ├── age_distribution.in ├── armstrong_numbers.hs ├── balanced_smileys.hs ├── balanced_smileys.in ├── beautiful_strings.hs ├── beautiful_strings.in ├── beautiful_strings.out ├── bit_positions.hs ├── bit_positions.in ├── calculate_distance.hs ├── calculate_distance.in ├── cash_register.hs ├── cash_register.in ├── chain_inspection.hs ├── chain_inspection.in ├── city_blocks_flyer.hs ├── city_blocks_flyer.in ├── climbing_stairs.hs ├── climbing_stairs.in ├── closest_pair.hs ├── closest_pair.in ├── commuting_engineer.hs ├── commuting_engineer.in ├── compressed_sequence.hs ├── compressed_sequence.in ├── counting_primes.hs ├── counting_primes.in ├── cracking_eggs.hs ├── da_vyncy.hs ├── da_vyncy.in ├── da_vyncy.out ├── data_recovery.hs ├── data_recovery.in ├── decimal_to_binary.hs ├── decimal_to_binary.in ├── decode_numbers.hs ├── delta_time.hs ├── delta_time.in ├── detecting_cycles.hs ├── detecting_cycles.in ├── digit_statistics.hs ├── digit_statistics.in ├── distinct_subsequences.hs ├── distinct_subsequences.in ├── distinct_triangles.hs ├── endianness.hs ├── even_numbers.hs ├── even_numbers.in ├── fibonacci_series.hs ├── fibonacci_series.in ├── file_size.hs ├── find_a_square.hs ├── find_a_square.in ├── find_a_writer.hs ├── find_a_writer.in ├── find_min.hs ├── find_min.in ├── find_min.out ├── first_non_repeated_character.hs ├── fizz_buzz.hs ├── fizz_buzz.in ├── flavius_josephus.hs ├── flavius_josephus.in ├── jolly_jumpers.hs ├── jolly_jumpers.in ├── juggling_with_zeros.hs ├── juggling_with_zeros.in ├── labyrinth.hs ├── largest_sub_matrix.hs ├── lettercase_percentage_ratio.hs ├── levenshtein_distance.hs ├── levenshtein_distance_dictionary.in ├── longest_lines.hs ├── longest_lines.in ├── longest_word.hs ├── longest_word.in ├── lowercase.hs ├── lowercase.in ├── lowest_common_ancestor.hs ├── lowest_common_ancestor.in ├── lowest_unique_number.hs ├── lowest_unique_number.in ├── message_decoding.hs ├── message_decoding.in ├── minesweeper.hs ├── minesweeper.in ├── minesweeper.out ├── minimum_coins.hs ├── minimum_path_sum.hs ├── minimum_path_sum.in ├── morse_code.hs ├── morse_code.in ├── multiples_of_a_number.hs ├── multiples_of_a_number.in ├── multiplication_tables.hs ├── nice_angles.hs ├── nice_angles.in ├── number_of_ones.hs ├── number_operations.hs ├── number_operations.in ├── number_pairs.hs ├── number_pairs.in ├── odd_numbers.hs ├── overlapping_rectangles.hs ├── overlapping_rectangles.in ├── package_problem.hs ├── package_problem.in ├── palindromic_ranges.hs ├── palindromic_ranges.in ├── panagrams.hs ├── panagrams.in ├── pascals_triangle.hs ├── pascals_triangle.in ├── pass_triangle.hs ├── pass_triangle.in ├── penultimate_word.hs ├── penultimate_word.in ├── point_in_circle.hs ├── point_in_circle.in ├── prefix_expressions.hs ├── prefix_expressions.in ├── prime_palindrome.hs ├── racing_chars.hs ├── remove_characters.hs ├── remove_characters.in ├── repeated_substring.hs ├── repeated_substring.in ├── reverse_and_add.hs ├── reverse_groups.hs ├── reverse_groups.in ├── reverse_words.hs ├── reverse_words.in ├── robot_movements.hs ├── roller_coaster.hs ├── roman_and_arabic.hs ├── set_intersection.hs ├── set_intersection.in ├── simple_calculator.hs ├── simple_calculator.in ├── simple_calculator.out ├── simple_sorting.hs ├── simple_sorting.in ├── skyscrapers.hs ├── skyscrapers.in ├── stack_implementation.hs ├── stack_implementation.in ├── string_list.hs ├── string_list.in ├── string_rotation.hs ├── string_rotation.in ├── string_searching.hs ├── string_searching.in ├── string_substitution.hs ├── string_substitution.in ├── strings_and_arrows.hs ├── strings_and_arrows.in ├── sudoku.hs ├── sum_of_digits.hs ├── sum_of_digits.in ├── sum_of_integers.hs ├── sum_of_integers.in ├── sum_of_integers_from_file.hs ├── sum_of_integers_from_file.in ├── sum_of_primes.hs ├── sum_to_zero.hs ├── sum_to_zero.in ├── swap_case.hs ├── swap_case.in ├── telephone_words.hs ├── telephone_words.in ├── template.hs ├── text_dollar.hs ├── text_dollar.in ├── text_dollar.out ├── text_to_number.hs ├── text_to_number.in ├── trailing_string.hs ├── trailing_string.in ├── type_ahead.hs ├── type_ahead.in ├── ugly_numbers-slow-lowmemory.hs ├── ugly_numbers.hs ├── ugly_numbers.in ├── unique_elements.hs ├── unique_elements.in ├── valid_parentheses.hs ├── valid_parentheses.in ├── where_is_wifi.hs ├── word_search.hs └── word_search.in ├── Fake ├── Bar.hs └── Foo.hs ├── GoogleCodeJam ├── CoinJam │ └── CoinJam.hs └── TheLastWord │ ├── 01.in │ ├── 01.out │ ├── 02.in │ ├── 02.out │ └── TheLastWord.hs ├── HackerRank ├── AreaUnderCurves │ ├── AreaUnderCurves.hs │ └── problem_statement.pdf ├── FibonacciModified │ └── FibonacciModified.hs ├── FractalTrees │ ├── FractalTrees.hs │ └── problem_statement.pdf ├── HugeGCD │ └── HugeGCD.hs ├── IdentifySmithNumbers │ ├── IdentifySmithNumbers.hs │ └── problem_statement.pdf ├── ListsAndGCD │ ├── ListsAndGCD.hs │ ├── problem_statement.pdf │ └── sample01.in ├── MaxMin │ ├── MaxMin.hs │ └── problem_statement.pdf ├── PickingCards │ └── PickingCards.hs ├── SolveMeSecond │ └── SolveMeSecond.hs ├── StringCompression │ ├── StringCompression.hs │ └── problem_statement.pdf ├── SubsetSum │ ├── SubsetSum.hs │ └── problem_statement.pdf └── ValidBST │ ├── ValidBST.hs │ └── problem_statement.pdf ├── Hashing └── Hashing.hs ├── MyParallel └── collatz.hs ├── Nonogram ├── Base.hs ├── Solver.hs └── Tests.hs ├── OpenGL ├── DrawingShapes.hs └── HelloWorld.hs ├── PercolationTheory ├── LICENSE ├── Setup.hs ├── app │ └── Main.hs ├── src │ └── Lib.hs ├── stack.yaml └── test │ └── Spec.hs ├── Poker └── Data.hs ├── README.md ├── Rosalind ├── CONS.hs ├── CONS.in ├── CONS.out ├── DNA.hs ├── DNA.in ├── FIB.hs ├── FIBD.hs ├── GC.hs ├── GC.in ├── GRPH.hs ├── GRPH.in ├── GRPH.out ├── HAMM.hs ├── HAMM.in ├── IEV.hs ├── IPRB.hs ├── LCSM.hs ├── LCSM.in ├── LCSM.out ├── LEXF.hs ├── LEXF.in ├── LEXF.out ├── LEXV.hs ├── LEXV.in ├── LEXV.out ├── LIA.hs ├── MRNA.hs ├── MRNA.in ├── PDST.hs ├── PDST.in ├── PDST.out ├── PERM.hs ├── PERM.out ├── PPER.hs ├── PROB.hs ├── PROB.in ├── PROB.out ├── PROT.hs ├── PROT.in ├── PROT.out ├── PRTM.hs ├── PRTM.in ├── PRTM.out ├── Parsing.hs ├── REVC.hs ├── REVC.in ├── RNA.hs ├── RNA.in ├── SETO.hs ├── SETO.in ├── SETO.out ├── SIGN.hs ├── SIGN.out ├── SSET.hs ├── SUBS.hs ├── SUBS.in ├── Structures.hs ├── TREE.hs ├── TREE.in ├── TRIE.hs ├── TRIE.in ├── TRIE.out └── template.hs ├── Secretary ├── Main.hs └── Strategies.hs ├── SimpleChatServer └── SimpleChatServer.hs ├── StreamingMedian ├── StreamingMedian.hs └── Test.hs ├── TopCoder ├── ABC │ └── ABC.hs ├── AlmostFibonacciKnapSack │ ├── AlmostFibonacciKnapsack.hs │ ├── input.txt │ └── output.txt ├── CuttingJigsawPuzzle │ ├── 1.in │ ├── 1.out │ ├── 2.in │ ├── 2.out │ └── puzzle.hs ├── Frog │ ├── 1.in │ ├── 1.out │ ├── 2.in │ ├── 2.out │ ├── 3.in │ ├── 3.out │ ├── 4.in │ ├── 4.out │ ├── Frog.hs │ ├── Frog_set.hs │ ├── Makefile │ └── makeInput.py ├── SumFullSet │ ├── SumFullSet.hs │ ├── input.txt │ └── output.txt └── WorldPeace │ ├── WorldPeace.hs │ ├── input.00 │ ├── input.01 │ ├── input.02 │ ├── input.03 │ ├── input.04 │ ├── output.00 │ ├── output.01 │ ├── output.02 │ ├── output.03 │ └── output.04 ├── VTY ├── 2048.hs ├── Life.hs ├── Samples │ ├── IntroDialogBox.hs │ ├── Sample1.hs │ ├── Sample2.hs │ ├── Sample3.hs │ └── UserDefinedWidget.hs └── Virus.hs ├── crud-server ├── LICENSE ├── Setup.hs ├── app │ └── Main.hs ├── src │ ├── DeltaSet.hs │ ├── DisjointSets.hs │ └── Lib.hs ├── stack.yaml └── test │ └── Spec.hs ├── hello-world ├── LetWhereScope.hs ├── Simple.hs ├── SimpleImport.hs ├── UserDefinedTypeclass.hs ├── WindowExtrema.hs ├── ackermann.hs ├── aeson_example_0.hs ├── alternatives_to_if.hs ├── art.hs ├── automatic_differentiation.hs ├── caesar_cipher.hs ├── class_exploration_1.hs ├── class_exploration_2.hs ├── clockwise_spiral.hs ├── coin_combos.hs ├── command_line_arguments.hs ├── cs65_hw01.hs ├── data_map.hs ├── dequeue.hs ├── error_handling_0.hs ├── euclid.hs ├── extremeSubarraySums.hs ├── fact.hs ├── factor.hs ├── farey.hs ├── fib.hs ├── flood_fill.hs ├── gadt_example_0.hs ├── golomb.hs ├── helloworld.hs ├── hill.hs ├── imperative_programming_1.hs ├── integer_partitions.hs ├── lagrange_polynomials.hs ├── lazy_state.hs ├── length.hs ├── matrix_multiply.hs ├── mc_integration.hs ├── mirror_bst.hs ├── monad_exploration_1.hs ├── monad_exploration_2.hs ├── nonogram.hs ├── nqueens.hs ├── optimization_test_1.hs ├── pair_partitions.hs ├── poker.hs ├── polynomial.hs ├── powmod.hs ├── primes_simple.hs ├── pythagorean_triples.hs ├── qmc_integration.hs ├── queue.hs ├── quicksort.hs ├── random_numbers.hs ├── range_min_query.hs ├── recursive_descent_parser_0.hs ├── recursive_descent_parser_1.hs ├── rex_hw01.hs ├── roll.hs ├── sequences.hs ├── shortest_anagram_subsequence.hs ├── split_stream.hs ├── stdin.hs ├── sudoku.hs ├── suffix_tree.hs ├── test_module.hs ├── todigits.hs ├── tree_level_sum.hs ├── trie.hs ├── types_vs_data.hs ├── user_defined_types.hs └── word_count.hs ├── parsec ├── parsec_example_0.hs ├── parsec_example_1.hs ├── parsec_example_2.hs ├── parsec_example_3.hs ├── parsec_example_4.hs ├── parsec_example_5.hs ├── parsec_example_6.hs ├── parsec_example_7.hs └── parsec_example_8.hs ├── poker ├── LICENSE ├── README.md ├── Setup.hs ├── app │ ├── Main.hs │ └── RandomHands.hs ├── poker.cabal ├── src │ ├── Lib.hs │ └── Poker │ │ ├── Analysis.hs │ │ └── Data.hs ├── stack.yaml └── test │ └── Spec.hs ├── project-euler ├── LICENSE ├── README.md ├── Setup.hs ├── app │ └── Main.hs ├── doc │ └── Problems │ │ ├── P036.pdf │ │ ├── P072.pdf │ │ └── P073.pdf ├── project-euler.cabal ├── src │ ├── Experimental │ │ ├── 060.hs │ │ ├── 061.hs │ │ ├── 126.hs │ │ ├── 165.hs │ │ ├── 185.hs │ │ ├── 248.hs │ │ └── 259.hs │ ├── Old │ │ ├── 001.hs │ │ ├── 002.hs │ │ ├── 003.hs │ │ ├── 004.hs │ │ ├── 005.hs │ │ ├── 006.hs │ │ ├── 007.hs │ │ ├── 008.hs │ │ ├── 008.in │ │ ├── 009.hs │ │ ├── 010.hs │ │ ├── 011.in │ │ ├── 012.hs │ │ ├── 013.hs │ │ ├── 013.in │ │ ├── 023.hs │ │ ├── 044.hs │ │ ├── 075.hs │ │ ├── 079.hs │ │ ├── 091.hs │ │ ├── 092.hs │ │ ├── 108.hs │ │ ├── 119.hs │ │ ├── 132.hs │ │ ├── 145.hs │ │ ├── 149.hs │ │ ├── 150.hs │ │ ├── 155.hs │ │ ├── 179.hs │ │ ├── 201.hs │ │ ├── 240.hs │ │ ├── 301.hs │ │ └── 347.hs │ ├── Problems │ │ ├── P001.hs │ │ ├── P002.hs │ │ ├── P003.hs │ │ ├── P004.hs │ │ ├── P005.hs │ │ ├── P006.hs │ │ ├── P007.hs │ │ ├── P008.hs │ │ ├── P009.hs │ │ ├── P010.hs │ │ ├── P011.hs │ │ ├── P012.hs │ │ ├── P013.hs │ │ ├── P014.hs │ │ ├── P015.hs │ │ ├── P016.hs │ │ ├── P017.hs │ │ ├── P018.hs │ │ ├── P019.hs │ │ ├── P020.hs │ │ ├── P021.hs │ │ ├── P022.hs │ │ ├── P023.hs │ │ ├── P024.hs │ │ ├── P025.hs │ │ ├── P026.hs │ │ ├── P027.hs │ │ ├── P028.hs │ │ ├── P029.hs │ │ ├── P030.hs │ │ ├── P031.hs │ │ ├── P032.hs │ │ ├── P033.hs │ │ ├── P034.hs │ │ ├── P035.hs │ │ ├── P036.hs │ │ ├── P037.hs │ │ ├── P038.hs │ │ ├── P039.hs │ │ ├── P040.hs │ │ ├── P041.hs │ │ ├── P042.hs │ │ ├── P043.hs │ │ ├── P044.hs │ │ ├── P045.hs │ │ ├── P046.hs │ │ ├── P047.hs │ │ ├── P048.hs │ │ ├── P049.hs │ │ ├── P050.hs │ │ ├── P051.hs │ │ ├── P052.hs │ │ ├── P053.hs │ │ ├── P054.hs │ │ ├── P055.hs │ │ ├── P056.hs │ │ ├── P057.hs │ │ ├── P058.hs │ │ ├── P059.hs │ │ ├── P062.hs │ │ ├── P063.hs │ │ ├── P065.hs │ │ ├── P066.hs │ │ ├── P067.hs │ │ ├── P069.hs │ │ ├── P071.hs │ │ ├── P072.hs │ │ ├── P073.hs │ │ ├── P074.hs │ │ ├── P076.hs │ │ ├── P077.hs │ │ ├── P079.hs │ │ ├── P085.hs │ │ ├── P087.hs │ │ ├── P091.hs │ │ ├── P092.hs │ │ ├── P097.hs │ │ ├── P099.hs │ │ ├── P101.hs │ │ ├── P102.hs │ │ ├── P108.hs │ │ ├── P110.hs │ │ ├── P112.hs │ │ ├── P114.hs │ │ ├── P115.hs │ │ ├── P116.hs │ │ ├── P117.hs │ │ ├── P119.hs │ │ ├── P122.hs │ │ ├── P124.hs │ │ ├── P125.hs │ │ ├── P129.hs │ │ ├── P130.hs │ │ ├── P132.hs │ │ ├── P147.hs │ │ ├── P149.hs │ │ ├── P150.hs │ │ ├── P157.hs │ │ ├── P164.hs │ │ ├── P173.hs │ │ ├── P174.hs │ │ ├── P178.hs │ │ ├── P181.hs │ │ ├── P183.hs │ │ ├── P188.hs │ │ ├── P190.hs │ │ ├── P190.tex │ │ ├── P191.hs │ │ ├── P197.hs │ │ ├── P200.hs │ │ ├── P201.hs │ │ ├── P203.hs │ │ ├── P204.hs │ │ ├── P205.hs │ │ ├── P206.hs │ │ ├── P209.hs │ │ ├── P235.hs │ │ ├── P240.hs │ │ ├── P249.hs │ │ ├── P250.hs │ │ ├── P301.hs │ │ ├── P317.hs │ │ ├── P317.tex │ │ └── P493.hs │ └── Util │ │ ├── List.hs │ │ ├── Math.hs │ │ └── Prime.hs ├── stack.yaml └── tests │ ├── Problems │ ├── P001Test.hs │ ├── P002Test.hs │ ├── P003Test.hs │ ├── P004Test.hs │ ├── P005Test.hs │ ├── P006Test.hs │ ├── P007Test.hs │ ├── P008Test.hs │ ├── P009Test.hs │ ├── P010Test.hs │ ├── P011Test.hs │ ├── P012Test.hs │ ├── P013Test.hs │ ├── P014Test.hs │ ├── P015Test.hs │ ├── P016Test.hs │ ├── P017Test.hs │ ├── P018Test.hs │ ├── P019Test.hs │ ├── P020Test.hs │ ├── P021Test.hs │ ├── P022Test.hs │ ├── P023Test.hs │ ├── P024Test.hs │ ├── P025Test.hs │ ├── P026Test.hs │ ├── P027Test.hs │ ├── P028Test.hs │ ├── P029Test.hs │ ├── P030Test.hs │ ├── P031Test.hs │ ├── P032Test.hs │ ├── P033Test.hs │ ├── P034Test.hs │ ├── P035Test.hs │ ├── P036Test.hs │ ├── P037Test.hs │ ├── P038Test.hs │ ├── P039Test.hs │ ├── P040Test.hs │ ├── P041Test.hs │ ├── P042Test.hs │ ├── P043Test.hs │ ├── P044Test.hs │ ├── P045Test.hs │ ├── P046Test.hs │ ├── P047Test.hs │ ├── P048Test.hs │ ├── P049Test.hs │ ├── P050Test.hs │ ├── P051Test.hs │ ├── P052Test.hs │ ├── P053Test.hs │ ├── P054Test.hs │ ├── P055Test.hs │ ├── P056Test.hs │ ├── P057Test.hs │ ├── P058Test.hs │ ├── P059Test.hs │ ├── P062Test.hs │ ├── P063Test.hs │ ├── P065Test.hs │ ├── P066Test.hs │ ├── P067Test.hs │ ├── P069Test.hs │ ├── P071Test.hs │ ├── P072Test.hs │ ├── P073Test.hs │ ├── P074Test.hs │ ├── P076Test.hs │ ├── P077Test.hs │ ├── P079Test.hs │ ├── P085Test.hs │ ├── P087Test.hs │ ├── P091Test.hs │ ├── P092Test.hs │ ├── P097Test.hs │ ├── P099Test.hs │ ├── P101Test.hs │ ├── P102Test.hs │ ├── P108Test.hs │ ├── P110Test.hs │ ├── P112Test.hs │ ├── P114Test.hs │ ├── P115Test.hs │ ├── P116Test.hs │ ├── P117Test.hs │ ├── P119Test.hs │ ├── P122Test.hs │ ├── P124Test.hs │ ├── P125Test.hs │ ├── P129Test.hs │ ├── P130Test.hs │ ├── P132Test.hs │ ├── P147Test.hs │ ├── P149Test.hs │ ├── P150Test.hs │ ├── P157Test.hs │ ├── P164Test.hs │ ├── P173Test.hs │ ├── P174Test.hs │ ├── P178Test.hs │ ├── P181Test.hs │ ├── P183Test.hs │ ├── P188Test.hs │ ├── P190Test.hs │ ├── P191Test.hs │ ├── P197Test.hs │ ├── P200Test.hs │ ├── P201Test.hs │ ├── P203Test.hs │ ├── P204Test.hs │ ├── P205Test.hs │ ├── P206Test.hs │ ├── P209Test.hs │ ├── P235Test.hs │ ├── P240Test.hs │ ├── P249Test.hs │ ├── P250Test.hs │ ├── P301Test.hs │ ├── P317Test.hs │ └── P493Test.hs │ ├── Tasty.hs │ ├── TestData │ ├── P008.hs │ ├── P011.hs │ ├── P013.hs │ ├── P018.hs │ ├── P022.hs │ ├── P042.hs │ ├── P054.hs │ ├── P059.hs │ ├── P067.hs │ ├── P079.hs │ ├── P099.hs │ └── P102.hs │ └── Util │ └── PrimeTest.hs └── sheep-in-hilly-meadow ├── sheep_in_hilly_meadow.hs ├── sheep_in_hilly_meadow.in ├── sheep_in_hilly_meadow.in.1000x1000 └── sheep_in_hilly_meadow.in.100x100 /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | cabal-dev 3 | *.o 4 | *.hi 5 | *.chi 6 | *.chs.h 7 | .virthualenv 8 | 9 | *~ 10 | *.exe 11 | a.out 12 | 13 | *.aux 14 | *.log 15 | 16 | foo.hs 17 | .*.swp 18 | .idea 19 | *.iml 20 | */.stack-work 21 | -------------------------------------------------------------------------------- /AdventOfCode-2015/01.hs: -------------------------------------------------------------------------------- 1 | -- 01.hs 2 | import Data.Maybe (fromJust) 3 | import Data.List (findIndex) 4 | 5 | main = do 6 | floorChanges <- fmap (map floorChange) (readFile "01.input") 7 | print $ solvePart1 floorChanges 8 | print $ fromJust $ solvePart2 floorChanges 9 | 10 | floorChange :: Char -> Int 11 | floorChange '(' = 1 12 | floorChange ')' = -1 13 | 14 | solvePart1 :: [Int] -> Int 15 | solvePart1 = sum 16 | 17 | solvePart2 :: [Int] -> Maybe Int 18 | solvePart2 inp = 19 | let floors = scanl (+) 0 inp 20 | in findIndex (<0) floors 21 | -------------------------------------------------------------------------------- /AdventOfCode-2015/02.hs: -------------------------------------------------------------------------------- 1 | -- 02.hs 2 | import Text.ParserCombinators.Parsec 3 | main = do 4 | boxes <- fmap (map parseBox . lines) (readFile "02.input") 5 | print $ sum $ map requiredPaper boxes 6 | print $ sum $ map requiredRibbon boxes 7 | 8 | data Box = Box { l :: Int, w :: Int, h :: Int } deriving (Eq) 9 | instance Show Box where 10 | show (Box l w h) = show l ++ "x" ++ show w ++ "x" ++ show h 11 | 12 | requiredPaper :: Box -> Int 13 | requiredPaper (Box l w h) = 14 | let side_areas = [ l*w, w*h, h*l ] 15 | in 2 * sum side_areas + minimum side_areas 16 | 17 | requiredRibbon :: Box -> Int 18 | requiredRibbon (Box l w h) = 19 | let perimeters = [ 2*l + 2*w, 2*w + 2*h, 2*h + 2*l ] 20 | volume = l*w*h 21 | in minimum perimeters + volume 22 | 23 | parseBox :: String -> Box 24 | parseBox inp = case parse pBox "" inp of 25 | Right v -> v 26 | Left _ -> error $ "Could not parse input:" ++ show inp 27 | 28 | pBox = do 29 | [l, w, h] <- sepBy pDimension (char 'x') 30 | return $ Box l w h 31 | 32 | pDimension = read <$> many digit 33 | -------------------------------------------------------------------------------- /AdventOfCode-2015/04.hs: -------------------------------------------------------------------------------- 1 | -- 04.hs 2 | import Data.Hash.MD5 3 | import Data.List (isPrefixOf) 4 | 5 | main = do 6 | let secret = "bgvyzdsv" 7 | print $ head $ goodKeys secret ((replicate 5 '0') `isPrefixOf`) 8 | print $ head $ goodKeys secret ((replicate 6 '0') `isPrefixOf`) 9 | 10 | goodKeys :: String -> (String -> Bool) -> [Int] 11 | goodKeys secret pred = filter good [1..] 12 | where good i = pred $ md5s $ Str (secret ++ show i) 13 | -------------------------------------------------------------------------------- /AdventOfCode-2015/08.hs: -------------------------------------------------------------------------------- 1 | -- 08.hs 2 | import Data.Char (chr, digitToInt) 3 | 4 | main = do 5 | raw_strings <- fmap lines (readFile "08.input") 6 | let raw_length = sum $ map length raw_strings 7 | let parsed_strings = map (parseString . init . tail) raw_strings 8 | let parsed_length = sum $ map length parsed_strings 9 | print $ raw_length - parsed_length 10 | let encoded_strings = map ((\s -> "\"" ++ s ++ "\"") . encodeString) raw_strings 11 | let encoded_length = sum $ map length encoded_strings 12 | print $ encoded_length - raw_length 13 | 14 | parseString :: String -> String 15 | parseString "" = "" 16 | parseString ('\\':'\\':r) = '\\' : parseString r 17 | parseString ('\\':'"':r) = '"' : parseString r 18 | parseString ('\\':'x':a:b:r) = chr (16 * digitToInt a + digitToInt b) : parseString r 19 | parseString (v:str) = v : parseString str 20 | 21 | encodeString :: String -> String 22 | encodeString "" = "" 23 | encodeString ('\\':r) = "\\\\" ++ encodeString r 24 | encodeString ('"':r) = "\\\"" ++ encodeString r 25 | encodeString (v:r) = v : encodeString r 26 | -------------------------------------------------------------------------------- /AdventOfCode-2015/09.input: -------------------------------------------------------------------------------- 1 | Faerun to Norrath = 129 2 | Faerun to Tristram = 58 3 | Faerun to AlphaCentauri = 13 4 | Faerun to Arbre = 24 5 | Faerun to Snowdin = 60 6 | Faerun to Tambi = 71 7 | Faerun to Straylight = 67 8 | Norrath to Tristram = 142 9 | Norrath to AlphaCentauri = 15 10 | Norrath to Arbre = 135 11 | Norrath to Snowdin = 75 12 | Norrath to Tambi = 82 13 | Norrath to Straylight = 54 14 | Tristram to AlphaCentauri = 118 15 | Tristram to Arbre = 122 16 | Tristram to Snowdin = 103 17 | Tristram to Tambi = 49 18 | Tristram to Straylight = 97 19 | AlphaCentauri to Arbre = 116 20 | AlphaCentauri to Snowdin = 12 21 | AlphaCentauri to Tambi = 18 22 | AlphaCentauri to Straylight = 91 23 | Arbre to Snowdin = 129 24 | Arbre to Tambi = 53 25 | Arbre to Straylight = 40 26 | Snowdin to Tambi = 15 27 | Snowdin to Straylight = 99 28 | Tambi to Straylight = 70 29 | -------------------------------------------------------------------------------- /AdventOfCode-2015/10.hs: -------------------------------------------------------------------------------- 1 | -- 10.hs 2 | import Data.List (group) 3 | 4 | main = do 5 | let xs = map length $ iterate lookAndSay "1113222113" 6 | print $ xs !! 40 7 | print $ xs !! 50 8 | 9 | lookAndSay :: String -> String 10 | lookAndSay xs = 11 | concat [ show (length g) ++ [head g] | g <- group xs ] 12 | -------------------------------------------------------------------------------- /AdventOfCode-2015/12.hs: -------------------------------------------------------------------------------- 1 | -- 12.hs 2 | {-# LANGUAGE OverloadedStrings #-} 3 | import Data.Aeson 4 | import qualified Data.ByteString.Lazy as B 5 | import qualified Data.HashMap.Strict as HM 6 | import Data.Scientific 7 | 8 | main = do 9 | inp <- B.getContents 10 | case eitherDecode inp :: Either String Value of 11 | Left err -> putStrLn err 12 | Right v -> do 13 | print $ sumNumbers v 14 | print $ sumNumbers $ dropRedObjects v 15 | 16 | sumNumbers :: Value -> Scientific 17 | sumNumbers (Number x) = x 18 | sumNumbers (Array xs) = sum $ fmap sumNumbers xs 19 | sumNumbers o@(Object xs) = sum $ fmap (sumNumbers . snd) $ HM.toList xs 20 | sumNumbers _ = 0 21 | 22 | dropRedObjects :: Value -> Value 23 | dropRedObjects (Object xs) 24 | | any (== String "red") (HM.elems xs) = Object (HM.empty) 25 | | otherwise = Object $ HM.map dropRedObjects xs 26 | dropRedObjects (Array xs) = Array $ fmap dropRedObjects xs 27 | dropRedObjects v = v 28 | -------------------------------------------------------------------------------- /AdventOfCode-2015/14.input: -------------------------------------------------------------------------------- 1 | Rudolph can fly 22 km/s for 8 seconds, but then must rest for 165 seconds. 2 | Cupid can fly 8 km/s for 17 seconds, but then must rest for 114 seconds. 3 | Prancer can fly 18 km/s for 6 seconds, but then must rest for 103 seconds. 4 | Donner can fly 25 km/s for 6 seconds, but then must rest for 145 seconds. 5 | Dasher can fly 11 km/s for 12 seconds, but then must rest for 125 seconds. 6 | Comet can fly 21 km/s for 6 seconds, but then must rest for 121 seconds. 7 | Blitzen can fly 18 km/s for 3 seconds, but then must rest for 50 seconds. 8 | Vixen can fly 20 km/s for 4 seconds, but then must rest for 75 seconds. 9 | Dancer can fly 7 km/s for 20 seconds, but then must rest for 119 seconds. 10 | -------------------------------------------------------------------------------- /BasicConcurrentServer/BasicServer.hs: -------------------------------------------------------------------------------- 1 | import Network 2 | import Control.Concurrent 3 | import System.IO 4 | 5 | main = withSocketsDo $ do 6 | sock <- listenOn $ PortNumber 5002 7 | loop sock 8 | 9 | loop sock = do 10 | (h,_,_) <- accept sock 11 | forkIO $ body h 12 | loop sock 13 | where 14 | body h = do 15 | hPutStr h msg 16 | hFlush h 17 | hClose h 18 | 19 | msg = "HTTP/1.0 200 OK\nContent-Length: 5\n\nPong!\n" 20 | -------------------------------------------------------------------------------- /Boggle/testboard1.in: -------------------------------------------------------------------------------- 1 | A 2 | BCDEFG 3 | UANTCASTYSWQ 4 | EORNOTOBEKANGARTOB 5 | LUYAGIMMXVRHPJITSOOTHEPZ 6 | -------------------------------------------------------------------------------- /Boggle/testdictionary1.in: -------------------------------------------------------------------------------- 1 | SAD 2 | STING 3 | MOTTO 4 | SADDEST 5 | STINGS 6 | QUANTCAST 7 | FADE 8 | CASTE 9 | CAST 10 | CASTED 11 | CASTLE 12 | BAGS 13 | BAG 14 | GAG 15 | DEFEAT 16 | FADED 17 | DEFACTO 18 | EAST 19 | -------------------------------------------------------------------------------- /CodeEval/a_pile_of_bricks.in: -------------------------------------------------------------------------------- 1 | [4,3] [3,-3]|(1 [10,9,4] [9,4,2]) 2 | [-1,-5] [5,-2]|(1 [4,7,8] [2,9,0]);(2 [0,7,1] [5,9,8]) 3 | [-4,-5] [-5,-3]|(1 [4,8,6] [0,9,2]);(2 [8,-1,3] [0,5,4]) 4 | -------------------------------------------------------------------------------- /CodeEval/age_distribution.in: -------------------------------------------------------------------------------- 1 | 0 2 | 19 3 | -4 4 | 104 5 | 62 6 | 12 7 | 14 8 | -------------------------------------------------------------------------------- /CodeEval/balanced_smileys.in: -------------------------------------------------------------------------------- 1 | :(( 2 | i am sick today (:() 3 | (:) 4 | hacker cup: started :):) 5 | )( 6 | -------------------------------------------------------------------------------- /CodeEval/beautiful_strings.in: -------------------------------------------------------------------------------- 1 | ABbCcc 2 | Good luck in the Facebook Hacker Cup this year! 3 | Ignore punctuation, please :) 4 | Sometimes test cases are hard to make up. 5 | So I just go consult Professor Dalves 6 | -------------------------------------------------------------------------------- /CodeEval/beautiful_strings.out: -------------------------------------------------------------------------------- 1 | 152 2 | 754 3 | 491 4 | 729 5 | 646 6 | -------------------------------------------------------------------------------- /CodeEval/bit_positions.in: -------------------------------------------------------------------------------- 1 | 86,2,3 2 | 125,1,2 3 | -------------------------------------------------------------------------------- /CodeEval/calculate_distance.in: -------------------------------------------------------------------------------- 1 | (25, 4) (1, -6) 2 | (47, 43) (-25, -11) 3 | -------------------------------------------------------------------------------- /CodeEval/cash_register.in: -------------------------------------------------------------------------------- 1 | 15.94;16.00 2 | 17;16 3 | 35;35 4 | 45;50 5 | -------------------------------------------------------------------------------- /CodeEval/chain_inspection.in: -------------------------------------------------------------------------------- 1 | 4-2;BEGIN-3;3-4;2-END 2 | 4-2;BEGIN-3;3-4;2-3 3 | -------------------------------------------------------------------------------- /CodeEval/city_blocks_flyer.in: -------------------------------------------------------------------------------- 1 | (0,2,4,8,10,13,14,18,22,23,24,33,40,42,44,47,49,53,55,63,66,81,87,91) (0,147,220) 2 | (0,1,2,4) (0,1,3,4,5) 3 | (0,1,3,4,6) (0,1,2,4) 4 | -------------------------------------------------------------------------------- /CodeEval/climbing_stairs.hs: -------------------------------------------------------------------------------- 1 | -- climbing_stairs.hs 2 | {- 3 | - You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 4 | - Input sample: 5 | - 6 | - Your program should accept as its first argument a path to a filename. Each line in this file contains a positive integer which is the total number of stairs. e.g. 7 | - 8 | - 10 9 | - 20 10 | - 11 | - Output sample: 12 | - 13 | - Print out the number of ways to climb to the top of the staircase. e.g. 14 | - 15 | - 89 16 | - 10946 17 | -} 18 | 19 | {- 20 | - Fibonacci. This is a hard problem? 21 | -} 22 | 23 | 24 | import System.Environment (getArgs) 25 | 26 | fibs = 1:1:zipWith (+) fibs (tail fibs) 27 | main = do 28 | args <- getArgs 29 | txt <- readFile (head args) 30 | putStr $ solveProblem txt 31 | 32 | solveProblem txt = let inputs = map read $ lines txt 33 | anss = map (fibs !!) inputs 34 | in unlines $ map show anss 35 | -------------------------------------------------------------------------------- /CodeEval/climbing_stairs.in: -------------------------------------------------------------------------------- 1 | 10 2 | 20 3 | -------------------------------------------------------------------------------- /CodeEval/closest_pair.in: -------------------------------------------------------------------------------- 1 | 5 2 | 0 2 3 | 6 67 4 | 43 71 5 | 39 107 6 | 189 140 7 | 0 8 | -------------------------------------------------------------------------------- /CodeEval/commuting_engineer.in: -------------------------------------------------------------------------------- 1 | 1 | CodeEval 1355 Market St, SF (37.7768016, -122.4169151) 2 | 2 | Yelp 706 Mission St, SF (37.7860105, -122.4025377) 3 | 3 | Square 110 5th St, SF (37.7821494, -122.4058960) 4 | 4 | Airbnb 99 Rhode Island St, SF (37.7689269, -122.4029053) 5 | 5 | Dropbox 185 Berry St, SF (37.7768800, -122.3911496) 6 | 6 | Zynga 699 8th St, SF (37.7706628, -122.4040139) 7 | 7 | Zynga 699 8th St, SF (31.7706628, -122.4040139) 8 | 8 | Zynga 699 8th St, SF (36.7706628, -122.4040139) 9 | 9 | Zynga 699 8th St, SF (55.7706628, -122.4040139) 10 | 10 | Zynga 699 8th St, SF (37.7706628, -122.4040139) 11 | -------------------------------------------------------------------------------- /CodeEval/compressed_sequence.in: -------------------------------------------------------------------------------- 1 | 40 40 40 40 29 29 29 29 29 29 29 29 57 57 92 92 92 92 92 86 86 86 86 86 86 86 86 86 86 2 | 73 73 73 73 41 41 41 41 41 41 41 41 41 41 3 | 1 1 3 3 3 2 2 2 2 14 14 14 11 11 11 2 4 | 7 5 | -------------------------------------------------------------------------------- /CodeEval/counting_primes.in: -------------------------------------------------------------------------------- 1 | 2,10 2 | 20,30 3 | 101,109 4 | 203,210 5 | 97,100 6 | -------------------------------------------------------------------------------- /CodeEval/cracking_eggs.hs: -------------------------------------------------------------------------------- 1 | -- egg k e 2 | egg 0 e = 0 3 | egg k 1 = k 4 | egg k e = minimum [ 1 + max eggdies egglives | n <- [1..k] 5 | , let eggdies = egg (n-1) (e-1) 6 | , let egglives = egg (k-n) e ] 7 | 8 | eggs = let zero = repeat 0 9 | ans = zero : [ 0:k:[ optimalChoice ans k e | e <- [2..]] | k <- [1..]] 10 | in ans 11 | 12 | optimalChoice ans k e = 1 + minimum [ max eggdies egglives 13 | | n <- [1..k] 14 | , let eggdies = ans !! (n-1) !! (e-1) 15 | , let egglives = ans !! (k-n) !! e ] 16 | -------------------------------------------------------------------------------- /CodeEval/da_vyncy.in: -------------------------------------------------------------------------------- 1 | O draconia;conian devil! Oh la;h lame sa;saint! 2 | m quaerat voluptatem.;pora incidunt ut labore et d;, consectetur, adipisci velit;olore magnam aliqua;idunt ut labore et dolore magn;uptatem.;i dolorem ipsum qu;iquam quaerat vol;psum quia dolor sit amet, consectetur, a;ia dolor sit amet, conse;squam est, qui do;Neque porro quisquam est, qu;aerat voluptatem.;m eius modi tem;Neque porro qui;, sed quia non numquam ei;lorem ipsum quia dolor sit amet;ctetur, adipisci velit, sed quia non numq;unt ut labore et dolore magnam aliquam qu;dipisci velit, sed quia non numqua;us modi tempora incid;Neque porro quisquam est, qui dolorem i;uam eius modi tem;pora inc;am al 3 | -------------------------------------------------------------------------------- /CodeEval/da_vyncy.out: -------------------------------------------------------------------------------- 1 | O draconian devil! Oh lame saint! 2 | Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. 3 | -------------------------------------------------------------------------------- /CodeEval/data_recovery.in: -------------------------------------------------------------------------------- 1 | 2000 and was not However, implemented 1998 it until;9 8 3 4 1 5 7 2 2 | programming first The language;3 2 1 3 | programs Manchester The written ran Mark 1952 1 in Autocode from;6 2 1 7 5 3 11 4 8 9 4 | -------------------------------------------------------------------------------- /CodeEval/decimal_to_binary.hs: -------------------------------------------------------------------------------- 1 | -- decimal_to_binary.hs 2 | {- 3 | - Given a decimal (base 10) number, print out its binary representation. 4 | - Input sample: 5 | - 6 | - File containing positive whole decimal numbers, one per line. e.g. 7 | - 8 | - 2 9 | - 10 10 | - 67 11 | - 12 | - Output sample: 13 | - 14 | - Print the decimal representation, one per line. 15 | - e.g. 16 | - 17 | - 10 18 | - 1010 19 | - 1000011 20 | -} 21 | 22 | 23 | import System.Environment (getArgs) 24 | 25 | main = do 26 | args <- getArgs 27 | txt <- readFile (head args) 28 | putStr $ solveProblem txt 29 | 30 | solveProblem txt = let inputs = map read $ lines txt 31 | outputs = map binaryString inputs 32 | in unlines outputs 33 | 34 | binaryString 0 = "0" 35 | binaryString n = reverse $ binaryString' n 36 | where binaryString' 0 = "" 37 | binaryString' n | even n = '0' : binaryString' (n `quot` 2) 38 | | odd n = '1' : binaryString' (n `quot` 2) 39 | -------------------------------------------------------------------------------- /CodeEval/decimal_to_binary.in: -------------------------------------------------------------------------------- 1 | 2 2 | 10 3 | 67 4 | -------------------------------------------------------------------------------- /CodeEval/delta_time.in: -------------------------------------------------------------------------------- 1 | 14:01:57 12:47:11 2 | 13:09:42 22:16:15 3 | 08:08:06 08:38:28 4 | 23:35:07 02:49:59 5 | 14:31:45 14:46:56 6 | -------------------------------------------------------------------------------- /CodeEval/detecting_cycles.in: -------------------------------------------------------------------------------- 1 | 2 0 6 3 1 6 3 1 6 3 1 2 | 2 0 6 3 | -------------------------------------------------------------------------------- /CodeEval/digit_statistics.in: -------------------------------------------------------------------------------- 1 | 2 5 2 | 3 1000000000000 3 | 9 1000000007777 4 | -------------------------------------------------------------------------------- /CodeEval/distinct_subsequences.in: -------------------------------------------------------------------------------- 1 | babgbag,bag 2 | rabbbit,rabbit 3 | -------------------------------------------------------------------------------- /CodeEval/endianness.hs: -------------------------------------------------------------------------------- 1 | -- endianness.hs 2 | {- 3 | - CHALLENGE DESCRIPTION: 4 | - 5 | - Write a program to print out the endianness of the system. 6 | - 7 | - INPUT SAMPLE: 8 | - 9 | - None 10 | - 11 | - OUTPUT SAMPLE: 12 | - 13 | - Print to stdout, the endianness, wheather it is LittleEndian or BigEndian. 14 | - e.g. 15 | - 16 | - BigEndian 17 | -} 18 | 19 | import Data.Binary (decode) 20 | import Data.Binary.Put (putWord16host, runPut) 21 | import Data.Word (Word8) 22 | 23 | main = do 24 | putStrLn solveProblem 25 | 26 | solveProblem = if littleEndian then "LittleEndian" else "BigEndian" 27 | 28 | littleEndian = let raw = decode $ runPut $ putWord16host 0x11 :: Word8 29 | in raw == 0x11 30 | 31 | -------------------------------------------------------------------------------- /CodeEval/even_numbers.hs: -------------------------------------------------------------------------------- 1 | -- even_numbers.hs 2 | {- 3 | - Write a program which checks input numbers and determines whether a number is even or not. 4 | - 5 | - Input sample: 6 | - 7 | - Your program should accept as its first argument a path to a filename. Input example is the following 8 | - 9 | - 701 10 | - 4123 11 | - 2936 12 | - Output sample: 13 | - 14 | - Print 1 if the number is even or 0 if the number is odd. 15 | - 16 | - 0 17 | - 0 18 | - 1 19 | -} 20 | 21 | {- 22 | - is a builtin predicate in Haskell 23 | - it could easily be implemented as 24 | - even x = (x `mod` 2) == 0 25 | -} 26 | import System.Environment (getArgs) 27 | 28 | main = do 29 | args <- getArgs 30 | txt <- readFile (head args) 31 | putStr $ solveProblem txt 32 | 33 | solveProblem txt = let inputs = map read $ lines txt 34 | anss = map even inputs 35 | outputs = [ if a then "1" else "0" | a <- anss ] 36 | in unlines outputs 37 | -------------------------------------------------------------------------------- /CodeEval/even_numbers.in: -------------------------------------------------------------------------------- 1 | 701 2 | 4123 3 | 2936 4 | -------------------------------------------------------------------------------- /CodeEval/fibonacci_series.hs: -------------------------------------------------------------------------------- 1 | -- fibonacci_series.hs 2 | {- 3 | - The Fibonacci series is defined as: F(0) = 0; F(1) = 1; F(n) = F(n-1) + F(n-2) when n>1;. Given a positive integer 'n', print out the F(n). 4 | - Input sample: 5 | - 6 | - The first argument will be a text file containing a positive integer, one per line. e.g. 7 | - 8 | - 5 9 | - 12 10 | - 11 | - Output sample: 12 | - 13 | - Print to stdout, the fibonacci number, F(n). 14 | - e.g. 15 | - 16 | - 5 17 | - 144 18 | -} 19 | 20 | import System.Environment (getArgs) 21 | 22 | fibs = [0,1] ++ zipWith (+) fibs (tail fibs) 23 | 24 | main = do 25 | args <- getArgs 26 | txt <- readFile (head args) 27 | putStr $ solveProblem txt 28 | 29 | solveProblem txt = let inps = map read $ lines txt 30 | outputs = map (fibs !!) inps 31 | in unlines $ map show outputs 32 | -------------------------------------------------------------------------------- /CodeEval/fibonacci_series.in: -------------------------------------------------------------------------------- 1 | 5 2 | 12 3 | -------------------------------------------------------------------------------- /CodeEval/file_size.hs: -------------------------------------------------------------------------------- 1 | -- file_size.hs 2 | {- 3 | - Print the size of a file in bytes. 4 | - Input sample: 5 | - 6 | - Path to a filename. e.g. 7 | - 8 | - foo.txt 9 | - 10 | - Output sample: 11 | - 12 | - Print the size of the file in bytes. 13 | - e.g. 14 | - 15 | - 55 16 | -} 17 | 18 | import System.Environment (getArgs) 19 | 20 | main = do 21 | args <- getArgs 22 | txt <- readFile (head args) 23 | print $ length txt 24 | -------------------------------------------------------------------------------- /CodeEval/find_a_square.in: -------------------------------------------------------------------------------- 1 | (1,6), (6,7), (2,7), (9,1) 2 | (4,1), (3,4), (0,5), (1,2) 3 | (4,6), (5,5), (5,6), (4,5) 4 | -------------------------------------------------------------------------------- /CodeEval/find_min.in: -------------------------------------------------------------------------------- 1 | 97,39,34,37,656,97 2 | 186,75,68,16,539,186 3 | 137,49,48,17,461,137 4 | 98,59,6,30,524,98 5 | 46,18,7,11,9,46 6 | -------------------------------------------------------------------------------- /CodeEval/find_min.out: -------------------------------------------------------------------------------- 1 | 8 2 | 38 3 | 41 4 | 40 5 | 12 6 | -------------------------------------------------------------------------------- /CodeEval/first_non_repeated_character.hs: -------------------------------------------------------------------------------- 1 | -- first_non_repeated_character.hs 2 | {- 3 | - Write a program to find the first non repeated character in a string. 4 | - Input sample: 5 | - 6 | - The first argument will be a text file containing strings. e.g. 7 | - 8 | - yellow 9 | - tooth 10 | - 11 | - Output sample: 12 | - 13 | - Print to stdout, the first non repeating character, one per line. 14 | - e.g. 15 | - 16 | - y 17 | - h 18 | -} 19 | 20 | 21 | import System.Environment (getArgs) 22 | import Data.Maybe 23 | 24 | main = do 25 | args <- getArgs 26 | txt <- readFile (head args) 27 | putStr $ solveProblem txt 28 | 29 | solveProblem txt = let inputs = lines txt 30 | outputs = map (:"") $ mapMaybe firstUnique inputs 31 | in unlines outputs 32 | 33 | 34 | firstUnique [] = Nothing 35 | firstUnique (ch:r) | ch `elem` r = firstUnique $ filter (/= ch) r 36 | | otherwise = Just ch 37 | -------------------------------------------------------------------------------- /CodeEval/fizz_buzz.in: -------------------------------------------------------------------------------- 1 | 3 5 10 2 | 2 7 15 3 | -------------------------------------------------------------------------------- /CodeEval/flavius_josephus.in: -------------------------------------------------------------------------------- 1 | 10,3 2 | 5,2 3 | 100,100 4 | -------------------------------------------------------------------------------- /CodeEval/jolly_jumpers.in: -------------------------------------------------------------------------------- 1 | 4 1 4 2 3 2 | 2 1 2 3 | 5 1 2 3 4 5 4 | -------------------------------------------------------------------------------- /CodeEval/juggling_with_zeros.in: -------------------------------------------------------------------------------- 1 | 00 0 0 00 00 0 2 | 00 0 3 | 00 0 0 000 00 0000000 0 000 4 | 0 000000000 00 00 5 | -------------------------------------------------------------------------------- /CodeEval/lettercase_percentage_ratio.hs: -------------------------------------------------------------------------------- 1 | -- lettercase_percentage_ratio.hs 2 | {- 3 | - Figure out what percent of characters in a string are upper/lower-case 4 | -} 5 | 6 | import System.Environment (getArgs) 7 | import Data.Char (isLower, isUpper) 8 | import Text.Printf 9 | 10 | main = do 11 | args <- getArgs 12 | txt <- readFile (head args) 13 | putStr $ solveProblem txt 14 | 15 | solveProblem txt = 16 | let inps = lines txt 17 | anss = map lowerUpperPercentages inps 18 | outs = map showAnswer anss 19 | in unlines outs 20 | 21 | lowerUpperPercentages :: String -> (Double, Double) 22 | lowerUpperPercentages str = 23 | let lo = fromIntegral $ length $ filter isLower str 24 | up = fromIntegral $ length $ filter isUpper str 25 | tot = fromIntegral $ length str 26 | in (lo / tot, up / tot) 27 | 28 | showAnswer (lo, up) = 29 | printf "lowercase: %.2f uppercase: %.2f" (100*lo) (100*up) 30 | -------------------------------------------------------------------------------- /CodeEval/longest_lines.in: -------------------------------------------------------------------------------- 1 | 2 2 | Hello World 3 | 4 | CodeEval 5 | Quick Fox 6 | A 7 | San Francisco 8 | -------------------------------------------------------------------------------- /CodeEval/longest_word.in: -------------------------------------------------------------------------------- 1 | some line with text 2 | another line 3 | -------------------------------------------------------------------------------- /CodeEval/lowercase.hs: -------------------------------------------------------------------------------- 1 | -- lowercase.hs 2 | {- 3 | - Given a string write a program to convert it into lowercase. 4 | - Input sample: 5 | - 6 | - The first argument will be a text file containing sentences, one per line. 7 | - You can assume all characters are from the english language. e.g. 8 | - 9 | - HELLO CODEEVAL 10 | - This is some text 11 | - 12 | - Output sample: 13 | - 14 | - Print to stdout, the lowercase version of the sentence, each on a new line. 15 | - e.g. 16 | - 17 | - hello codeeval 18 | - this is some text 19 | -} 20 | 21 | 22 | import System.Environment (getArgs) 23 | import Data.Char (toLower) 24 | 25 | main = do 26 | args <- getArgs 27 | txt <- readFile (head args) 28 | putStr $ solveProblem txt 29 | 30 | solveProblem txt = map toLower txt 31 | -------------------------------------------------------------------------------- /CodeEval/lowercase.in: -------------------------------------------------------------------------------- 1 | HELLO CODEEVAL 2 | This is some text 3 | -------------------------------------------------------------------------------- /CodeEval/lowest_common_ancestor.in: -------------------------------------------------------------------------------- 1 | 8 52 2 | 3 29 3 | -------------------------------------------------------------------------------- /CodeEval/lowest_unique_number.in: -------------------------------------------------------------------------------- 1 | 3 3 9 1 6 5 8 1 5 3 2 | 9 2 9 9 1 8 8 8 2 1 1 3 | -------------------------------------------------------------------------------- /CodeEval/message_decoding.in: -------------------------------------------------------------------------------- 1 | $#**\0100000101101100011100101000 2 | -------------------------------------------------------------------------------- /CodeEval/minesweeper.in: -------------------------------------------------------------------------------- 1 | 3,5;**.........*... 2 | 4,4;*........*...... 3 | -------------------------------------------------------------------------------- /CodeEval/minesweeper.out: -------------------------------------------------------------------------------- 1 | **100332001*100 2 | *10022101*101110 3 | -------------------------------------------------------------------------------- /CodeEval/minimum_path_sum.in: -------------------------------------------------------------------------------- 1 | 2 2 | 4,6 3 | 2,8 4 | 3 5 | 1,2,3 6 | 4,5,6 7 | 7,8,9 8 | -------------------------------------------------------------------------------- /CodeEval/morse_code.in: -------------------------------------------------------------------------------- 1 | .- ...- ..--- .-- .... .. . -.-. -..- ....- ..... 2 | -... .... ...-- 3 | -------------------------------------------------------------------------------- /CodeEval/multiples_of_a_number.in: -------------------------------------------------------------------------------- 1 | 13,8 2 | 17,16 3 | -------------------------------------------------------------------------------- /CodeEval/multiplication_tables.hs: -------------------------------------------------------------------------------- 1 | -- multiplication_tables.hs 2 | {- 3 | - Challenge Description: 4 | - 5 | - Print out the grade school multiplication table upto 12*12. 6 | - Input sample: 7 | - 8 | - There is no input for this program. 9 | - Output sample: 10 | - 11 | - Print out the table in a matrix like fashion, each number formatted to 12 | - a width of 4 (The numbers are right-aligned and strip out leading/trailing 13 | - spaces on each line). The first 3 line will look like: 14 | - 15 | - 1 2 3 4 5 6 7 8 9 10 11 12 16 | - 2 4 6 8 10 12 14 16 18 20 22 24 17 | - 3 6 9 12 15 18 21 24 27 30 33 36 18 | -} 19 | 20 | import Text.Printf 21 | 22 | main = do 23 | putStr $ solveProblem 12 12 24 | 25 | solveProblem :: Int -> Int -> String 26 | solveProblem m n = unlines $ map printRow $ multiplicationTable m n 27 | 28 | printRow :: [Int] -> String 29 | printRow row = concat [ printf "%4d" v | v <- row ] 30 | 31 | multiplicationTable m n = [[ i*j | j <- [1..n]] | i <- [1..m]] 32 | 33 | -------------------------------------------------------------------------------- /CodeEval/nice_angles.hs: -------------------------------------------------------------------------------- 1 | -- nice_angles.hs 2 | {- 3 | - Convert a decimal expression for an angle into minutes/seconds format 4 | -} 5 | 6 | import Text.Printf 7 | import System.Environment (getArgs) 8 | 9 | main = do 10 | args <- getArgs 11 | txt <- readFile (head args) 12 | putStr $ solveProblem txt 13 | 14 | solveProblem txt = let inps = map read $ lines txt 15 | anss = map toAngle inps 16 | in unlines $ map show anss 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | data Angle = Angle { getDegrees :: Int 27 | , getMinutes :: Int 28 | , getSeconds :: Int 29 | } deriving (Eq, Ord) 30 | 31 | instance Show Angle where 32 | show (Angle d m s) = printf "%d.%02d'%02d\"" d m s 33 | 34 | toAngle :: Double -> Angle 35 | toAngle x = let d = floor x 36 | r = 60 * (x - fromIntegral d) 37 | m = floor r 38 | r' = 60 * (r - fromIntegral m) 39 | s = floor r' 40 | in Angle d m s 41 | -------------------------------------------------------------------------------- /CodeEval/nice_angles.in: -------------------------------------------------------------------------------- 1 | 330.39991833 2 | 0.001 3 | 14.64530319 4 | 0.25 5 | 254.16991217 6 | -------------------------------------------------------------------------------- /CodeEval/number_of_ones.hs: -------------------------------------------------------------------------------- 1 | -- number_of_ones.hs 2 | {- 3 | - Write a program to determine the number of 1 bits in the internal representation of a given integer. 4 | - Input sample: 5 | - 6 | - The first argument will be a text file containing an integer, one per line. e.g. 7 | - 8 | - 10 9 | - 22 10 | - 56 11 | - 12 | - Output sample: 13 | - 14 | - Print to stdout, the number of ones in the binary form of each number. 15 | - e.g. 16 | - 17 | - 2 18 | - 3 19 | - 3 20 | -} 21 | 22 | 23 | import System.Environment (getArgs) 24 | import qualified Data.Bits as Bits 25 | 26 | main = do 27 | args <- getArgs 28 | txt <- readFile (head args) 29 | putStr $ solveProblem txt 30 | 31 | solveProblem txt = let inputs = [ read ln :: Int | ln <- lines txt ] 32 | ans = map Bits.popCount inputs 33 | in unlines $ map show ans 34 | -------------------------------------------------------------------------------- /CodeEval/number_operations.in: -------------------------------------------------------------------------------- 1 | 44 6 1 49 47 2 | 34 1 49 2 21 3 | 31 38 27 51 18 4 | -------------------------------------------------------------------------------- /CodeEval/number_pairs.in: -------------------------------------------------------------------------------- 1 | 1,2,3,4,6;5 2 | 2,4,5,6,9,11,15;20 3 | 1,2,3,4;50 4 | -------------------------------------------------------------------------------- /CodeEval/odd_numbers.hs: -------------------------------------------------------------------------------- 1 | -- odd_numbers.hs 2 | {- 3 | - Print the odd numbers from 1 to 99. 4 | - Input sample: 5 | - 6 | - None 7 | - Output sample: 8 | - 9 | - Print the odd numbers from 1 to 99, one number per line. 10 | -} 11 | 12 | solveProblem a b = let outputs = [a,a+2..b] 13 | in unlines $ map show outputs 14 | 15 | main = do 16 | putStr $ solveProblem 1 99 17 | -------------------------------------------------------------------------------- /CodeEval/overlapping_rectangles.in: -------------------------------------------------------------------------------- 1 | -3,3,-1,1,1,-1,3,-3 2 | -3,3,-1,1,-2,4,2,2 3 | -------------------------------------------------------------------------------- /CodeEval/palindromic_ranges.in: -------------------------------------------------------------------------------- 1 | 1 2 2 | 1 7 3 | 87 88 4 | -------------------------------------------------------------------------------- /CodeEval/panagrams.in: -------------------------------------------------------------------------------- 1 | A quick brown fox jumps over the lazy dog 2 | A slow yellow fox crawls under the proactive dog 3 | -------------------------------------------------------------------------------- /CodeEval/pascals_triangle.in: -------------------------------------------------------------------------------- 1 | 6 2 | 10 3 | 20 4 | -------------------------------------------------------------------------------- /CodeEval/pass_triangle.in: -------------------------------------------------------------------------------- 1 | 5 2 | 9 6 3 | 4 6 8 4 | 0 7 1 5 5 | -------------------------------------------------------------------------------- /CodeEval/penultimate_word.hs: -------------------------------------------------------------------------------- 1 | -- penultimate_word.hs 2 | {- 3 | - Write a program which finds the next-to-last word in a string. 4 | - 5 | - Input sample: 6 | - 7 | - Your program should accept as its first argument a path to a filename. Input example is the following 8 | - 9 | - some line with text 10 | - another line 11 | - Each line has more than one word. 12 | - 13 | - Output sample: 14 | - 15 | - Print the next-to-last word in the following way. 16 | - 17 | - with 18 | - another 19 | -} 20 | 21 | 22 | import System.Environment (getArgs) 23 | 24 | main = do 25 | args <- getArgs 26 | txt <- readFile (head args) 27 | putStr $ solveProblem txt 28 | 29 | solveProblem txt = let inputs = map words $ lines txt 30 | outputs = map (last.init) inputs 31 | in unlines outputs 32 | -------------------------------------------------------------------------------- /CodeEval/penultimate_word.in: -------------------------------------------------------------------------------- 1 | Your program should accept as its first argument a path to a filename. Input 2 | example is the following 3 | some line with text 4 | another line 5 | -------------------------------------------------------------------------------- /CodeEval/point_in_circle.in: -------------------------------------------------------------------------------- 1 | Center: (2.12, -3.48); Radius: 17.22; Point: (16.21, -5.0) 2 | Center: (5.05, -11); Radius: 21.2; Point: (-31, -45) 3 | Center: (-9.86, 1.95); Radius: 47.28; Point: (6.03, -6.42) 4 | -------------------------------------------------------------------------------- /CodeEval/prefix_expressions.in: -------------------------------------------------------------------------------- 1 | * + 2 3 4 2 | -------------------------------------------------------------------------------- /CodeEval/prime_palindrome.hs: -------------------------------------------------------------------------------- 1 | -- prime_palindrome.hs 2 | {- 3 | - Write a program to determine the biggest prime palindrome under 1000. 4 | - Input sample: 5 | - 6 | - None 7 | - Output sample: 8 | - 9 | - Your program should print the largest palindrome on stdout. i.e. 10 | - 11 | - 929 12 | -} 13 | 14 | 15 | primes = 2:[n | n <- [3,5..], all (/= 0) [ n `mod` p | p <- divcans n ] ] 16 | where divcans n = takeWhile (\p -> p*p <= n) primes 17 | 18 | isPalindromic n = let s = show n 19 | in s == reverse s 20 | solveProblem bound = maximum $ filter isPalindromic $ takeWhile (>--><--<<--<<>>>-->< 3 | <-->> 4 | -------------------------------------------------------------------------------- /CodeEval/sum_of_digits.hs: -------------------------------------------------------------------------------- 1 | -- sum_of_digits.hs 2 | {- 3 | - Given a positive integer, find the sum of its constituent digits. 4 | - Input sample: 5 | - 6 | - The first argument will be a text file containing positive integers, one per line. e.g. 7 | - 8 | - 23 9 | - 496 10 | - 11 | - Output sample: 12 | - 13 | - Print to stdout, the sum of the numbers that make up the integer, one per line. 14 | - e.g. 15 | - 16 | - 5 17 | - 19 18 | -} 19 | 20 | import System.Environment (getArgs) 21 | 22 | sumOfDigits 0 = 0 23 | sumOfDigits n = let (q,r) = n `divMod` 10 24 | in r + sumOfDigits q 25 | 26 | main = do 27 | args <- getArgs 28 | txt <- readFile (head args) 29 | putStr $ solveProblem txt 30 | 31 | solveProblem txt = let inps = map read $ lines txt 32 | outputs = map sumOfDigits inps 33 | in unlines $ map show outputs 34 | -------------------------------------------------------------------------------- /CodeEval/sum_of_digits.in: -------------------------------------------------------------------------------- 1 | 23 2 | 496 3 | -------------------------------------------------------------------------------- /CodeEval/sum_of_integers.in: -------------------------------------------------------------------------------- 1 | -10, 2, 3, -2, 0, 5, -15 2 | 2,3,-2,-1,10 3 | -------------------------------------------------------------------------------- /CodeEval/sum_of_integers_from_file.hs: -------------------------------------------------------------------------------- 1 | -- sum_of_integers_from_file.hs 2 | {- 3 | - Print out the sum of integers read from a file. 4 | - Input sample: 5 | - 6 | - The first argument to the program will be a text file containing a positive integer, one per line. e.g. 7 | - 8 | - 5 9 | - 12 10 | - 11 | - NOTE: For solutions in JavaScript, assume that there are 7 lines of input 12 | - 13 | - Output sample: 14 | - 15 | - Print out the sum of all the integers read from the file. 16 | - e.g. 17 | - 18 | - 17 19 | -} 20 | 21 | import System.Environment (getArgs) 22 | 23 | main = do 24 | args <- getArgs 25 | txt <- readFile (head args) 26 | putStr $ solveProblem txt 27 | 28 | solveProblem txt = let inps = map read $ lines txt 29 | in show $ sum inps 30 | -------------------------------------------------------------------------------- /CodeEval/sum_of_integers_from_file.in: -------------------------------------------------------------------------------- 1 | 5 2 | 12 3 | -------------------------------------------------------------------------------- /CodeEval/sum_of_primes.hs: -------------------------------------------------------------------------------- 1 | -- sum_of_primes.hs 2 | {- 3 | - Write a program to determine the sum of the first 1000 prime numbers. 4 | - 5 | - Input sample: 6 | - None 7 | - 8 | - Output sample: 9 | - Your program should print the sum on stdout.i.e. 10 | - 11 | - 3682913 12 | -} 13 | 14 | primes = 2:[n | n <- [3,5..], all (/= 0) [ n `mod` p | p <- divcans n ] ] 15 | where divcans n = takeWhile (\p -> p*p <= n) primes 16 | 17 | main = print $ sum $ take 1000 primes 18 | -------------------------------------------------------------------------------- /CodeEval/sum_to_zero.in: -------------------------------------------------------------------------------- 1 | 2,3,1,0,-4,-1 2 | 0,-1,3,-2 3 | -------------------------------------------------------------------------------- /CodeEval/swap_case.hs: -------------------------------------------------------------------------------- 1 | -- swap_case.hs 2 | {- 3 | - Write a program which swaps letters' case in a sentence. All non-letter characters should remain the same. 4 | - 5 | - Input sample: 6 | - 7 | - Your program should accept as its first argument a path to a filename. Input example is the following 8 | - 9 | - Hello world! 10 | - JavaScript language 1.8 11 | - A letter 12 | - Output sample: 13 | - 14 | - Print results in the following way. 15 | - 16 | - hELLO WORLD! 17 | - jAVAsCRIPT LANGUAGE 1.8 18 | - a LETTER 19 | -} 20 | 21 | 22 | import System.Environment (getArgs) 23 | import Data.Char (isUpper, isLower, toUpper, toLower) 24 | 25 | main = do 26 | args <- getArgs 27 | txt <- readFile (head args) 28 | putStr $ solveProblem txt 29 | 30 | solveProblem txt = let inputs = lines txt 31 | outputs = [ map swapCase ln | ln <- inputs ] 32 | in unlines outputs 33 | 34 | swapCase ch | isUpper ch = toLower ch 35 | | isLower ch = toUpper ch 36 | | otherwise = ch 37 | -------------------------------------------------------------------------------- /CodeEval/swap_case.in: -------------------------------------------------------------------------------- 1 | Hello world! 2 | JavaScript language 1.8 3 | A letter 4 | -------------------------------------------------------------------------------- /CodeEval/telephone_words.in: -------------------------------------------------------------------------------- 1 | 4155230 2 | -------------------------------------------------------------------------------- /CodeEval/template.hs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | import System.Environment (getArgs) 5 | 6 | main = do 7 | args <- getArgs 8 | txt <- readFile (head args) 9 | putStr $ solveProblem txt 10 | 11 | wordsBy pred s = wordsBy' pred $ dropWhile pred s 12 | where wordsBy' _ [] = [] 13 | wordsBy' pred s = let (f,r) = break pred s 14 | in f:wordsBy' pred (dropWhile pred r) 15 | 16 | solveProblem txt = let lns = lines txt 17 | 18 | -------------------------------------------------------------------------------- /CodeEval/text_dollar.in: -------------------------------------------------------------------------------- 1 | 3 2 | 10 3 | 21 4 | 466 5 | 1234 6 | -------------------------------------------------------------------------------- /CodeEval/text_dollar.out: -------------------------------------------------------------------------------- 1 | ThreeDollars 2 | TenDollars 3 | TwentyOneDollars 4 | FourHundredSixtySixDollars 5 | OneThousandTwoHundredThirtyFourDollars 6 | -------------------------------------------------------------------------------- /CodeEval/text_to_number.in: -------------------------------------------------------------------------------- 1 | fifteen 2 | negative six hundred thirty eight 3 | zero 4 | two million one hundred seven 5 | -------------------------------------------------------------------------------- /CodeEval/trailing_string.in: -------------------------------------------------------------------------------- 1 | Hello World,World 2 | Hello CodeEval,CodeEval 3 | San Francisco,San Jose 4 | -------------------------------------------------------------------------------- /CodeEval/type_ahead.in: -------------------------------------------------------------------------------- 1 | 2,lamb 2 | 2,the 3 | 4,why does the 4 | -------------------------------------------------------------------------------- /CodeEval/ugly_numbers.in: -------------------------------------------------------------------------------- 1 | 1 2 | 9 3 | 011 4 | 12345 5 | 123456789234 6 | 123456789234 7 | 123456789234 8 | 123456789234 9 | 123456789234 10 | 123456789234 11 | 123456789234 12 | 123456789234 13 | 123456789234 14 | 123456789234 15 | -------------------------------------------------------------------------------- /CodeEval/unique_elements.in: -------------------------------------------------------------------------------- 1 | 1,1,1,2,2,3,3,4,4 2 | 2,3,4,5,5 3 | -------------------------------------------------------------------------------- /CodeEval/valid_parentheses.in: -------------------------------------------------------------------------------- 1 | () 2 | ()[]{} 3 | (()) 4 | {{}} 5 | (([[{{}}]])) 6 | ([{([{}{}][])}[]])([]){} 7 | (() 8 | ()) 9 | [[] 10 | []] 11 | -------------------------------------------------------------------------------- /CodeEval/word_search.in: -------------------------------------------------------------------------------- 1 | ASADB 2 | ABCCED 3 | SEE 4 | ABCB 5 | ABCESCFSADEE 6 | ABCESCEFSADEE 7 | -------------------------------------------------------------------------------- /Fake/Bar.hs: -------------------------------------------------------------------------------- 1 | module Fake.Bar 2 | ( a 3 | , b 4 | , c 5 | ) where 6 | 7 | a x = x 8 | b x = 2*x 9 | c x = 3*x 10 | -------------------------------------------------------------------------------- /Fake/Foo.hs: -------------------------------------------------------------------------------- 1 | module Fake.Foo 2 | ( a 3 | , b 4 | , c 5 | ) where 6 | 7 | a x = x 8 | b x = x*x 9 | c x = x*x*x 10 | -------------------------------------------------------------------------------- /GoogleCodeJam/TheLastWord/01.in: -------------------------------------------------------------------------------- 1 | 7 2 | CAB 3 | JAM 4 | CODE 5 | ABAAB 6 | CABCBBABC 7 | ABCABCABC 8 | ZXCASDQWE 9 | -------------------------------------------------------------------------------- /GoogleCodeJam/TheLastWord/01.out: -------------------------------------------------------------------------------- 1 | CAB 2 | MJA 3 | OCDE 4 | BBAAA 5 | CCCABBBAB 6 | CCCBAABAB 7 | ZXCASDQWE 8 | -------------------------------------------------------------------------------- /GoogleCodeJam/TheLastWord/TheLastWord.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | 3 | main = do 4 | inps <- fmap (tail . lines) getContents 5 | forM_ (zip inps [1..]) $ \(inp, i) -> do 6 | putStrLn $ "Case #" ++ show i ++ ": " ++ lastWord inp 7 | 8 | lastWord "" = "" 9 | lastWord s = 10 | let 11 | (front, maxChar, back) = splitAtMax s 12 | in maxChar : (lastWord front) ++ back 13 | 14 | 15 | splitAtMax :: (Eq a, Ord a) => [a] -> ([a], a, [a]) 16 | splitAtMax xs = 17 | let 18 | x = maximum xs 19 | idx = lastIndexOf x xs 20 | in (take idx xs, xs !! idx, drop (idx+1) xs) 21 | 22 | lastIndexOf :: Eq a => a -> [a] -> Int 23 | lastIndexOf e xs = last [ idx | (x, idx) <- zip xs [0..], x == e ] 24 | -------------------------------------------------------------------------------- /HackerRank/AreaUnderCurves/problem_statement.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanpbrewster/haskell/6edd0afe234008a48b4871032dedfd143ca6e412/HackerRank/AreaUnderCurves/problem_statement.pdf -------------------------------------------------------------------------------- /HackerRank/FibonacciModified/FibonacciModified.hs: -------------------------------------------------------------------------------- 1 | modifiedFibSequence :: Integer -> Integer -> [Integer] 2 | modifiedFibSequence a b = 3 | let fibs = a : b : zipWith nextElement (tail fibs) (fibs) 4 | in fibs 5 | where nextElement tnp1 tn = tnp1^2 + tn 6 | 7 | main = do 8 | [a, b, n] <- fmap (map read . words) getLine :: IO [Integer] 9 | print $ (modifiedFibSequence a b) !! (fromInteger (n-1)) 10 | -------------------------------------------------------------------------------- /HackerRank/FractalTrees/problem_statement.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanpbrewster/haskell/6edd0afe234008a48b4871032dedfd143ca6e412/HackerRank/FractalTrees/problem_statement.pdf -------------------------------------------------------------------------------- /HackerRank/IdentifySmithNumbers/IdentifySmithNumbers.hs: -------------------------------------------------------------------------------- 1 | -- IdentifySmithNumers.hs 2 | -- See problem_statement.pdf 3 | 4 | import Data.Char (digitToInt) 5 | 6 | primes = 2 : filter isPrime [3,5..] 7 | 8 | isComposite n = any (\p -> n `mod` p == 0) (takeWhile (\p -> p*p <= n) primes) 9 | 10 | isPrime = not . isComposite 11 | 12 | factor :: Int -> [Int] 13 | factor n = factor' n primes 14 | where factor' n (p:ps) | n `mod` p == 0 = p : factor' (n `div` p) (p:ps) 15 | | p*p > n = if n > 1 then [n] else [] 16 | | otherwise = factor' n ps 17 | 18 | isSmith :: Int -> Bool 19 | isSmith n = 20 | let factor_digits = map digitToInt $ concat $ map show $ factor n 21 | original_digits = map digitToInt $ show n 22 | in sum factor_digits == sum original_digits 23 | 24 | boolToInt :: Bool -> Int 25 | boolToInt True = 1 26 | boolToInt False = 0 27 | 28 | main = do 29 | n <- readLn :: IO Int 30 | print $ boolToInt (isSmith n) 31 | -------------------------------------------------------------------------------- /HackerRank/IdentifySmithNumbers/problem_statement.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanpbrewster/haskell/6edd0afe234008a48b4871032dedfd143ca6e412/HackerRank/IdentifySmithNumbers/problem_statement.pdf -------------------------------------------------------------------------------- /HackerRank/ListsAndGCD/ListsAndGCD.hs: -------------------------------------------------------------------------------- 1 | import Data.Map.Strict (fromList, intersectionWith, toList) 2 | import Control.Monad 3 | 4 | main = do 5 | t <- readLn :: IO Int 6 | inps <- replicateM t readInput 7 | putStrLn $ solveProblem inps 8 | 9 | chunksOf size [] = [] 10 | chunksOf size xs = let (f,r) = splitAt size xs in f : chunksOf size r 11 | 12 | readInput = do 13 | xs <- fmap (map read . words) getLine 14 | return [ (k,v) | [k,v] <- chunksOf 2 xs ] 15 | 16 | solveProblem = formatAnswer . listGCD 17 | 18 | type ListOfPrimes = [(Int,Int)] 19 | listGCD :: [ListOfPrimes] -> ListOfPrimes 20 | listGCD pss = 21 | let ns = map fromList pss 22 | ans = foldl1 (intersectionWith min) ns 23 | in toList ans 24 | 25 | formatAnswer :: ListOfPrimes -> String 26 | formatAnswer ps = 27 | unwords [ show p ++ " " ++ show e | (p,e) <- ps ] 28 | -------------------------------------------------------------------------------- /HackerRank/ListsAndGCD/problem_statement.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanpbrewster/haskell/6edd0afe234008a48b4871032dedfd143ca6e412/HackerRank/ListsAndGCD/problem_statement.pdf -------------------------------------------------------------------------------- /HackerRank/ListsAndGCD/sample01.in: -------------------------------------------------------------------------------- 1 | 4 2 | 2 2 3 2 5 3 3 | 3 2 5 3 11 1 4 | 2 2 3 3 5 4 7 6 19 18 5 | 3 10 5 15 6 | -------------------------------------------------------------------------------- /HackerRank/MaxMin/MaxMin.hs: -------------------------------------------------------------------------------- 1 | import Data.List (sort) 2 | import Control.Monad 3 | 4 | main = do 5 | n <- readLn :: IO Int 6 | k <- readLn :: IO Int 7 | xs <- replicateM n readLn :: IO [Int] 8 | print $ solveInstance k xs 9 | 10 | solveInstance :: Int -> [Int] -> Int 11 | solveInstance = minimumUnfairness 12 | 13 | 14 | -- For a list X = [x1, ..., xn], find a subset S = {x_i1, ..., x_ik} where 15 | -- unfairness(S) = max(S) - min(S) 16 | -- in minimized 17 | -- To do this, sort X, then look at elements that are k apart 18 | minimumUnfairness :: Int -> [Int] -> Int 19 | minimumUnfairness k xs = 20 | let xs' = sort xs 21 | unfairnesses = zipWith (-) (drop (k-1) xs') xs' 22 | in minimum unfairnesses 23 | -------------------------------------------------------------------------------- /HackerRank/MaxMin/problem_statement.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanpbrewster/haskell/6edd0afe234008a48b4871032dedfd143ca6e412/HackerRank/MaxMin/problem_statement.pdf -------------------------------------------------------------------------------- /HackerRank/PickingCards/PickingCards.hs: -------------------------------------------------------------------------------- 1 | import Data.List (sort, foldl') 2 | import Control.Monad 3 | 4 | magic_mod = 1000000007 5 | 6 | main = do 7 | t <- readLn :: IO Int 8 | inps <- replicateM t readInput 9 | let anss = map solveInstance inps 10 | mapM_ putStrLn anss 11 | 12 | readInput = getLine >> fmap (map read . words) getLine 13 | 14 | solveInstance = show . ways 15 | 16 | ways xs = 17 | let cum_opts = cumScan xs 18 | true_opts = map (max 0) $ zipWith (-) cum_opts [0..] 19 | in productMod magic_mod $ take (length xs) true_opts 20 | 21 | cumScan = fastScan 22 | 23 | bfScan xs = [ length $ filter (<= k) xs | k <- [0..] ] 24 | 25 | fastScan xs = fastScan' (sort xs) 0 0 26 | where fastScan' [] cur count = repeat count 27 | fastScan' (y:ys) cur count 28 | | y <= cur = fastScan' ys cur (count+1) 29 | | otherwise = count : fastScan' (y:ys) (cur+1) count 30 | 31 | productMod m = foldl' (\cur x -> (cur*x) `mod` m) 1 32 | -------------------------------------------------------------------------------- /HackerRank/SolveMeSecond/SolveMeSecond.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | 3 | main = do 4 | n <- readLn :: IO Int 5 | lns <- replicateM n getLine 6 | let anss = map (sum . map read . words) lns 7 | mapM_ print anss 8 | -------------------------------------------------------------------------------- /HackerRank/StringCompression/StringCompression.hs: -------------------------------------------------------------------------------- 1 | import Data.List( group ) 2 | 3 | -- compress 4 | compress :: String -> String 5 | compress str = concat $ map abbreviate $ group str 6 | 7 | -- abbreviate "aaa" == "a3" 8 | -- abbreviate "a" == "a" 9 | abbreviate :: [Char] -> String 10 | abbreviate [ch] = [ch] 11 | abbreviate chs = head chs : show (length chs) 12 | 13 | main = do 14 | str <- getLine 15 | putStrLn $ compress str 16 | -------------------------------------------------------------------------------- /HackerRank/StringCompression/problem_statement.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanpbrewster/haskell/6edd0afe234008a48b4871032dedfd143ca6e412/HackerRank/StringCompression/problem_statement.pdf -------------------------------------------------------------------------------- /HackerRank/SubsetSum/SubsetSum.hs: -------------------------------------------------------------------------------- 1 | import Control.Monad 2 | import Data.List( sort ) 3 | import Data.IntMap hiding( map ) 4 | 5 | main = do 6 | n <- readLn :: IO Int 7 | arr <- fmap (map read . words) getLine :: IO [Int] 8 | t <- readLn :: IO Int 9 | inps <- replicateM t readLn :: IO [Int] 10 | let anss = solveAllInstances arr inps 11 | mapM_ print anss 12 | 13 | solveAllInstances arr inps = 14 | let arr' = reverse (sort arr) -- descending order 15 | cumsums = scanl (+) 0 arr' 16 | m = fromAscList $ zip cumsums [0..] 17 | in map (formatOutput . flip lookupGE m) inps 18 | 19 | formatOutput Nothing = -1 20 | formatOutput (Just (_, n)) = n 21 | -------------------------------------------------------------------------------- /HackerRank/SubsetSum/problem_statement.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanpbrewster/haskell/6edd0afe234008a48b4871032dedfd143ca6e412/HackerRank/SubsetSum/problem_statement.pdf -------------------------------------------------------------------------------- /HackerRank/ValidBST/problem_statement.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanpbrewster/haskell/6edd0afe234008a48b4871032dedfd143ca6e412/HackerRank/ValidBST/problem_statement.pdf -------------------------------------------------------------------------------- /MyParallel/collatz.hs: -------------------------------------------------------------------------------- 1 | -- collatz.hs 2 | 3 | collatz n | even n = n `div` 2 4 | | otherwise = 3*n + 1 5 | 6 | collatzSequence n = takeWhile (>1) $ iterate collatz n 7 | 8 | collatzLength = length . collatzSequence 9 | 10 | maxCollatzSeq bound = let xs = [1..bound] 11 | cls = map collatzLength xs 12 | in maximum $ zip cls xs 13 | 14 | maxCollatzPar bound = let xs = [1..bound] 15 | cls = map collatzLength xs 16 | in maximum $ zip cls xs 17 | 18 | maxCollatz = maxCollatzSeq 19 | 20 | main = print $ maxCollatz (10^6) 21 | -------------------------------------------------------------------------------- /Nonogram/Solver.hs: -------------------------------------------------------------------------------- 1 | module Nonogram.Solver 2 | where 3 | 4 | import Nonogram.Base 5 | import qualified Data.Array as A 6 | import Control.Monad (replicateM) 7 | 8 | solve :: Constraints -> Solution 9 | solve = bruteforceSolver 10 | 11 | bruteforceSolver constraints = 12 | let candidates = enumerateAll $ dimensions constraints 13 | sols = filter (`isConsistentWith` constraints) candidates 14 | in head sols 15 | 16 | 17 | -- Produce all possible solutions with given dimensions 18 | enumerateAll :: (Int, Int) -> [Solution] 19 | enumerateAll (nr,nc) = 20 | let enumerated_elements = replicateM (nr*nc) [White, Black] 21 | in [ Solution $ A.listArray ((1,1), (nr,nc)) elements | elements <- enumerated_elements ] 22 | 23 | isConsistentWith :: Solution -> Constraints -> Bool 24 | solution `isConsistentWith` constraints = 25 | generateConstraints solution == constraints 26 | -------------------------------------------------------------------------------- /Nonogram/Tests.hs: -------------------------------------------------------------------------------- 1 | module Nonogram.Tests 2 | where 3 | 4 | import Nonogram.Base 5 | 6 | tests = [ Constraints [Constraint (0,2), Constraint (2,0)] [Constraint (1,1), Constraint(1,1)] 7 | , Constraints (replicate 2 (Constraint (1,1))) (replicate 2 (Constraint (1,1))) 8 | , Constraints (replicate 3 (Constraint (2,1))) (replicate 3 (Constraint (2,1))) 9 | , Constraints (replicate 4 (Constraint (3,1))) (replicate 4 (Constraint (3,1))) 10 | ] 11 | -------------------------------------------------------------------------------- /OpenGL/DrawingShapes.hs: -------------------------------------------------------------------------------- 1 | -- DrawingPictures.hs 2 | 3 | 4 | import Graphics.UI.GLUT 5 | import Control.Monad 6 | 7 | my_points :: [(GLfloat,GLfloat,GLfloat)] 8 | my_points = [ (sin (2*pi*k/12), cos (2*pi*k/12), 0) | k <- [1..12] ] 9 | 10 | main :: IO () 11 | main = do 12 | (_progName, _args) <- getArgsAndInitialize 13 | _ <- createWindow "" 14 | displayCallback $= display 15 | mainLoop 16 | 17 | display :: DisplayCallback 18 | display = do 19 | clear [ColorBuffer] 20 | renderPrimitive LineLoop $ 21 | mapM_ (\(x, y, z) -> vertex $ Vertex3 x y z) my_points 22 | forM_ my_points $ \p -> renderCircle 0.1 p 23 | flush 24 | 25 | renderCircle r (x,y,z) = do 26 | let circle_points = [ (x + r * cos t, y + r * sin t, z) | t <- [0, 0.2 .. 6.28] ] 27 | renderPrimitive Polygon $ 28 | mapM_ (\(x,y,z) -> vertex $ Vertex3 x y z) circle_points 29 | -------------------------------------------------------------------------------- /OpenGL/HelloWorld.hs: -------------------------------------------------------------------------------- 1 | import Graphics.UI.GLUT 2 | 3 | main :: IO () 4 | main = do 5 | (_progName, _args) <- getArgsAndInitialize 6 | _window <- createWindow "Hello World" 7 | displayCallback $= display 8 | mainLoop 9 | 10 | display :: DisplayCallback 11 | display = do 12 | clear [ ColorBuffer ] 13 | flush 14 | -------------------------------------------------------------------------------- /PercolationTheory/Setup.hs: -------------------------------------------------------------------------------- 1 | import Distribution.Simple 2 | main = defaultMain 3 | -------------------------------------------------------------------------------- /PercolationTheory/app/Main.hs: -------------------------------------------------------------------------------- 1 | module Main where 2 | 3 | import Lib 4 | 5 | main :: IO () 6 | main = someFunc 7 | -------------------------------------------------------------------------------- /PercolationTheory/src/Lib.hs: -------------------------------------------------------------------------------- 1 | module Lib 2 | ( someFunc 3 | ) where 4 | 5 | someFunc :: IO () 6 | someFunc = putStrLn "someFunc" 7 | -------------------------------------------------------------------------------- /PercolationTheory/test/Spec.hs: -------------------------------------------------------------------------------- 1 | main :: IO () 2 | main = putStrLn "Test suite not yet implemented" 3 | -------------------------------------------------------------------------------- /Poker/Data.hs: -------------------------------------------------------------------------------- 1 | module Poker.Data where 2 | 3 | data Rank = Two 4 | | Three 5 | | Four 6 | | Five 7 | | Six 8 | | Seven 9 | | Eight 10 | | Nine 11 | | Ten 12 | | Jack 13 | | Queen 14 | | King 15 | | Ace 16 | deriving (Eq, Ord, Show) 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | haskell 2 | ======= 3 | 4 | Personal haskell programs 5 | -------------------------------------------------------------------------------- /Rosalind/DNA.in: -------------------------------------------------------------------------------- 1 | TCCTAGTTTGGCCAGGACATCAATGCTTGGTTAATTCTGATGCAGCCCCCCAATCCTTGGTACTGACGAAAGCCCAATCTCTTGTGAGGGCCGCAAATACCTGCGTATAGACAGAATTATACGTGCCGTACTGGATGTGGCTGACCTGCAATGACCAGTTTGACGGTCTTTTTCGCGGCTGGTTAGTGGTCGTACAGCTGAGAACGGCCGCCGAACGCCTTCGCGGCAGCTATTCAATCGCATCGCGTAAGGCGTAACTGAGCATTACTGTAGTGTTCCGCTAGCTCAGCGCAGCAGTCGAACAGCCCATACGCATAGTATACATTTTTTCGTCCTACAACAGCAAGTGGTGTCTCGCCAGTTATAGGAGACGATAGATGGGTGGTCAGCCAAACTCGCCATTGTTTTGATATAGGATCTACCGGTCGATCATCTTCTAAGTCACTACCTGTAGATCGGGGGGTTCCAGGGCAGCATGTCCCTCCCTCCAGCTTTCTGTGGATAAATCTGCCGACTATAGTTTGAGGGGGCTACTGCGACCCCGCTTGGGGCTCGACGGCCGTGAGTTAATGACGCTCAATGGACAAAGTGCATTTTCGCACGTGACGATCGTTAAAATGCCTTACACTCCAGCGGCTTAAGCGGAGCAGGTCCAATAGCGCTTAGGCGGCCCGCAGTTGTTATCAAGGATCGTTAGAGGGATTGCGGCTACACGAAGGTTAAGAGAATCGCATTCAGCTAAAACTTGATACTCATTACACGTCACGTGCTCTTAGTAGCACTACCCGCCCGGTAGTGCGCTGCCCGCTGAGTTGTGGGTAGCGGCCTTCTTAGCAGCTCGCACGCTGCTGTGTTTGCTTCGATGAGCAGGGGTGAACCAAGTCTATAGGGACGGAAAACCGGTGCCTTGAGTTGGCCTCA 2 | -------------------------------------------------------------------------------- /Rosalind/LCSM.out: -------------------------------------------------------------------------------- 1 | CGTGCCATGAATAGACTTCCTTCACGCCACCTAACTAAATGC 2 | -------------------------------------------------------------------------------- /Rosalind/LEXF.in: -------------------------------------------------------------------------------- 1 | X L Z D T C G A 2 | 3 3 | -------------------------------------------------------------------------------- /Rosalind/LEXV.hs: -------------------------------------------------------------------------------- 1 | -- LEXV.hs 2 | {- 3 | - Given an alphabet, A = {a1, ..., am}, and a length k, form all 4 | - strings of length [1..k] and output them in lexicographical order 5 | -} 6 | 7 | import System.Environment (getArgs) 8 | 9 | main = do 10 | args <- getArgs 11 | txt <- readFile (head args) 12 | putStrLn $ solveProblem txt 13 | 14 | solveProblem txt = let (alphabet, k) = parseInput txt 15 | ans = tail $ preTraversal k alphabet 16 | in unlines ans 17 | 18 | parseInput txt = let [ alphabet_str, k_str ] = lines txt 19 | alphabet = concat $ words alphabet_str 20 | k = read k_str 21 | in (alphabet, k) 22 | 23 | preTraversal :: Int -> String -> [String] 24 | preTraversal 0 alph = [""] 25 | preTraversal k alph = "" : [ x : str | x <- alph, str <- preTraversal (k-1) alph ] 26 | -------------------------------------------------------------------------------- /Rosalind/LEXV.in: -------------------------------------------------------------------------------- 1 | Y S T C W R Z P V F 2 | 3 3 | -------------------------------------------------------------------------------- /Rosalind/MRNA.in: -------------------------------------------------------------------------------- 1 | MGCRCEPYRVHGWAWNIQRYFESNRSNHLHPFWASLDEENTDTHPNPLSCCEDTAVCEALMWGSSAFLQDEVQIEMCGYHWWYYILSLIEIDGEGECSAKCTDMDYDTHHCCMDKITIDQTLEVFGSLVMNHFRAMMIELFDWHCKVNCVECACNIPNYMNWWEVFPYKFNVRMCTGSFLYSNTVFKQLNYCVTIDSDYGGYCNTSWNNDMYCHMAGEHPAAYGFPPANSANCGKWQQWMISQRSLIKVMYEISPDERVWVIQNVWADVLMTDIRADDWAIWLVSNRTTPMNQHHKRFDMLHWVCEWLKKTKSEWSYVLRTKNEYYATSPLARIRVGNVMVYVMWAHLCMHHNAVAIPVKPNNTKCEETYWGKILCIQRDKNTNRNLKQGWAPCFNGGAGFDDHINHMCLEHGAMLPYVARTHMVGVMEHFTQPQTWYLISFFLSVFGELEWQGDFTTDWINPCQRHYISDWHTMVFRCIWARNDWWKMANEIHMYCGIFDCILLMYGLQRYGLRRYLGEWRTFEWIWVWKYAFFPIVNAFEEGTDWMSWLPFPIFFMDTSHGPEYCKLDAGNLHADTVGVYMKFKGLGQIALKMGSVWCYIQHKGSMESLHGVMPWVAGWWECKDISRFEYHMYHMTILIKYWPVCEFWFFPQIMLAYSYEVSAFQLERRMWPTTPEIILVPAFSTNFPAEGSAFNDKYLHEDGWLEDRDRIAHALWHHKLRSPFQLKWHFMIMLDHYDWDKKQDNSRHQILEPAGHRFKYAMKGYPLMQKEWEGHLEMWTNASKIKMRIRMKVIYRLDHHKTISEDHAGNAPTDGIINWDFACCFGAWTFDRYTGEKLLEMNTSRGYPYCYHPHRAVFAKAYRRMEWMWDHQYNHIRNCIHSLEHKSCDAITRVDWCSGNKHTTPKFHDRMGWPEQKDSITTVQWFNQHNITNCTGHKQYKCAVFHMRLELAEPAMRLFFNTDKSQNDVIQLKKPLKKLYFPWFEPAFQTMWSFP 2 | -------------------------------------------------------------------------------- /Rosalind/PDST.out: -------------------------------------------------------------------------------- 1 | 0.0 0.309 0.573 0.595 0.478 0.595 0.601 0.481 0.559 2 | 0.309 0.0 0.48 0.539 0.334 0.515 0.535 0.335 0.495 3 | 0.573 0.48 0.0 0.283 0.318 0.64 0.29 0.585 0.63 4 | 0.595 0.539 0.283 0.0 0.46 0.657 0.453 0.612 0.642 5 | 0.478 0.334 0.318 0.46 0.0 0.602 0.47 0.528 0.603 6 | 0.595 0.515 0.64 0.657 0.602 0.0 0.652 0.332 0.464 7 | 0.601 0.535 0.29 0.453 0.47 0.652 0.0 0.607 0.648 8 | 0.481 0.335 0.585 0.612 0.528 0.332 0.607 0.0 0.306 9 | 0.559 0.495 0.63 0.642 0.603 0.464 0.648 0.306 0.0 10 | 11 | -------------------------------------------------------------------------------- /Rosalind/PERM.hs: -------------------------------------------------------------------------------- 1 | -- PERM.hs 2 | {- 3 | - A permutation of length n is an ordering of the positive integers {1,2,…,n}. For example, π=(5,3,2,1,4) is a permutation of length 5. 4 | - 5 | - Given: A positive integer n≤7. 6 | - 7 | - Return: The total number of permutations of length n, followed by a list of all such permutations (in any order). 8 | - Sample Dataset 9 | - 10 | - 3 11 | - 12 | - Sample Output 13 | - 14 | - 6 15 | - 1 2 3 16 | - 1 3 2 17 | - 2 1 3 18 | - 2 3 1 19 | - 3 1 2 20 | - 3 2 1 21 | -} 22 | 23 | import Data.List (permutations) 24 | import System.Environment (getArgs) 25 | 26 | main = do 27 | args <- getArgs 28 | let n = read (head args) 29 | let ps = permutations [1..n] 30 | print $ length ps 31 | putStr $ unlines [ unwords $ map show p | p <- ps ] 32 | -------------------------------------------------------------------------------- /Rosalind/PPER.hs: -------------------------------------------------------------------------------- 1 | -- PPER.hs 2 | {- 3 | - Given n objects, how many ways can you choose-and-permute exactly k of them? 4 | - Return your answer mod 10^6 5 | -} 6 | 7 | {- 8 | - We want Binomial[n,k] * k! == n! / k! (n-k)! * k! == n! / (n-k)! == fallingFactorial n k 9 | -} 10 | 11 | import System.Environment (getArgs) 12 | 13 | main = do 14 | args <- getArgs 15 | let [n,k] = map read args 16 | print $ (n `falling` k) `mod` 10^6 17 | 18 | n `falling` k = product [n-k+1 .. n] 19 | -------------------------------------------------------------------------------- /Rosalind/PROB.in: -------------------------------------------------------------------------------- 1 | TTGTTAGGAGTGAGCTATAGTAAGCTATCCAGTCTGTGGCGCCCGTCGGTAGCACGATCCTTCTTTCCCAAGACGCAGACTTAGTATAAAAGTGT 2 | 0.062 0.161 0.199 0.279 0.337 0.383 0.418 0.498 0.539 0.641 0.668 0.761 0.821 0.840 0.894 3 | -------------------------------------------------------------------------------- /Rosalind/PROB.out: -------------------------------------------------------------------------------- 1 | -84.33008164170363 -68.10258712521001 -64.96783534542956 -60.64889720479488 -58.77882762862932 -57.83964655782804 -57.39876767570726 -57.18734330663994 -57.49130888519261 -59.53451583081559 -60.42600459147678 -65.01564498599558 -69.80975596870877 -71.79928258249728 -79.52236797902344 2 | -------------------------------------------------------------------------------- /Rosalind/PRTM.in: -------------------------------------------------------------------------------- 1 | EWISPYCDQHNPSQKIMHKYYLNKNAANYCTICFYDHPNHLAPARSSRAKLLMTWNEDSGYNCMPCHNHEATKGRVSRSAQVRWYFMFDDTVSLQKDDTTWFYNYSLLMPARAFNHWAVEALQKPFYPEMMLTWGQAHRGMMQNHSWISKCVVHADEEMYIQHSHAVWHGSKLMPIIECAKNFCVWWYGPQAFDKINVKEVFMMHPHNQAWKYQTHTVKVDRKIHVWLPNGDIYHGMFRHSYADWKGTVWNIATSVRDNDDDDRHREQYGMKYDIGHWSKNKRGRTSNKPTFLLIDKQDESWDPPRWNGGIWLCCGTIRKTAEPPFGSTIGCWPKMKRPPYQWWVTLMCEAMMYRFGGKHLARAFCPQLKKSFMSVAMATEEKFQDQWDTFNEISQCIYPNVFVNVSHHCTEDACYPRFSLYHPHWDALITPMEWPVHLMWIHKHRLEDNLIYIMQQPRQQCDQHNDVHQKRGNDGGTCQLFDDIWVSFPGSSGCDELLYASESHAQFWPDMYRQATSICKVILQLAKWHQMKHRHYVQWKDKYWCVPLKTWWNKEEIWKEGAPNHENIDWTLGDYIQWMLLMCCRACNWFFLVADRLFSAAKASSSHHTLAAVWRFDYRTLEFCWAEDDDYSSMATGNAGITWMDGCMKNDCNPMKIDETNKVNHTQRVLACAWHWYHPTANVSFRWDSPPDDAVMLTYQQCWAYEWLFYRYHAHYQHVIMMVNRNNVGPGYSMDEPVDKLIAWLEQANMVKMSYPPMIKVMMRTHVPQERAYGARVGAYVNLVFSVNAMLENVIKPMLQSQEYTVNVMFQGANFWCFSQRFTIHIKVKQQDNMKVYEDTYDPDDMLIIFWYMYKLYSVKTESAHWDQTYTMNNFNWQRCLFCMLNFTFPKENYLNEICRLLGDWMFYWVCRCACWPCEYHWAMGMGRLMRVIHIPLWKNFYFSTMWTIFYIH 2 | -------------------------------------------------------------------------------- /Rosalind/PRTM.out: -------------------------------------------------------------------------------- 1 | 114145.74820000051 2 | 3 | -------------------------------------------------------------------------------- /Rosalind/REVC.in: -------------------------------------------------------------------------------- 1 | GCAGGGGGGTCATGTGGGCGGTCACAAGCTTTATAGCGAGTCTTATGTTGATTGGACTCTTGGCTGGAACAACGACCAAGCCGTAAACTTGGTCTTCTTTGTCTAGAATGGTCTACCCTTAGGCGAGCTGACGTTTCCGCCTGATAGTCGGACAAGCACTGTTAAAAGTCTCTCGTTTACATACTTTGTTCCGTGATCTAATCAGAAAAATATAGTAATCTCGTGCCGACCCTTACGAGGCAGGCTAGGAGGCATCGACGGTGGTTTCCCGCTATATAGTAAGAGAGATCGGGAAGCAGCCAGTGCAAGGATACTGTCTAGCGCTCGCACATCAGACGTACCCTCAGAGACTTTCAGCTGAGACATTAGAGAAATCTTTCATAGAGCCTCTAATGGTTTCATGAGAGAAAATAAATTATCTCCAGTTCCATCGGAAATACGTGCTGATCAAGGAAGCGGATACTCAGAACCCGGCGTTATCATCTGGGGAGATGTTTACGTACAAGGAGAACATAACTCAGCCGTCGACCTCAGTGCAGCAATAAGACAGAGCAGAGTCGGCCCTCATGAATTACGGATACCATACGCGCACACCTAGGTGGGATGGAAGCTAATATCGTAGGCTCTGCAGCAGTAGAGCGCGTGAAATCCGGGTCGTGCAGTAATGACTAGGTTTCATCAGGACGCATGAAGCGTCCGGCCGATTCTTCCGTGCATGAGACTCTTGTGCGAGAGGTGCCACGATGTGCCCGTTTATTGGCAAGGCCAAGTACGGCCAGCTCTATATTGTCAGACACCGGCAAATCAATTCTGGTTCAAGATGCCACGTTCCGTCAGATAGGCTCCCACGTAGACCCAGATCAGGTCCCTAACCGACCGCTATCCGCGGCGTCACAACAAACACAACGCGCTCCAAGTGAGGCACGAAAGATCGGACTCAGGGACTGATTCTGTTGACCTAAGTGTGAAGTCCCTTGT 2 | -------------------------------------------------------------------------------- /Rosalind/RNA.hs: -------------------------------------------------------------------------------- 1 | -- RNA.hs 2 | {- 3 | - Problem 4 | - 5 | - An RNA string is a string formed from the alphabet containing 'A', 'C', 'G', and 'U'. 6 | - 7 | - Given a DNA string t corresponding to a coding strand, its transcribed RNA string u is formed by replacing all occurrences of 'T' in t with 'U' in u. 8 | - 9 | - Given: A DNA string t having length at most 1000 nt. 10 | - 11 | - Return: The transcribed RNA string of t. 12 | - Sample Dataset 13 | - 14 | - GATGGAACTTGACTACGTAAATT 15 | - 16 | - Sample Output 17 | - 18 | - GAUGGAACUUGACUACGUAAAUU 19 | -} 20 | 21 | import System.Environment (getArgs) 22 | import Data.Text (pack, unpack, replace, singleton) 23 | 24 | main = do 25 | args <- getArgs 26 | txt <- readFile (head args) 27 | putStrLn $ solveProblem txt 28 | 29 | solveProblem txt = let inp = pack $ concat $ lines txt 30 | ans = replace (singleton 'T') (singleton 'U') inp 31 | in unpack ans 32 | 33 | -------------------------------------------------------------------------------- /Rosalind/RNA.in: -------------------------------------------------------------------------------- 1 | GGCATAGTGTTTGTGCCGGTCTTCTATACAACACCCTAACTTAGGTAGCACGCATCAGAGTCATCTGATGAGAATTCTTTTTATACAATCCTGCACTATGAAAGGGCAACCGATCAGACCTTGTGAAAGTAGGCGCAGACAAAGGTAGAGTTTGAATGAGGGATTCTGGACCTGCCTGGGTAGTTATGGAGTCGATCAAGGGCGAGCTCGACTCAGCGCTTATTTCCAAATCTAACTCCTGTATCATGCACCACGCCTTCGGAAGGGCTAGGAAATAACCCCATCCGAATGGACCGTCGTTATATTTGCGAGCTAGTGGAAAGCGACCAGGCAGTTTTGGCGGATTCCGGCCATACCCTTGCAAGTGTAGCAAAGCGAAATGCCGAGTAATGTCGGTAGCGGAACAATTCAACCTCTCATGCTTTGAATGTTTCACCCGCTCGAACTCTGACCTTTCCAGACGACAGCCGCGGAGCAGGAGTAGGTCTCTGACGGACACTAGTAGTATGACCCTTACGAGGATACGCTACCCTACTAGACAGTAGAGCACGCCACTCAGACTGAACGATTACACAACAGCGCGGACCTGAGCTCACAGGCGAGATCTGATGACCTTTCCTGGTTCCTTCGCTGGTGTTGGGGGTTCCCGCCCCGTTTGTCGTCAGGCGTCAGTCAGCAGCATCTCCCGTATATAGGCTAATACCAGCCATTCGTCGATAAAACGTAGAGGCAACAGCCTGAAGCATTGCTCAGCGGGGATCATCTGGAAGTTTCCCGGAAGTTCCTGCCAAACGAACGGCGGCGAGGGGTGCGGTTTTAGAGTGTACTATTTACATTACAACCTGAGTTGCTCACGCCAGGTGTAGGTTGATAAGACTCATCTAGCATCGGCGCAGTCAACATAGTGTTTCGATTCTGATCGAAACTAAGCCGAGGGTATTCC 2 | -------------------------------------------------------------------------------- /Rosalind/SSET.hs: -------------------------------------------------------------------------------- 1 | -- SSET.hs 2 | {- 3 | - Count the number of subsets of [1..n] for a given value of n. 4 | - Print the count, modulo 10^6 5 | -} 6 | 7 | import System.Environment (getArgs) 8 | 9 | main = do 10 | args <- getArgs 11 | let n = read (head args) 12 | print $ powerMod 2 n (10^6) 13 | 14 | powerMod x 0 _ = 1 15 | powerMod x k m = let t = powerMod x (k `quot` 2) m 16 | y = if odd k then x*t^2 else t^2 17 | in y `mod` m 18 | -------------------------------------------------------------------------------- /Rosalind/SUBS.in: -------------------------------------------------------------------------------- 1 | TAAAGTAGAGTGAGTTAAAGTATAAAGTAAATATCGAGTAAAGTAATTAAAGTAAAATAAAGTAATTAAAGTATGTGGAGCTAAAGTAGCCTTTAAAGTACCTAAAGTAGTCGTATTAAAGTAGCTAAAGTAGAAGTTAAAGTATCTAAAGTATAAAGTATAACTAAAGTAACACTATAAAGTAGCTTGTAAAGTATAAAGTATCTTCCTATTTCATAAAGTAGGTAAAGTAGGCTAAAGTACTACTAGTTAAAGTAGTGTAAATAAGTAAAGTACTTCAATTAAAGTAGTGGGGTTAAAGTAATAAAGTATAAAGTAGGTAAAGTAATAAAGTAACTAAAGTAAGGTAGGTAAAGTATTAAAGTAAGGTAAAGTACTCTTGATGTAAAGTAGTAAAGTATAAAGTAATAAAGTAGAAATCGCATCTAAAGTAGTAAAGTAGGAGTGGTAAAGTAATGATAAAGTATTCTTAAAGTATAAAGTATAAAGTACCTTAAAGTATAAAGTAATAAAGTATAAAGTATAAAGTACAAATAAAGTATAAAGTAGACTAAAGTAATCTAAAGTATGCCTAAAGTAGTTCGGTTGGTTAAAGTAACTGTTATAAAGTATAAAGTATAAAGTAGTAAAGTAATTATAAAGTAGTATAAAGTATAAAGTATTTAAAGTAGTAAAGTATTAAAGTATCCCGCATTAAAGTATAAAGTATAAAGTAGTAAAGTACTAAAGTAATAAAGTATTAAAGTATCTAAAGTATAAAGTAAGGTAAAGTATAAAGTATAATAAAGTAGGGCTTAGTAAAGTATAAAGTATTGT 2 | TAAAGTATA 3 | -------------------------------------------------------------------------------- /Rosalind/template.hs: -------------------------------------------------------------------------------- 1 | import System.Environment (getArgs) 2 | 3 | main = do 4 | args <- getArgs 5 | txt <- readFile (head args) 6 | putStrLn $ solveProblem txt 7 | 8 | solveProblem txt = txt 9 | -------------------------------------------------------------------------------- /Secretary/Strategies.hs: -------------------------------------------------------------------------------- 1 | module Strategies 2 | ( eStop 3 | , sqrtStop 4 | ) where 5 | 6 | import Data.List (find) 7 | import Data.Maybe (fromMaybe) 8 | 9 | stop :: Int -> [Int] -> Int 10 | stop k xs = 11 | let 12 | (as, bs) = splitAt k xs 13 | bestFromAs = maximum as 14 | firstBRecord = find (>= bestFromAs) bs 15 | in fromMaybe (last xs) firstBRecord 16 | 17 | eStop xs = stop (round $ fromIntegral (length xs) / (exp 1)) xs 18 | sqrtStop xs = stop (round $ sqrt $ fromIntegral (length xs)) xs 19 | -------------------------------------------------------------------------------- /TopCoder/AlmostFibonacciKnapSack/input.txt: -------------------------------------------------------------------------------- 1 | 148 2 | 2 3 | 13 4 | 3 5 | 86267769395 6 | -------------------------------------------------------------------------------- /TopCoder/AlmostFibonacciKnapSack/output.txt: -------------------------------------------------------------------------------- 1 | 6 10 8 5 2 | 1 3 | 2 3 4 4 | 2 5 | 3 14 15 9 26 53 5 8 6 | -------------------------------------------------------------------------------- /TopCoder/CuttingJigsawPuzzle/1.in: -------------------------------------------------------------------------------- 1 | 2 4 2 | ABDC 3 | ABDC 4 | -------------------------------------------------------------------------------- /TopCoder/CuttingJigsawPuzzle/1.out: -------------------------------------------------------------------------------- 1 | 3 2 | 2 1 3 | -------------------------------------------------------------------------------- /TopCoder/CuttingJigsawPuzzle/2.in: -------------------------------------------------------------------------------- 1 | 2 6 2 | ABCCBA 3 | ABCCBA 4 | -------------------------------------------------------------------------------- /TopCoder/CuttingJigsawPuzzle/2.out: -------------------------------------------------------------------------------- 1 | 1 2 | 2 6 3 | -------------------------------------------------------------------------------- /TopCoder/Frog/1.in: -------------------------------------------------------------------------------- 1 | 6 7 2 | 14 3 | 2 1 4 | 6 6 5 | 4 2 6 | 2 5 7 | 2 6 8 | 2 7 9 | 3 4 10 | 6 1 11 | 6 2 12 | 2 3 13 | 6 3 14 | 6 4 15 | 6 5 16 | 6 7 17 | -------------------------------------------------------------------------------- /TopCoder/Frog/1.out: -------------------------------------------------------------------------------- 1 | 7 2 | -------------------------------------------------------------------------------- /TopCoder/Frog/2.in: -------------------------------------------------------------------------------- 1 | 6 7 2 | 18 3 | 1 1 4 | 6 2 5 | 3 5 6 | 1 5 7 | 4 7 8 | 1 2 9 | 1 4 10 | 1 6 11 | 1 7 12 | 2 1 13 | 2 3 14 | 2 6 15 | 4 2 16 | 4 4 17 | 4 5 18 | 5 4 19 | 5 5 20 | 6 6 21 | -------------------------------------------------------------------------------- /TopCoder/Frog/2.out: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /TopCoder/Frog/3.in: -------------------------------------------------------------------------------- 1 | 5 5 2 | 4 3 | 1 5 4 | 2 5 5 | 4 4 6 | 2 3 7 | -------------------------------------------------------------------------------- /TopCoder/Frog/3.out: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /TopCoder/Frog/4.out: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /TopCoder/Frog/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS = $(wildcard *.in) 2 | OUTPUTS = $(INPUTS:%.in=%.out) 3 | 4 | TESTS = $(INPUTS:%.in=test.%) 5 | TEST_ANSWERS = $(INPUTS:%.in=%.test) 6 | 7 | .INTERMEDIATE: $(TEST_ANSWERS) 8 | .PHONY: test all 9 | 10 | test: frog.exe $(TESTS) 11 | 12 | test.%: %.out %.test 13 | cmp $*.test $*.out 14 | 15 | %.test: %.in 16 | ./frog.exe < $< > $@ 17 | 18 | clean: 19 | rm $(TEST_ANSWERS) 20 | -------------------------------------------------------------------------------- /TopCoder/Frog/makeInput.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import sys 3 | from random import randint 4 | 5 | def main(): 6 | w = h = 10 7 | n = 50 8 | if len(sys.argv) >= 3: 9 | w, h = int(sys.argv[1]), int(sys.argv[2]) 10 | if len(sys.argv) == 4: 11 | n = int(sys.argv[3]) 12 | pts = set() 13 | while len(pts) < n: 14 | pts.add( (randint(1,w), randint(1,h)) ) 15 | 16 | print("%d %d"%(w,h)) 17 | print("%d"%n) 18 | for pt in pts: 19 | print("%d %d"%pt) 20 | 21 | if __name__ == "__main__": 22 | main() 23 | -------------------------------------------------------------------------------- /TopCoder/SumFullSet/SumFullSet.hs: -------------------------------------------------------------------------------- 1 | -- SumFullSet.hs 2 | 3 | import qualified System.Environment as Env 4 | import qualified Data.ByteString.Char8 as BS 5 | import Data.Maybe (fromJust) 6 | 7 | main = do 8 | args <- Env.getArgs 9 | inps <- fmap concat (mapM parseInput args) 10 | mapM_ putStrLn [ if isClosed inp then "closed" else "not closed" | inp <- inps ] 11 | 12 | parseInput :: String -> IO [[Int]] 13 | parseInput fn = do 14 | lns <- fmap BS.lines (BS.readFile fn) 15 | return [ map (fst . fromJust . BS.readInt) (BS.words ln) | ln <- lns ] 16 | 17 | isClosed :: [Int] -> Bool 18 | isClosed xs = all (`elem` xs) (allPairSums xs) 19 | 20 | allPairSums [] = [] 21 | allPairSums (x:xs) = map (+x) xs ++ allPairSums xs 22 | -------------------------------------------------------------------------------- /TopCoder/SumFullSet/input.txt: -------------------------------------------------------------------------------- 1 | -1 0 1 2 | -1 1 3 | 0 1 4 | 0 1 1 5 | 16 0 43 43 -36 -49 -46 -16 40 34 -43 -24 13 -48 45 19 12 0 43 6 26 -23 50 28 -3 21 46 45 -32 -41 0 -27 42 19 47 -36 -21 -1 5 -21 -28 -43 23 -26 -5 21 -41 16 -37 38 6 | 10 7 | -------------------------------------------------------------------------------- /TopCoder/SumFullSet/output.txt: -------------------------------------------------------------------------------- 1 | closed 2 | not closed 3 | closed 4 | not closed 5 | not closed 6 | closed 7 | -------------------------------------------------------------------------------- /TopCoder/WorldPeace/input.00: -------------------------------------------------------------------------------- 1 | 4 2 | 4 4 4 4 4 3 | -------------------------------------------------------------------------------- /TopCoder/WorldPeace/input.01: -------------------------------------------------------------------------------- 1 | 5 2 | 1 2 3 4 5 6 3 | -------------------------------------------------------------------------------- /TopCoder/WorldPeace/input.02: -------------------------------------------------------------------------------- 1 | 2 2 | 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 3 | -------------------------------------------------------------------------------- /TopCoder/WorldPeace/input.03: -------------------------------------------------------------------------------- 1 | 7 2 | 96 17 32 138 112 50 7 19 412 23 14 50 47 343 427 22 39 3 | -------------------------------------------------------------------------------- /TopCoder/WorldPeace/input.04: -------------------------------------------------------------------------------- 1 | 10 2 | 638074479 717901019 910893151 924124222 991874870 919392444 729973192 607898881 3 | 838529741 907090878 632877562 678638852 749258866 949661738 784641190 815740520 4 | 689809286 711327114 658017649 636727234 871088534 964608547 867960437 964911023 5 | 642411618 868318236 793328473 849540177 960039699 998262224 775720601 634685437 6 | 743766982 826321850 846671921 712570181 676890302 814283264 958273130 899003369 7 | 909973864 921987721 978601888 633027021 896400011 725078407 662183572 629843174 8 | 617774786 695823011 9 | -------------------------------------------------------------------------------- /TopCoder/WorldPeace/output.00: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /TopCoder/WorldPeace/output.01: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /TopCoder/WorldPeace/output.02: -------------------------------------------------------------------------------- 1 | 3000000000 2 | -------------------------------------------------------------------------------- /TopCoder/WorldPeace/output.03: -------------------------------------------------------------------------------- 1 | 166 2 | -------------------------------------------------------------------------------- /TopCoder/WorldPeace/output.04: -------------------------------------------------------------------------------- 1 | 3983180234 2 | -------------------------------------------------------------------------------- /VTY/Samples/Sample1.hs: -------------------------------------------------------------------------------- 1 | import Graphics.Vty.Widgets.All 2 | import qualified Data.Text as T 3 | 4 | main = do 5 | e <- editWidget 6 | ui <- centered e 7 | fg <- newFocusGroup 8 | addToFocusGroup fg e 9 | c <- newCollection 10 | addToCollection c ui fg 11 | 12 | e `onActivate` \this -> 13 | getEditText this >>= (error . ("You entered: " ++) . T.unpack) 14 | 15 | runUi c defaultContext 16 | -------------------------------------------------------------------------------- /VTY/Samples/Sample2.hs: -------------------------------------------------------------------------------- 1 | import Graphics.Vty.Widgets.All 2 | import Graphics.Vty.LLInput 3 | import Graphics.Vty 4 | import System.Exit (exitSuccess) 5 | import qualified Data.Text as T 6 | 7 | main = do 8 | tw <- plainText $ T.pack "foo" 9 | 10 | tw `onKeyPressed` \_ key _ -> 11 | if key == KASCII 'q' then exitSuccess 12 | else do tw `appendText` (T.pack "!") 13 | return True 14 | 15 | fg <- newFocusGroup 16 | addToFocusGroup fg tw 17 | 18 | c <- newCollection 19 | 20 | -- you can run show_tw to ensure that the collection, c, shows tw 21 | show_tw <- addToCollection c tw fg 22 | 23 | runUi c defaultContext 24 | -------------------------------------------------------------------------------- /crud-server/Setup.hs: -------------------------------------------------------------------------------- 1 | import Distribution.Simple 2 | main = defaultMain 3 | -------------------------------------------------------------------------------- /crud-server/app/Main.hs: -------------------------------------------------------------------------------- 1 | module Main where 2 | 3 | import Lib 4 | 5 | main :: IO () 6 | main = someFunc 7 | -------------------------------------------------------------------------------- /crud-server/src/DeltaSet.hs: -------------------------------------------------------------------------------- 1 | module DeltaSet where 2 | 3 | import qualified Data.Set as S 4 | import qualified DisjointSets as DJS 5 | 6 | data AddOrRemove = Added | Removed deriving (Eq, Show) 7 | newtype DeltaSet a = DeltaSet (DJS.DisjointSets a AddOrRemove) deriving (Show) 8 | 9 | empty :: DeltaSet a 10 | empty = DeltaSet DJS.empty 11 | 12 | added :: DeltaSet a -> S.Set a 13 | added (DeltaSet djs) = djs `DJS.elementsBy` Added 14 | 15 | removed :: DeltaSet a -> S.Set a 16 | removed (DeltaSet djs) = djs `DJS.elementsBy` Removed 17 | 18 | plus :: Ord a => DeltaSet a -> S.Set a -> DeltaSet a 19 | (DeltaSet djs) `plus` xs = DeltaSet (DJS.addToSet Added (S.elems xs) djs) 20 | 21 | minus :: Ord a => DeltaSet a -> S.Set a -> DeltaSet a 22 | (DeltaSet djs) `minus` xs = DeltaSet (DJS.addToSet Removed (S.elems xs) djs) 23 | -------------------------------------------------------------------------------- /crud-server/src/DisjointSets.hs: -------------------------------------------------------------------------------- 1 | module DisjointSets where 2 | 3 | import qualified Data.Map as M 4 | import qualified Data.Set as S 5 | 6 | newtype DisjointSets a t = DisjointSets (M.Map a t) deriving (Show) 7 | 8 | empty :: DisjointSets a t 9 | empty = DisjointSets M.empty 10 | 11 | addToSet :: Ord a => t -> [a] -> DisjointSets a t -> DisjointSets a t 12 | -- The order here is important, as M.union is left-biased, and we want to overwrite 13 | -- the values in `m` with the new (t, a) values 14 | addToSet t as (DisjointSets m) = DisjointSets $ M.union (M.fromList $ zip as (repeat t)) m 15 | 16 | elementsBy :: Eq t => DisjointSets a t -> t -> S.Set a 17 | elementsBy (DisjointSets m) t = 18 | M.keysSet $ M.filter (== t) m 19 | 20 | all :: DisjointSets a t -> S.Set a 21 | all (DisjointSets m) = M.keysSet m 22 | 23 | -------------------------------------------------------------------------------- /crud-server/src/Lib.hs: -------------------------------------------------------------------------------- 1 | module Lib 2 | ( someFunc 3 | ) where 4 | 5 | import qualified Data.Set as S 6 | import qualified DeltaSet as DS 7 | 8 | someFunc :: IO () 9 | someFunc = print $ DS.empty `DS.plus` (S.fromList [1,2,3]) `DS.minus` (S.fromList [2,4,6]) 10 | -------------------------------------------------------------------------------- /crud-server/test/Spec.hs: -------------------------------------------------------------------------------- 1 | main :: IO () 2 | main = putStrLn "Test suite not yet implemented" 3 | -------------------------------------------------------------------------------- /hello-world/LetWhereScope.hs: -------------------------------------------------------------------------------- 1 | foo x = let y = 2*x in bar y 2 | -- where bar z = y*x (does not compile, y is not in scope) 3 | where bar z = x*z 4 | -------------------------------------------------------------------------------- /hello-world/Simple.hs: -------------------------------------------------------------------------------- 1 | module Simple where 2 | 3 | foo = "foo" 4 | -------------------------------------------------------------------------------- /hello-world/SimpleImport.hs: -------------------------------------------------------------------------------- 1 | module Main where 2 | 3 | import Simple 4 | 5 | main = print Simple.foo 6 | -------------------------------------------------------------------------------- /hello-world/UserDefinedTypeclass.hs: -------------------------------------------------------------------------------- 1 | -- define a typeclass of things that can be "negated" 2 | class Foo a where 3 | foo :: a -> a 4 | 5 | data Weight = Weight Int deriving (Show, Eq, Ord) 6 | 7 | instance Foo Weight where 8 | foo (Weight w) = Weight (-w) 9 | -------------------------------------------------------------------------------- /hello-world/ackermann.hs: -------------------------------------------------------------------------------- 1 | -- ackermann.hs 2 | -- The Ackermann function 3 | 4 | -- a m n 5 | a 0 n = n+1 6 | a m 0 = a (m-1) 1 7 | a m n = a (m-1) (a m (n-1)) 8 | 9 | -- slightly optimized 10 | a' 1 n = n+2 11 | a' 2 n = 2*n+3 12 | a' 3 n = 2^(n+3)-3 13 | a' 0 n = n+1 14 | a' m 0 = a' (m-1) 1 15 | a' m n = a' (m-1) (a' m (n-1)) 16 | -------------------------------------------------------------------------------- /hello-world/aeson_example_0.hs: -------------------------------------------------------------------------------- 1 | import Data.Aeson 2 | import qualified Data.ByteString.Lazy as B 3 | 4 | main = do 5 | txt <- B.getContents 6 | print $ (decode txt :: Maybe Value) 7 | -------------------------------------------------------------------------------- /hello-world/alternatives_to_if.hs: -------------------------------------------------------------------------------- 1 | data Property = Even | Odd | GT10 | Positive | Negative | Square deriving (Show) 2 | 3 | properties1 xs = 4 | (if any even xs then [Even] else []) ++ 5 | (if any odd xs then [Odd] else []) ++ 6 | (if any (> 10) xs then [GT10] else []) ++ 7 | (if any (> 0) xs then [Positive] else []) ++ 8 | (if any (< 0) xs then [Negative] else []) ++ 9 | (if any isSquare xs then [Square] else []) 10 | 11 | properties2 xs = 12 | let predMap = [ (even, Even) 13 | , (odd, Odd) 14 | , ((> 10), GT10) 15 | , ((> 0), Positive) 16 | , ((< 0), Negative) 17 | , (isSquare, Square) 18 | ] 19 | in [ prop | (p, prop) <- predMap, any p xs ] 20 | 21 | isSquare x = 22 | let r = round $ sqrt $ fromIntegral x 23 | in r*r == x 24 | -------------------------------------------------------------------------------- /hello-world/art.hs: -------------------------------------------------------------------------------- 1 | genfun(0,r:rs) 2 | | r < 0.50 = (\(x,y) -> x) 3 | | otherwise = (\(x,y) -> y) 4 | 5 | genfun(d,r:rs) 6 | | r < 0.25 = (\(x,y) -> sin(x)) 7 | | r < 0.50 = (\(x,y) -> cos(x)) 8 | | r < 0.75 = (\(x,y) -> (x+y)/2) 9 | | otherwise = (\(x,y) -> x*y) 10 | -------------------------------------------------------------------------------- /hello-world/automatic_differentiation.hs: -------------------------------------------------------------------------------- 1 | data AD = AD { getX :: Double 2 | , getDX :: Double 3 | } deriving (Show, Eq, Ord) 4 | instance Num AD where 5 | (AD x dx) + (AD y dy) = AD (x+y) (dx+dy) 6 | (AD x dx) - (AD y dy) = AD (x-y) (dx-dy) 7 | (AD x dx) * (AD y dy) = AD (x*y) (x*dy + y*dx) 8 | abs (AD x dx) = AD (abs x) (abs dx) 9 | signum (AD x dx) = AD (signum x) (signum dx) 10 | fromInteger x = AD (fromInteger x) 0 11 | 12 | instance Fractional AD where 13 | (AD x dx) / (AD y dy) = AD (x/y) (dx/y - x/y^2*dy) 14 | fromRational x = AD (fromRational x) 0 15 | 16 | mySin :: Fractional a => a -> a 17 | mySin x = x - x^3/6 + x^5/120 - x^7/5040 18 | 19 | myExp :: Fractional a => a -> a 20 | myExp x = 1 + x + x^2/2 + x^3/6 + x^4/24 + x^5/120 + x^6/720 + x^7/5040 21 | 22 | d f x = getDX $ f (AD x 1.0) 23 | 24 | --example: 25 | cos = d mySin 26 | -------------------------------------------------------------------------------- /hello-world/caesar_cipher.hs: -------------------------------------------------------------------------------- 1 | import Data.Char (isAlpha, isLower, isUpper, chr, ord) 2 | 3 | encrypt :: Int -> String -> String 4 | encrypt shift = map $ shiftCharBy shift 5 | decrypt shift = map $ shiftCharBy (-shift) 6 | 7 | shiftCharBy shift ch 8 | | not (isAlpha ch) = ch 9 | | otherwise = let base = if isUpper ch then ord 'A' else ord 'a' 10 | offset = ord ch - base + shift 11 | in chr (base + offset `mod` 26) 12 | -------------------------------------------------------------------------------- /hello-world/class_exploration_1.hs: -------------------------------------------------------------------------------- 1 | class Plus a where 2 | (#) :: a -> a -> a 3 | (%) :: a -> a -> a 4 | 5 | data W = W Int deriving (Show) 6 | 7 | instance Plus W where 8 | W x # W y = W (x+y) 9 | W x % W y = W (x-y) 10 | -------------------------------------------------------------------------------- /hello-world/class_exploration_2.hs: -------------------------------------------------------------------------------- 1 | class Addable a where 2 | plus :: a -> a -> a 3 | minus :: a -> a -> a 4 | neg :: a -> a 5 | 6 | data Weight = Weight Int deriving (Show) 7 | data Value = Value Int deriving (Show) 8 | 9 | instance Addable Weight where 10 | Weight x `plus` Weight y = Weight (x+y) 11 | Weight x `minus` Weight y = Weight (x-y) 12 | neg (Weight x) = Weight (-x) 13 | 14 | instance Addable Value where 15 | Value x `plus` Value y = Value (x+y) 16 | Value x `minus` Value y = Value (x-y) 17 | neg (Value x) = Value (-x) 18 | -------------------------------------------------------------------------------- /hello-world/command_line_arguments.hs: -------------------------------------------------------------------------------- 1 | import System.Environment (getArgs) 2 | 3 | main = do 4 | args <- getArgs 5 | print args 6 | -------------------------------------------------------------------------------- /hello-world/data_map.hs: -------------------------------------------------------------------------------- 1 | -- data_map.hs 2 | {- 3 | - Simple program to learn how to use the built-in map type 4 | -} 5 | 6 | import qualified Data.Map as Map 7 | 8 | m = Map.fromList [("a", 5), ("b", 10) 9 | ,("c", 15), ("d", 20) 10 | ,("e", 25), ("f", 30)] 11 | 12 | main = print $ Map.lookup "e" m 13 | -------------------------------------------------------------------------------- /hello-world/dequeue.hs: -------------------------------------------------------------------------------- 1 | -- dequeue.hs 2 | {- 3 | - Exploration of the Data.Dequeue package 4 | -} 5 | 6 | import qualified Data.Dequeue as DQ 7 | 8 | main = do 9 | let q = DQ.fromList [1,2,3,4,5] :: DQ.BankersDequeue Int 10 | let (Just f, q') = DQ.popFront q 11 | let q'' = DQ.pushBack q' 6 12 | print f 13 | print q'' 14 | -------------------------------------------------------------------------------- /hello-world/euclid.hs: -------------------------------------------------------------------------------- 1 | g(m,0)=m 2 | g(m,n)=g(n,m`mod`n) 3 | -------------------------------------------------------------------------------- /hello-world/fact.hs: -------------------------------------------------------------------------------- 1 | fact 0 = 1 2 | fact n = n * fact (n-1) 3 | 4 | main = print (fact 100) 5 | -------------------------------------------------------------------------------- /hello-world/factor.hs: -------------------------------------------------------------------------------- 1 | -- divisors(n)=[k|k<-[1..n],n `mod` k==0] 2 | smallest_factor(n,i) = 3 | if (i*i>n) then n 4 | else (if n `mod` i==0 then i else smallest_factor(n,i+1)) 5 | factor(n) = 6 | let p = smallest_factor(n,2) in 7 | if p == n then [n] 8 | else p:factor(n `quot` p) 9 | -------------------------------------------------------------------------------- /hello-world/fib.hs: -------------------------------------------------------------------------------- 1 | fibs = 1:1:zipWith (+) fibs (tail fibs) 2 | -------------------------------------------------------------------------------- /hello-world/gadt_example_0.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE GADTs #-} 2 | data Foo a where 3 | Bar :: Foo a 4 | Baz :: a -> Foo a -> Foo a 5 | 6 | instance Show a => Show (Foo a) where 7 | show Bar = "" 8 | show (Baz f Bar) = show f 9 | show (Baz f r) = show f ++ " -> " ++ show r 10 | -------------------------------------------------------------------------------- /hello-world/golomb.hs: -------------------------------------------------------------------------------- 1 | -- golomb.hs 2 | {- 3 | - The Golomb's self-describing sequence {G(n)} is the only nondecreasing 4 | - sequence of natural numbers such that n appears exactly G(n) times in the 5 | - sequence. The values of G(n) for the first few n are 6 | - 7 | - n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 … 8 | - G(n) 1 2 2 3 3 4 4 4 5 5 5 6 6 6 6 … 9 | - 10 | - You are given that G(103) = 86, G(106) = 6137. 11 | -} 12 | 13 | g1 = tail g' 14 | where g' = [0,1,2,2] ++ concat [replicate (g' !! k) k | k <- [3..] ] 15 | 16 | -- g2 is MUCH slower than g1 17 | g2 = [1,2] ++ g' 3 [2] 18 | where g' k (c:cs) = c : g' (k+1) (cs ++ replicate c k) 19 | 20 | -- g3 is faster than g1 21 | g3 = tail g' 22 | where g' = [0,1,2,2] ++ concat [replicate z k | (z,k) <- zip (drop 3 g') [3..] ] 23 | 24 | main = print $ g3 !! (5*10^7) 25 | -------------------------------------------------------------------------------- /hello-world/helloworld.hs: -------------------------------------------------------------------------------- 1 | main = print "Hello, world!" 2 | -------------------------------------------------------------------------------- /hello-world/integer_partitions.hs: -------------------------------------------------------------------------------- 1 | import Data.List (sort) 2 | 3 | 4 | -- find all the partitions of n into exactly k elements of xs (repeats allowed) 5 | -- xs must be all positive integers 6 | partitions n k xs = partitions' n k (reverse $ sort xs) 7 | 8 | partitions' 0 0 xs = [[]] 9 | partitions' n 0 xs = [] 10 | partitions' n k [] = [] 11 | partitions' n k x:xs | k*x < n = [] 12 | | otherwise = let with = partitions' (n-x) (k-1) (x:xs) 13 | without = partitions' n k xs 14 | in (map (x:) with) ++ without 15 | 16 | main = print $ length $ partitions 70 10 [1..12] 17 | -------------------------------------------------------------------------------- /hello-world/lazy_state.hs: -------------------------------------------------------------------------------- 1 | -- lazy_state.hs 2 | {- 3 | - An exploration of "imperative-style" programming within Haskell, using 4 | - the State monad. 5 | -} 6 | 7 | import qualified Control.Monad.ST as STS 8 | import qualified Data.STRef as STRS 9 | 10 | import qualified Control.Monad.ST.Lazy as STL 11 | import qualified Data.STRef.Lazy as STRL 12 | 13 | main = do 14 | let n = 10^7 15 | print $ take 20 (lazyRange n) 16 | print $ take 20 (strictRange n) 17 | 18 | strictRange :: Int -> [Int] 19 | strictRange n = STS.runST $ STRS.newSTRef 0 >>= range' 20 | where 21 | range' xref = do 22 | x <- STRS.readSTRef xref 23 | if x >= n then return [] else do 24 | STRS.writeSTRef xref (x+1) 25 | xs <- range' xref 26 | return (x : xs) 27 | 28 | lazyRange :: Int -> [Int] 29 | lazyRange n = STL.runST $ STRL.newSTRef 0 >>= range' 30 | where 31 | range' xref = do 32 | x <- STRL.readSTRef xref 33 | if x >= n then return [] else do 34 | STRL.writeSTRef xref (x+1) 35 | xs <- range' xref 36 | return (x : xs) 37 | -------------------------------------------------------------------------------- /hello-world/length.hs: -------------------------------------------------------------------------------- 1 | len([])=0 2 | len(f:r)=1+len(r) 3 | leng(l)=foldr (\x y->1+y) 0 l 4 | main=print(length([1..100000])) 5 | -------------------------------------------------------------------------------- /hello-world/matrix_multiply.hs: -------------------------------------------------------------------------------- 1 | -- matrix_multiply.hs 2 | {- 3 | - Simple program to explore matrix multiplication in Haskell. 4 | -} 5 | 6 | 7 | import Data.List (transpose) 8 | import ProjectEuler.Util (chunks) 9 | 10 | dot v1 v2 = sum $ zipWith (*) v1 v2 11 | 12 | mmult m1 m2 = let rs1 = m1 -- rows of m1 13 | cs2 = transpose m2 -- columns of m2 14 | in [ [ dot r c | c <- cs2 ] | r <- rs1 ] 15 | 16 | t1 = chunks 4 [1..16] 17 | t2 = chunks 4 [16,15..1] 18 | -------------------------------------------------------------------------------- /hello-world/mirror_bst.hs: -------------------------------------------------------------------------------- 1 | data Tree a = Nil | Tree a (Tree a) (Tree a) 2 | instance Show a => Show (Tree a) where 3 | show Nil = "_" 4 | show (Tree v l r) = "[" ++ show v ++ "," ++ show l ++ "," ++ show r ++ "]" 5 | leaf n = Tree n Nil Nil 6 | 7 | mirror Nil = Nil 8 | mirror (Tree a l r) = Tree a (mirror r) (mirror l) 9 | -------------------------------------------------------------------------------- /hello-world/monad_exploration_1.hs: -------------------------------------------------------------------------------- 1 | -- monad_exploration.hs 2 | import Control.Monad 3 | 4 | {- Minimal definition of a monadic Wrapper -} 5 | data Wrapper a = Wrapper a deriving (Show, Eq, Ord) 6 | instance Monad Wrapper where 7 | return x = Wrapper x 8 | Wrapper x >>= f = f x 9 | 10 | 11 | {- To add two wrappers, we need to ``lift'' the (+) function -} 12 | main = do 13 | print $ liftM2 (+) (Wrapper 1) (Wrapper 2) 14 | print $ foldl1 (liftM2 (+)) [Wrapper i | i <- [1..10]] 15 | -------------------------------------------------------------------------------- /hello-world/nqueens.hs: -------------------------------------------------------------------------------- 1 | -- nqueens.hs 2 | {- 3 | - A generalization of the 8-queens puzzle 4 | - 5 | - A very useful exploration of what Monads can do to clean up code. 6 | -} 7 | 8 | import Control.Monad 9 | 10 | main = print $ head (nQueens' 20) 11 | 12 | nQueens n = nestM n (addOneQueen n) [] 13 | nQueens' n = nest n (concat . map (addOneQueen n)) [[]] 14 | 15 | isSafePosition qs new = not $ any (`canAttack` new) qs 16 | canAttack (r1,c1) (r2,c2) = r1 == r2 || c1 == c2 || abs (r2-r1) == abs (c2-c1) 17 | safePositions n qs = filter (isSafePosition qs) [(length qs+1,i) | i <- [1..n]] 18 | 19 | addOneQueen n qs = [ p:qs | p <- safePositions n qs ] 20 | 21 | nestM 0 _ a = return a 22 | nestM n m a = nestM (n-1) m a >>= m 23 | 24 | nest 0 _ x = x 25 | nest n f x = f $ nest (n-1) f x 26 | -------------------------------------------------------------------------------- /hello-world/pair_partitions.hs: -------------------------------------------------------------------------------- 1 | -- pair_partitions.hs 2 | -- Includes a function to split a list into two lists all possible ways 3 | 4 | 5 | import Data.List (subsequences, (\\)) 6 | 7 | pairPartitions xs = [ (a, xs \\ a) | a <- subsequences xs ] 8 | 9 | intListPartitions [] = [ ([],[]) ] 10 | intListPartitions (x:xs) = let ilps = intListPartitions xs 11 | in [ (i:a,(x-i):b) | (a,b) <- ilps, i <- [0..x] ] 12 | -------------------------------------------------------------------------------- /hello-world/powmod.hs: -------------------------------------------------------------------------------- 1 | powmod(x,0,m)=1 2 | powmod(x,n,m)= 3 | if odd(n) then (p*p*x)`mod`m else (p*p)`mod`m 4 | where p=powmod(x,n`quot`2,m) 5 | -------------------------------------------------------------------------------- /hello-world/primes_simple.hs: -------------------------------------------------------------------------------- 1 | import qualified ProjectEuler.Prime as Prime 2 | 3 | primes = 2:[n | n <- [3,5..], all (/= 0) [ n `mod` p | p <- divcans n ] ] 4 | where divcans n = takeWhile (\p -> p*p <= n) primes 5 | 6 | solveProblem n = sum $ take n primes 7 | 8 | main = print $ solveProblem (10^5) 9 | -------------------------------------------------------------------------------- /hello-world/pythagorean_triples.hs: -------------------------------------------------------------------------------- 1 | module PythagoreanTriples 2 | ( triples 3 | , triples_opt 4 | ) where 5 | 6 | is_pythag a b c = a*a + b*b == c*c 7 | 8 | triples n = [(a,b,c) | a<-[1..n], b<-[a..n], c<-[b..n], is_pythag a b c] 9 | 10 | triples_opt bound = [ (a,b,c) | m <- (takeWhile (\x -> x*x <= bound) [1..]) 11 | , n <- [1..m-1] 12 | , gcd (m*m-n*n) (2*m*n) == 1 13 | , k <- [1..bound `quot` (m*m+n*n)] 14 | , let a = k*(m*m - n*n) 15 | , let b = k*(2*m*n) 16 | , let c = k*(m*m + n*n) 17 | ] 18 | -------------------------------------------------------------------------------- /hello-world/queue.hs: -------------------------------------------------------------------------------- 1 | -- queue.hs 2 | {- 3 | - Example of how to implement a Queue using two lists. 4 | - Pretty clever, I guess. Amortized O(1) performance for both enqueue and 5 | - dequeue 6 | -} 7 | 8 | module Queue where 9 | 10 | data Queue a = Queue [a] [a] deriving (Show) 11 | 12 | empty = Queue [] [] 13 | 14 | enqueue :: Queue a -> a -> Queue a 15 | enqueue (Queue back front) v = Queue (v:back) front 16 | 17 | dequeue :: Queue a -> (Maybe a, Queue a) 18 | dequeue (Queue [] []) = (Nothing, Queue [] []) 19 | dequeue (Queue back (f:rest)) = (Just f, Queue back rest) 20 | dequeue (Queue back []) = dequeue (Queue [] (reverse back)) 21 | -------------------------------------------------------------------------------- /hello-world/quicksort.hs: -------------------------------------------------------------------------------- 1 | -- quicksort.hs 2 | 3 | {- 4 | - Basic implementation of a really inefficient quicksort. 5 | - Still stunningly fast (10k elements in ~0.3 seconds) 6 | -} 7 | 8 | import qualified System.Random as Rand 9 | 10 | quicksort [] = [] 11 | quicksort (pivot:rest) = let lo = filter (< pivot) rest 12 | hi = filter (> pivot) rest 13 | in (quicksort lo) ++ [pivot] ++ (quicksort hi) 14 | 15 | 16 | randomArray :: IO [Int] 17 | randomArray = do 18 | g <- Rand.getStdGen 19 | return (Rand.randomRs (1, 1000000000) g) 20 | 21 | main = do 22 | rands <- randomArray 23 | let shuffled_array = take 100000 rands 24 | print $ last (quicksort shuffled_array) 25 | -------------------------------------------------------------------------------- /hello-world/random_numbers.hs: -------------------------------------------------------------------------------- 1 | import System.Random.Mersenne 2 | import System.Environment 3 | import Data.Word 4 | 5 | main = do 6 | args <- getArgs 7 | let (n,s) = (read $ args !! 0, read $ args !! 1) :: (Int, Word32) 8 | rng <- newMTGen (Just s) 9 | rnds <- randoms rng :: IO [Double] 10 | print $ sum $ take n rnds 11 | -------------------------------------------------------------------------------- /hello-world/rex_hw01.hs: -------------------------------------------------------------------------------- 1 | pwr(m,0)=1 2 | pwr(m,n)= 3 | if n>0 4 | then m*pwr(m,n-1) 5 | else pwr(m,n+1)/m 6 | 7 | lg(n)= 8 | if n<=1 9 | then 0 10 | else 1+lg(n/2) 11 | 12 | twiddle(a:b:r)=b:a:r 13 | twiddle(l)=l 14 | 15 | enum(l)=enum_h(l,0) 16 | enum_h(x:xs,i)=(i,x):enum_h(xs,i+1) 17 | enum_h(_,_)=[] 18 | 19 | rem_all(e,x:xs)= 20 | if e==x 21 | then rem_all(e,xs) 22 | else x:rem_all(e,xs) 23 | rem_all(e,[])=[] 24 | 25 | uniq(x:xs)=x:rem_all(x,xs) 26 | uniq([])=0 27 | 28 | 29 | -------------------------------------------------------------------------------- /hello-world/roll.hs: -------------------------------------------------------------------------------- 1 | roll k xs = (take k xs):(roll k $ tail xs) 2 | -------------------------------------------------------------------------------- /hello-world/sequences.hs: -------------------------------------------------------------------------------- 1 | import Data.Sequence 2 | 3 | -- this is the most straightforward way I could get it to work 4 | myLength seq = case viewl seq of 5 | EmptyL -> 0 6 | x :< xs -> 1 + myLength xs 7 | 8 | 9 | -- This is how you do it using view patterns 10 | myLength2 (viewl -> EmptyL) = 0 11 | myLength2 (viewl -> x :< xs) = 1 + myLength2 xs 12 | -------------------------------------------------------------------------------- /hello-world/split_stream.hs: -------------------------------------------------------------------------------- 1 | splitStream (x:y:rest) = let (xs,ys) = splitStream rest 2 | in (x:xs, y:ys) 3 | -------------------------------------------------------------------------------- /hello-world/stdin.hs: -------------------------------------------------------------------------------- 1 | -- stdin.hs 2 | main = do 3 | txt <- getContents 4 | putStr txt 5 | -------------------------------------------------------------------------------- /hello-world/test_module.hs: -------------------------------------------------------------------------------- 1 | import qualified Fake.Foo as Foo 2 | import qualified Fake.Bar as Bar 3 | 4 | main = do 5 | print $ Foo.a 4 6 | print $ Foo.b 4 7 | print $ Foo.c 4 8 | print $ Bar.a 4 9 | print $ Bar.b 4 10 | print $ Bar.c 4 11 | -------------------------------------------------------------------------------- /hello-world/todigits.hs: -------------------------------------------------------------------------------- 1 | -- digits(n,b) :: Integer -> [Integer] 2 | 3 | digits 0 _ = [0] 4 | digits n b = reverse $ digits' n b 5 | 6 | digits' 0 _ = [] 7 | digits' n b = let (q,r) = quotRem n b 8 | in r:(digits' q b) 9 | -------------------------------------------------------------------------------- /hello-world/tree_level_sum.hs: -------------------------------------------------------------------------------- 1 | -- tree_level_sum.hs 2 | {- 3 | Given a tree, find the sum of all the elements at height H 4 | -} 5 | 6 | data Tree a = Tree a [Tree a] deriving (Show) 7 | 8 | getChildren (Tree _ children) = children 9 | getValue (Tree v _) = v 10 | 11 | type Height = Int 12 | 13 | -- porky is so-named because Richard came up with this solution 14 | porky :: Tree Int -> Height -> Int 15 | porky (Tree v children) 0 = v 16 | porky (Tree _ children) h = sum [ porky child (h-1) | child <- children ] 17 | 18 | -- This is the "breadth-first" traversal of the tree 19 | bfs :: Tree Int -> Height -> Int 20 | bfs tree h = 21 | let sumsByLevel = map (sum . map getValue) (levels tree) 22 | in sumsByLevel !! h 23 | 24 | levels :: Tree a -> [[Tree a]] 25 | levels tree = iterate (concatMap getChildren) [tree] 26 | -------------------------------------------------------------------------------- /hello-world/types_vs_data.hs: -------------------------------------------------------------------------------- 1 | data Value = Value Int deriving (Show, Eq, Ord) 2 | f :: Value -> Value 3 | f (Value x) = Value (x*x) 4 | 5 | type Weight = Int 6 | g :: Weight -> Weight 7 | g w = w*w 8 | -------------------------------------------------------------------------------- /parsec/parsec_example_0.hs: -------------------------------------------------------------------------------- 1 | -- parsec_example_0.hs 2 | {- 3 | - Simple parser example simply parses a list like 4 | - a,a,a,a,a,a,a,a 5 | - into a list of characters 6 | - ['a', 'a', ...] == "aaaaa..." 7 | -} 8 | 9 | import Text.ParserCombinators.Parsec 10 | 11 | line :: GenParser Char st [String] 12 | line = do 13 | result <- cells 14 | eol 15 | return result 16 | 17 | eol = char '\n' 18 | 19 | cells = do 20 | first <- cellContent 21 | next <- remainingCells 22 | return (first:next) 23 | 24 | remainingCells :: GenParser Char st [String] 25 | remainingCells = 26 | (char ',' >> cells) 27 | <|> (return []) 28 | 29 | cellContent :: GenParser Char st String 30 | cellContent = many (noneOf ",\n") 31 | 32 | parseLine input = parse line "(unknown)" input 33 | -------------------------------------------------------------------------------- /parsec/parsec_example_1.hs: -------------------------------------------------------------------------------- 1 | -- parsec_example_1.hs 2 | {- 3 | - Do (+), then (*) 4 | -} 5 | 6 | import Text.ParserCombinators.Parsec 7 | 8 | line = adds 9 | 10 | adds = do first <- add 11 | next <- moreAdds 12 | return (first:next) 13 | 14 | moreAdds = (char '+' >> adds) 15 | <|> (return []) 16 | 17 | add = prods 18 | 19 | prods = do first <- prod 20 | next <- moreProds 21 | return (first:next) 22 | 23 | prod :: GenParser Char st Int 24 | prod = do spaces 25 | ans <- many digit 26 | spaces 27 | return (read ans) 28 | 29 | moreProds = (char '*' >> prods) 30 | <|> (return []) 31 | 32 | parseLine input = parse line "(unknown)" input 33 | 34 | 35 | evaluate tree = sum $ map product tree 36 | -------------------------------------------------------------------------------- /parsec/parsec_example_2.hs: -------------------------------------------------------------------------------- 1 | -- parsec_example_2.hs 2 | {- 3 | - General arithmetic parser 4 | -} 5 | 6 | import Text.ParserCombinators.Parsec 7 | import Text.ParserCombinators.Parsec.Expr 8 | import Text.ParserCombinators.Parsec.Language 9 | import qualified Text.ParserCombinators.Parsec.Token as Token 10 | 11 | import Control.Monad 12 | 13 | data Expr = Const Integer | Plus Expr Expr | Minus Expr Expr deriving (Show) 14 | 15 | 16 | lexer = Token.makeTokenParser emptyDef 17 | reservedOp = Token.reservedOp lexer 18 | 19 | ops = [ [Infix (reservedOp "+" >> return Plus) AssocLeft] 20 | , [Infix (reservedOp "-" >> return Minus) AssocLeft] 21 | ] 22 | 23 | integer = Token.integer lexer 24 | term = liftM Const integer 25 | 26 | expr = buildExpressionParser ops term 27 | 28 | main = forever $ do 29 | ln <- getLine 30 | case parse expr "" ln of 31 | Left _ -> print "bad expr" 32 | Right tr -> print tr 33 | -------------------------------------------------------------------------------- /parsec/parsec_example_5.hs: -------------------------------------------------------------------------------- 1 | -- parsec_example_5.hs 2 | {- 3 | - A very simple, short example of parsing a CSV file, 4 | - taken from Real World Haskell 5 | -} 6 | 7 | import Text.ParserCombinators.Parsec 8 | 9 | csvFile = endBy line eol -- take `line' until you find `eol' 10 | line = sepBy cell (char ',') -- split up `cell' by `(char ',')' 11 | cell = many (noneOf ",\n") 12 | eol = char '\n' 13 | 14 | 15 | parseCSV :: String -> Either ParseError [[String]] 16 | parseCSV input = parse csvFile "(error!)" input 17 | -------------------------------------------------------------------------------- /parsec/parsec_example_6.hs: -------------------------------------------------------------------------------- 1 | -- parsec_example_6.hs 2 | {- 3 | - Try to parse 4 | - int: (double,double); (double,double); ...; (double,double) 5 | - 6 | -} 7 | 8 | import Text.ParserCombinators.Parsec 9 | 10 | pLine = do 11 | idx <- pIndex 12 | spaces 13 | char ':' 14 | pts <- pPoints 15 | return (idx, pts) 16 | 17 | pIndex = many (noneOf " :") 18 | pPoints = sepBy pPoint (char ';') 19 | pPoint = do 20 | spaces 21 | char '(' 22 | x <- many (noneOf ",") 23 | char ',' 24 | y <- many (noneOf ")") 25 | char ')' 26 | spaces 27 | return (x,y) 28 | 29 | 30 | parseLine :: String -> Either ParseError (String, [(String,String)]) 31 | parseLine input = parse pLine "(error!)" input 32 | -------------------------------------------------------------------------------- /poker/README.md: -------------------------------------------------------------------------------- 1 | placeholder to get Stack to stop complaining 2 | -------------------------------------------------------------------------------- /poker/Setup.hs: -------------------------------------------------------------------------------- 1 | import Distribution.Simple 2 | main = defaultMain 3 | -------------------------------------------------------------------------------- /poker/src/Lib.hs: -------------------------------------------------------------------------------- 1 | module Lib where 2 | 3 | someFunc :: IO () 4 | someFunc = putStrLn "someFunc" 5 | -------------------------------------------------------------------------------- /poker/test/Spec.hs: -------------------------------------------------------------------------------- 1 | main :: IO () 2 | main = putStrLn "Test suite not yet implemented" 3 | -------------------------------------------------------------------------------- /project-euler/README.md: -------------------------------------------------------------------------------- 1 | A bunch of Haskell programs to solve Project Euler problems 2 | 3 | To run a specific test, try 4 | `stack test --test-arguments "-p **/042"` 5 | -------------------------------------------------------------------------------- /project-euler/Setup.hs: -------------------------------------------------------------------------------- 1 | import Distribution.Simple 2 | main = defaultMain 3 | -------------------------------------------------------------------------------- /project-euler/app/Main.hs: -------------------------------------------------------------------------------- 1 | module Main where 2 | 3 | import qualified Problems.P001 as P001 4 | 5 | main :: IO () 6 | main = putStrLn (P001.solve) 7 | -------------------------------------------------------------------------------- /project-euler/doc/Problems/P036.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanpbrewster/haskell/6edd0afe234008a48b4871032dedfd143ca6e412/project-euler/doc/Problems/P036.pdf -------------------------------------------------------------------------------- /project-euler/doc/Problems/P072.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanpbrewster/haskell/6edd0afe234008a48b4871032dedfd143ca6e412/project-euler/doc/Problems/P072.pdf -------------------------------------------------------------------------------- /project-euler/doc/Problems/P073.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanpbrewster/haskell/6edd0afe234008a48b4871032dedfd143ca6e412/project-euler/doc/Problems/P073.pdf -------------------------------------------------------------------------------- /project-euler/src/Old/001.hs: -------------------------------------------------------------------------------- 1 | -- Project Euler problem 01 2 | -- Find the sum of all numbers, x <= 999, for which n%3 == 0 or n%5 == 0 3 | 4 | problem001 n = sum [ x | x <- [1..n], x `mod` 3 == 0 || x `mod` 5 == 0 ] 5 | 6 | 7 | -- 3 + 6 + 9 + ... = Sum[i*3,{i,1,n/3}] = 3*(n/3)*(n/3+1)/2 8 | problem001opt n = sigma n 3 + sigma n 5 - sigma n 15 9 | where 10 | sigma x k = let y = x `div` k in 11 | k*y*(y+1)`div`2 12 | 13 | main = print ans where ans = problem001 999 14 | -------------------------------------------------------------------------------- /project-euler/src/Old/002.hs: -------------------------------------------------------------------------------- 1 | -- Project Euler problem 01 2 | -- Find the sum of the even Fibonacci numbers <= 4,000,000 3 | 4 | fibs = 1:1: zipWith (+) fibs (tail fibs) 5 | 6 | problem002 n = sum $ takeWhile (<= n) $ filter (\x -> x `mod` 2 == 0) fibs 7 | 8 | main = print ans where ans = problem002 $ 4*10^6 9 | -------------------------------------------------------------------------------- /project-euler/src/Old/003.hs: -------------------------------------------------------------------------------- 1 | -- Project Euler problem 003 2 | -- Find the largest prime factor of 600,851,475,143 == 600851475143 3 | 4 | factorsGreaterThanK :: Integer -> Integer -> [Integer] 5 | factorsGreaterThanK n k | n == 1 = [] 6 | | k*k > n = [n] 7 | | n `mod` k == 0 = k : factorsGreaterThanK (n `div` k) k 8 | | otherwise = factorsGreaterThanK n $ k+1 9 | 10 | factors :: Integer -> [Integer] 11 | factors n = factorsGreaterThanK n 2 12 | 13 | problem003 n = maximum $ factors n 14 | 15 | main = print ans where ans = problem003 600851475143 16 | -------------------------------------------------------------------------------- /project-euler/src/Old/004.hs: -------------------------------------------------------------------------------- 1 | -- Project Euler problem 004 2 | -- Find the largest palindromic product of 3-digit numbers 3 | 4 | 5 | problem004 :: Integer -> Integer 6 | problem004 n = let lo = 10^(n-1) 7 | hi = 10^n 8 | in maximum [ x | a <- [lo..hi], b <- [a..hi], 9 | let x = a*b, (show x) == (reverse $ show x) ] 10 | 11 | main = print ans where ans = problem004 3 12 | -------------------------------------------------------------------------------- /project-euler/src/Old/005.hs: -------------------------------------------------------------------------------- 1 | -- Project Euler problem 005 2 | -- Find the LCM of {1,2,3,4,...,20} 3 | 4 | -- Note: lcm is built in 5 | problem005 :: Integer -> Integer 6 | problem005 n = foldr lcm 1 [1..n] 7 | 8 | main = print ans where ans = problem005 20 9 | -------------------------------------------------------------------------------- /project-euler/src/Old/006.hs: -------------------------------------------------------------------------------- 1 | -- Project Euler problem 006 2 | -- Evaluate (1+2+...+n)^2 - (1^2 + 2^2 + ... + n^2) for n=100 3 | 4 | problem006 :: Integer -> Integer 5 | problem006 n = sumsq - sqsum 6 | where sumsq = (n*(n+1) `div` 2)^2 7 | sqsum = (n*(n+1)*(2*n+1) `div` 6) 8 | 9 | main = print ans where ans = problem006 100 10 | -------------------------------------------------------------------------------- /project-euler/src/Old/007.hs: -------------------------------------------------------------------------------- 1 | -- Project Euler problem 007 2 | -- Find Prime[10001] 3 | 4 | 5 | -- noDivisors n L = any x in L such that x divides n 6 | noDivisors n (d:ds) | d*d > n = True 7 | | n `mod` d == 0 = False 8 | | otherwise = noDivisors n ds 9 | 10 | primes = 2:[ x | x <- [3,5..], isPrime x] 11 | isPrime n = noDivisors n primes 12 | 13 | problem007 n = primes !! (n-1) 14 | 15 | main = print ans where ans = problem007 10001 16 | -------------------------------------------------------------------------------- /project-euler/src/Old/008.hs: -------------------------------------------------------------------------------- 1 | -- Problem 008 2 | -- Find the largest product of 5 consecutive digits in Problem008.in 3 | import Data.Char 4 | 5 | -- group L n = a list of n-tuples made of elements in L 6 | group [] n = [] 7 | group digits n = (take n digits) : group (tail digits) n 8 | 9 | -- Read in a file, turn it into a big long string of digits 10 | -- Then group them into chunks and find the biggest product 11 | problem008 filename = do 12 | text <- readFile filename 13 | let digits = map digitToInt $ concat $ lines text 14 | return $ maximum $ map product $ group digits 5 15 | 16 | main = do 17 | ans <- problem008 "008.in" 18 | print ans 19 | -------------------------------------------------------------------------------- /project-euler/src/Old/009.hs: -------------------------------------------------------------------------------- 1 | -- Project Euler problem 009 2 | -- Find the Pythagorean triple (a,b,c) such that a+b+c == n, where n = 1000 3 | 4 | 5 | problem009 n = [ (a,b,c) | a <- [1..n], b <- [a..n], let c = n-a-b, a^2+b^2==c^2 ] 6 | 7 | main = print ans where ans = problem009 1000 8 | 9 | triples :: Integer -> [(Integer,Integer,Integer)] 10 | triples n = [ (a,b,c) | p <- [1..u], q <- [1..p-1], k <- [1..n `div` p], 11 | let a = k*(p^2-q^2), 12 | let b = k*(2*p*q), 13 | let c = k*(p^2+q^2), 14 | a+b+c == n ] 15 | where u = floor $ sqrt $ fromIntegral n 16 | 17 | problem009opt n = head $ triples n 18 | -------------------------------------------------------------------------------- /project-euler/src/Old/010.hs: -------------------------------------------------------------------------------- 1 | -- Project Euler problem 10 2 | -- Find the sum of all the primes < n, where n = 2,000,000 3 | -- 4 | -- NOTE: The definition in Primes is very inefficient. 5 | -- At some point you should update it to use some 6 | -- better data structures 7 | import Primes_TrialDivision 8 | 9 | problem010 n = sum $ takeWhile (< n) primes 10 | 11 | main = print ans where ans = problem010 $ 2*10^6 12 | -------------------------------------------------------------------------------- /project-euler/src/Old/013.hs: -------------------------------------------------------------------------------- 1 | -- Project Euler problem 13 2 | -- Find the first ten digits of the sum of the numbers in 013.in 3 | 4 | 5 | problem013 filename = do text <- readFile filename 6 | let nums = map read $ lines text 7 | return $ take 10 $ show $ sum nums 8 | 9 | main = do ans <- problem013 "013.in" 10 | print ans 11 | -------------------------------------------------------------------------------- /project-euler/src/Old/092.hs: -------------------------------------------------------------------------------- 1 | -- 092.hs 2 | {- 3 | - A number chain is created by continuously adding the square of the digits in 4 | - a number to form a new number until it has been seen before. 5 | - 6 | - For example, 7 | - 8 | - 44 → 32 → 13 → 10 → 1 → 1 9 | - 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 10 | - 11 | - Therefore any chain that arrives at 1 or 89 will become stuck in an endless 12 | - loop. What is most amazing is that EVERY starting number will eventually 13 | - arrive at 1 or 89. 14 | - 15 | - How many starting numbers below ten million will arrive at 89? 16 | -} 17 | 18 | import ProjectEuler.Math (integerDigits) 19 | 20 | endsWith89 1 = False 21 | endsWith89 89 = True 22 | endsWith89 n = endsWith89 (sum $ map (^2) $ integerDigits n) 23 | 24 | solveProblem bound = length $ filter endsWith89 [1..bound] 25 | 26 | main = print $ solveProblem (10^7) 27 | -------------------------------------------------------------------------------- /project-euler/src/Old/119.hs: -------------------------------------------------------------------------------- 1 | -- 119.hs 2 | {- 3 | - The number 512 is interesting because it is equal to the sum of its digits 4 | - raised to some power: 5 + 1 + 2 = 8, and 83 = 512. Another example of 5 | - a number with this property is 614656 = 284. 6 | - 7 | - We shall define an to be the nth term of this sequence and insist that 8 | - a number must contain at least two digits to have a sum. 9 | - 10 | - You are given that a2 = 512 and a10 = 614656. 11 | - 12 | - Find a30. 13 | -} 14 | 15 | import ProjectEuler.Util (mergeInf) 16 | import ProjectEuler.Math (integerDigits) 17 | 18 | main = print $ solveProblem 29 19 | 20 | solveProblem k = ans !! k 21 | 22 | ans = filter legit [10..] 23 | 24 | legit n = let ds = sum $ integerDigits n 25 | k = round $ log (fromIntegral n) / log (fromIntegral ds) 26 | in ds^k == n 27 | -------------------------------------------------------------------------------- /project-euler/src/Old/132.hs: -------------------------------------------------------------------------------- 1 | -- 132.hs 2 | {- 3 | - A number consisting entirely of ones is called a repunit. We shall define 4 | - R(k) to be a repunit of length k. 5 | - 6 | - For example, R(10) = 1111111111 = 11×41×271×9091, and the sum of these prime 7 | - factors is 9414. 8 | - 9 | - Find the sum of the first forty prime factors of R(109). 10 | -} 11 | 12 | {- 13 | - We know that A(p) is the smallest value of k such that p | R(k). 14 | - Thus, p | R(10^9) iff A(p) | 10^9 15 | - That is, if A(p) == k' (such that p | R(k')) then k must be a multiple of k' 16 | -} 17 | 18 | import qualified ProjectEuler.Prime as Prime 19 | 20 | -- (a n) stolen from 129.hs. Look there. 21 | a n = aH n 1 1 22 | aH n 0 k = k 23 | aH n r k = let r' = (10*r+1) `mod` n 24 | in aH n r' (k+1) 25 | 26 | solveProblem count k = 27 | let fs = filter (\p -> k `mod` (a p) == 0) (drop 3 Prime.primes) 28 | in take count fs 29 | 30 | main = print $ solveProblem 40 (10^9) 31 | -------------------------------------------------------------------------------- /project-euler/src/Old/179.hs: -------------------------------------------------------------------------------- 1 | -- 179.hs 2 | {- 3 | - Find the number of integers 1 < n < 10^7, for which n and n + 1 have the same 4 | - number of positive divisors. For example, 14 has the positive divisors 1, 2, 5 | - 7, 14 while 15 has 1, 3, 5, 15. 6 | -} 7 | 8 | {- 9 | - This is quite slow, but very beautiful. So called "executable specification". 10 | -} 11 | 12 | import ProjectEuler.Prime (sigma) 13 | import Data.List (group) 14 | 15 | 16 | main = print $ solveProblem (10^7) 17 | solveProblem bound = sum [ length g - 1 | g <- group $ map (sigma 0) [1..bound] ] 18 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P001.hs: -------------------------------------------------------------------------------- 1 | module Problems.P001 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - If we list all the natural numbers below 10 that are multiples of 3 or 5, 7 | - we get 3, 5, 6 and 9. The sum of these multiples is 23. 8 | - 9 | - Find the sum of all the multiples of 3 or 5 below 1000. 10 | -} 11 | -- Find the sum of all the multiples of {x1,x2,...} <= n 12 | solve :: String 13 | solve = show $ solveProblem [3, 5] (1000 - 1) 14 | 15 | solveProblem xs n = sum [i | i <- [1 .. n], or [i `mod` x == 0 | x <- xs]] 16 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P002.hs: -------------------------------------------------------------------------------- 1 | module Problems.P002 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - Each new term in the Fibonacci sequence is generated by adding the 7 | - previous two terms. By starting with 1 and 2, the first 10 terms will be: 8 | - 9 | - 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 10 | - 11 | - By considering the terms in the Fibonacci sequence whose values do not 12 | - exceed four million, find the sum of the even-valued terms. 13 | -} 14 | fib = 0 : 1 : zipWith (+) fib (tail fib) 15 | 16 | solveProblem bound = sum $ filter even $ takeWhile (< bound) fib 17 | 18 | solve = show $ solveProblem 4000000 19 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P003.hs: -------------------------------------------------------------------------------- 1 | module Problems.P003 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - The prime factors of 13195 are 5, 7, 13 and 29. 7 | - 8 | - What is the largest prime factor of the number 600851475143 ? 9 | -} 10 | factors 11 | :: Integral a 12 | => a -> [a] 13 | factors n = factorHelper n 2 14 | 15 | factorHelper 16 | :: Integral a 17 | => a -> a -> [a] 18 | factorHelper n k 19 | | k * k > n = [n] 20 | | n `mod` k == 0 = k : factorHelper (n `div` k) k 21 | | otherwise = factorHelper n (k + 1) 22 | 23 | largestFactor n = maximum (factors n) 24 | 25 | solve = show $ largestFactor 600851475143 26 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P004.hs: -------------------------------------------------------------------------------- 1 | module Problems.P004 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - A palindromic number reads the same both ways. The largest palindrome 7 | - made from the product of two 2-digit numbers is 9009 = 91*99. 8 | - 9 | - Find the largest palindrome made from the product of two 3-digit numbers. 10 | -} 11 | solve :: String 12 | solve = show $ solveProblem 3 13 | 14 | isPalindromic 15 | :: Show t 16 | => t -> Bool 17 | isPalindromic x = 18 | let s = show x 19 | in s == reverse s 20 | 21 | -- Find the largest palindrome made from the product of two `digit`-length 22 | -- numbers 23 | solveProblem digits = 24 | let lo = 10 ^ (digits - 1) 25 | hi = 10 ^ digits - 1 26 | in maximum 27 | [x | a <- [lo .. hi], b <- [a .. hi], let x = a * b, isPalindromic x] 28 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P005.hs: -------------------------------------------------------------------------------- 1 | module Problems.P005 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - 2520 is the smallest number that can be divided by each of the numbers 7 | - from 1 to 10 without any remainder. 8 | - 9 | - What is the smallest positive number that is evenly divisible by all of 10 | - the numbers from 1 to 20? 11 | -} 12 | solve :: String 13 | solve = show $ solveProblem 20 14 | 15 | solveProblem n = foldr lcm 1 [1 .. n] 16 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P006.hs: -------------------------------------------------------------------------------- 1 | module Problems.P006 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - The sum of the squares of the first ten natural numbers is, 7 | - 8 | - 12 + 22 + ... + 102 = 385 9 | - The square of the sum of the first ten natural numbers is, 10 | - 11 | - (1 + 2 + ... + 10)2 = 552 = 3025 12 | - 13 | - Hence the difference between the sum of the squares of the first ten 14 | - natural numbers and the square of the sum is 3025 385 = 2640. 15 | - 16 | - Find the difference between the sum of the squares of the first one 17 | - hundred natural numbers and the square of the sum. 18 | -} 19 | solve :: String 20 | solve = show $ solveProblem 100 21 | 22 | solveProblem n = sum [1 .. n] ^ 2 - sum [x ^ 2 | x <- [1 .. n]] 23 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P007.hs: -------------------------------------------------------------------------------- 1 | module Problems.P007 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can 7 | - see that the 6th prime is 13. 8 | - 9 | - What is the 10 001st prime number? 10 | -} 11 | solve :: String 12 | solve = show $ solveProblem 10001 13 | 14 | primes = 2 : filter isPrime [3,5 ..] 15 | 16 | isPrime n = noPrimeDivisors n primes 17 | where 18 | noPrimeDivisors n (k:ks) 19 | | n `mod` k == 0 = False 20 | | k * k > n = True 21 | | otherwise = noPrimeDivisors n ks 22 | 23 | solveProblem n = primes !! (n - 1) 24 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P009.hs: -------------------------------------------------------------------------------- 1 | module Problems.P009 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - A Pythagorean triplet is a set of three natural numbers, a b c, for which, 7 | - a^2 + b^2 = c^2 8 | - 9 | - For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. 10 | - 11 | - There exists exactly one Pythagorean triplet for which a + b + c = 1000. 12 | - Find the product abc. 13 | -} 14 | solve :: String 15 | solve = show $ solveProblem 1000 16 | 17 | triplets = 18 | [ (a, b, c) 19 | | m <- [1 ..] 20 | , n <- [1 .. m - 1] 21 | , let a = m * m - n * n 22 | , let b = 2 * m * n 23 | , let c = m * m + n * n 24 | , gcd a b == 1 25 | ] 26 | 27 | solveProblem :: Integer -> Integer 28 | solveProblem target = 29 | let (a, b, c) = 30 | head 31 | [ (a', b', c') 32 | | (a', b', c') <- triplets 33 | , target `mod` (a' + b' + c') == 0 34 | ] 35 | perimeter = a + b + c 36 | scale = target `div` perimeter 37 | in scale ^ 3 * a * b * c 38 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P010.hs: -------------------------------------------------------------------------------- 1 | module Problems.P010 2 | ( solve 3 | ) where 4 | 5 | import Data.Array ((!)) 6 | 7 | {- 8 | - The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. 9 | - Find the sum of all the primes below two million. 10 | -} 11 | import qualified Util.Prime as Prime 12 | 13 | solve :: String 14 | solve = show $ solveProblem 2e6 15 | 16 | solveProblem bound = sum $ filter isPrime [2 .. bound] 17 | where 18 | isPrime n = sieve ! n 19 | sieve = Prime.sieve bound 20 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P012.hs: -------------------------------------------------------------------------------- 1 | module Problems.P012 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - The sequence of triangle numbers is generated by adding the natural numbers. 7 | - So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The 8 | - first ten terms would be: 9 | - 10 | - 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... 11 | - 12 | - Let us list the factors of the first seven triangle numbers: 13 | - 14 | - 1: 1 15 | - 3: 1,3 16 | - 6: 1,2,3,6 17 | - 10: 1,2,5,10 18 | - 15: 1,3,5,15 19 | - 21: 1,3,7,21 20 | - 28: 1,2,4,7,14,28 21 | - 22 | - We can see that 28 is the first triangle number to have over five divisors. 23 | - 24 | - What is the value of the first triangle number to have over five hundred 25 | - divisors? 26 | -} 27 | import Util.Prime (sigma) 28 | 29 | solve :: String 30 | solve = show $ solveProblem 500 31 | 32 | solveProblem divisor_bound = 33 | let pents = map (\x -> x * (x + 1) `div` 2) [1 ..] 34 | legal p = sigma 0 p > divisor_bound 35 | answers = filter legal pents 36 | in head answers 37 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P013.hs: -------------------------------------------------------------------------------- 1 | module Problems.P013 2 | ( process 3 | ) where 4 | 5 | -- 013.hs 6 | type FileContents = String 7 | 8 | process :: FileContents -> String 9 | process txt = 10 | let ans = sum $ map read $ lines txt 11 | in take 10 $ show ans 12 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P014.hs: -------------------------------------------------------------------------------- 1 | module Problems.P014 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - Project Euler problem 14 7 | - Find the number, 1 <= k <= n, such that the Collatz sequence seeded by k is longest 8 | -} 9 | import Data.Array 10 | import Util.List (maximumBy) 11 | 12 | solve :: String 13 | solve = show $ fastSolve 1e6 14 | 15 | slowSolve :: Int -> Int 16 | slowSolve bound = maximumBy collatz [1 .. bound] 17 | 18 | fastSolve :: Int -> Int 19 | fastSolve bound = maximumBy (memo !) [1 .. bound] 20 | where 21 | memo = listArray (1, bound) (1 : map f [2 .. bound]) 22 | f n = 23 | let n' = next n 24 | in 1 + 25 | if n' <= bound 26 | then memo ! n' 27 | else f n' 28 | 29 | collatz :: Int -> Int 30 | collatz 1 = 1 31 | collatz n = 1 + collatz (next n) 32 | 33 | next :: Int -> Int 34 | next n = 35 | if even n 36 | then n `div` 2 37 | else 3 * n + 1 38 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P015.hs: -------------------------------------------------------------------------------- 1 | module Problems.P015 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - 015.hs 7 | - Project Euler problem 15 8 | - Find the number of paths from (0,0) to (a, b) moving only right and up 9 | - where a = b = 20 10 | -} 11 | solve :: String 12 | solve = show $ problem015 20 20 13 | 14 | -- Binom[n,k] = Fact[n]/(Fact[k]*Fact[n-k]) == (n*n-1*...*n-k+1)/(1*2*...*k) 15 | binom n k 16 | | k <= n - k = product [n - k + 1 .. n] `div` product [1 .. k] 17 | | otherwise = binom n (n - k) 18 | 19 | problem015 a b = binom (a + b) a 20 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P016.hs: -------------------------------------------------------------------------------- 1 | module Problems.P016 2 | ( solve 3 | ) where 4 | 5 | {- 016.hs 6 | - Project Euler problem 16 7 | - Find the sum of the base-b digits of n 8 | -} 9 | solve :: String 10 | solve = show $ problem016 (2 ^ 1000) 10 11 | 12 | problem016 n b = sum $ integerDigits n b 13 | 14 | integerDigits :: Integer -> Integer -> [Integer] 15 | integerDigits n b 16 | | b <= 1 = error $ "Trying to expand in base " ++ show b ++ " < 2" 17 | | n < b = [n] 18 | | otherwise = 19 | let (q, r) = n `divMod` b 20 | in integerDigits q b ++ [r] 21 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P020.hs: -------------------------------------------------------------------------------- 1 | module Problems.P020 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - Find the sum of the digits of 100! 7 | -} 8 | import Data.Char 9 | 10 | solve :: String 11 | solve = show $ problem020 100 12 | 13 | problem020 n = sum $ map digitToInt $ show $ product [1 .. n] 14 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P021.hs: -------------------------------------------------------------------------------- 1 | module Problems.P021 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - Project Euler problem 21 7 | - 8 | - Find the sum of all amicable numbers less than n, where n = 10,000 9 | - An amicable number is defined by the divisor sum function, 10 | - sigma1(k) = Sum[d, d divides k] 11 | - An amicable number is a member of an amicable pair 12 | - An amicable pair is a pair of numbers, (a,b), such that 13 | - sigma1(a) == b, and sigma1(b) == a 14 | -} 15 | import qualified Util.Prime as Prime 16 | 17 | solve :: String 18 | solve = show $ problem021 10000 19 | 20 | sig n = Prime.sigma 1 n - n 21 | 22 | problem021 n = 23 | sum [a + b | a <- [1 .. n], let b = sig a, a > b, b > 0, sig b == a] 24 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P022.hs: -------------------------------------------------------------------------------- 1 | module Problems.P022 2 | ( process 3 | ) where 4 | 5 | import Data.Char 6 | 7 | {- 8 | - Read in the file 022.in 9 | - Sort it 10 | - The i-th name has a score of i*value(name[i]) 11 | - where value("qza") = 17 + 26 + 1 12 | - Find the sum of the scores of the names 13 | -} 14 | import Data.List 15 | 16 | type FileContents = String 17 | 18 | process :: FileContents -> String 19 | process txt = show $ solveProblem txt 20 | 21 | solveProblem txt = 22 | let names = sort $ lines txt 23 | in sum [i * value (names !! (i - 1)) | i <- [1 .. length names]] 24 | 25 | value str = sum [ord ch - ord 'A' + 1 | ch <- str] 26 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P024.hs: -------------------------------------------------------------------------------- 1 | module Problems.P024 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - A permutation is an ordered arrangement of objects. For example, 3124 is one 7 | - possible permutation of the digits 1, 2, 3 and 4. If all of the permutations 8 | - are listed numerically or alphabetically, we call it lexicographic order. 9 | - The lexicographic permutations of 0, 1 and 2 are: 10 | - 11 | - 012 021 102 120 201 210 12 | - 13 | - What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 14 | - 5, 6, 7, 8 and 9? 15 | -} 16 | import Data.List (delete) 17 | 18 | solve :: String 19 | solve = nthPermutation (1000000 - 1) "0123456789" 20 | 21 | factorial n = product [1 .. n] 22 | 23 | -- Finds the n-th permutation of distinct items 24 | nthPermutation 0 xs = xs 25 | nthPermutation n xs = 26 | let r = factorial (length xs - 1) 27 | k = n `div` r 28 | e = xs !! k 29 | n' = n - k * r 30 | xs' = delete e xs 31 | in e : nthPermutation n' xs' 32 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P025.hs: -------------------------------------------------------------------------------- 1 | module Problems.P025 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - 7 | - The Fibonacci sequence is defined by the recurrence relation: 8 | - F[n] = F[n−1] + F[n−2], where F[1] = 1 and F[2] = 1. 9 | - 10 | - The 12th term, F[12], is the first term to contain three digits. 11 | - 12 | - What is the first term in the Fibonacci sequence to contain 1000 digits? 13 | -} 14 | solve :: String 15 | solve = show solveProblem 16 | 17 | fibs = 0 : 1 : zipWith (+) fibs (tail fibs) 18 | 19 | solveProblem :: Int 20 | solveProblem = idxWithDigits 1000 fibs 21 | 22 | idxWithDigits d xs = length $ takeWhile (\x -> length (show x) < d) xs 23 | 24 | firstWithDigits d xs = head $ dropWhile (\x -> length (show x) < d) xs 25 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P029.hs: -------------------------------------------------------------------------------- 1 | module Problems.P029 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - 7 | - 8 | - Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: 9 | - 10 | - 2^2 = 4, 2^3 = 8, 2^4 = 16, 2^5 = 32 11 | - 3^2 = 9, 3^3 = 27, 3^4 = 81, 3^5 = 243 12 | - 4^2 = 16, 4^3 = 64, 4^4 = 256, 4^5 = 1024 13 | - 5^2 = 25, 5^3 = 125, 5^4 = 625, 5^5 = 3125 14 | - 15 | - If they are then placed in numerical order, with any repeats removed, we get 16 | - the following sequence of 15 distinct terms: 17 | - 18 | - {4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125} 19 | - 20 | - How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 21 | - and 2 ≤ b ≤ 100? 22 | -} 23 | import qualified Data.Set as DS 24 | 25 | solve :: String 26 | solve = show solveProblem 27 | 28 | solveProblem = generalSolve 100 100 29 | 30 | generalSolve m n = DS.size $ DS.fromList [a ^ b | a <- [2 .. m], b <- [2 .. n]] 31 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P036.hs: -------------------------------------------------------------------------------- 1 | module Problems.P036 2 | ( solve 3 | ) where 4 | 5 | import Util.List (imerge, palindromes) 6 | 7 | {- 8 | - The decimal number, 585 = 10010010012 (binary), is palindromic in both 9 | - bases. 10 | - 11 | - Find the sum of all numbers, less than one million, which are palindromic in 12 | - base 10 and base 2. 13 | - 14 | - (Please note that the palindromic number, in either base, may not include 15 | - leading zeros.) 16 | -} 17 | {- 18 | - We can directly construct palindromes in each base. Then we just find the common elements. 19 | -} 20 | import Util.Math (fromIntegerDigitsBy) 21 | 22 | solve :: String 23 | solve = show $ solveProblem 1e6 24 | 25 | solveProblem bound = sum $ takeWhile (< bound) (multibasePalindromes [2, 10]) 26 | 27 | multibasePalindromes [] = [] 28 | multibasePalindromes bases = foldr1 imerge (map basePalindromes bases) 29 | 30 | basePalindromes b = 31 | map (fromIntegerDigitsBy b) $ 32 | filter (\xs -> head xs > 0) $ palindromes [0 .. b - 1] 33 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P039.hs: -------------------------------------------------------------------------------- 1 | module Problems.P039 2 | ( solve 3 | ) where 4 | 5 | import Data.List (sort, group) 6 | import Util.List (maximumBy) 7 | 8 | {- 9 | - If p is the perimeter of a right angle triangle with integral length sides, 10 | - {a,b,c}, there are exactly three solutions for p = 120. 11 | - 12 | - {20,48,52}, {24,45,51}, {30,40,50} 13 | - 14 | - For which value of p <= 1000, is the number of solutions maximised? 15 | -} 16 | import Util.Math (primitiveTriples) 17 | 18 | solve = show $ solveProblem 1000 19 | 20 | solveProblem bound = 21 | let prims = takeWhile (\t -> sum t < bound) primitiveTriples -- take the relevant primitive triples 22 | trips = [map (k *) t | t <- prims, k <- [1 .. bound `div` sum t]] -- generate the non-primitives 23 | perims = map sum trips 24 | in head $ maximumBy length $ group $ sort perims 25 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P040.hs: -------------------------------------------------------------------------------- 1 | module Problems.P040 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - An irrational decimal fraction is created by concatenating the positive 7 | - integers: 8 | - 9 | - 0.123456789101112131415161718192021... 10 | - 11 | - It can be seen that the 12th digit of the fractional part is 1. 12 | - 13 | - If dn represents the nth digit of the fractional part, find the value of the following expression. 14 | - 15 | - d1 * d10 * d100 * d1000 * d10000 * d100000 * d1000000 16 | -} 17 | import Util.Math (integerDigits) 18 | 19 | solve :: String 20 | solve = show solveProblem 21 | 22 | d = concatMap (reverse . integerDigits) [1 ..] 23 | 24 | solveProblem = 25 | product [d !! (i - 1) | i <- [1, 10, 100, 1000, 10000, 100000, 1000000]] 26 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P041.hs: -------------------------------------------------------------------------------- 1 | module Problems.P041 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - We shall say that an n-digit number is pandigital if it makes use of all the 7 | - digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is 8 | - also prime. 9 | - 10 | - What is the largest n-digit pandigital prime that exists? 11 | -} 12 | {- 13 | - Some things to note: 14 | - If the sum of the digits is a multiple of 3 then the number is divisible by 3. 15 | - Thus, there are no 5, 6, 8, or 9-digit pandigital primes, since 16 | - sum [1..8] == 36 17 | - sum [1..9] == 45 18 | - 19 | - Thus, we're probably looking for a 7-digit pandigital prime. 20 | -} 21 | import Data.List (permutations) 22 | import qualified Util.Math as Math 23 | import qualified Util.Prime as Prime 24 | 25 | solve :: String 26 | solve = show solveProblem 27 | 28 | solveProblem = 29 | let possibilities = map Math.fromIntegerDigits $ permutations [1 .. 7] 30 | in maximum $ filter Prime.test possibilities 31 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P046.hs: -------------------------------------------------------------------------------- 1 | module Problems.P046 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - It was proposed by Christian Goldbach that every odd composite number can be 7 | - written as the sum of a prime and twice a square. 8 | - 9 | - 9 = 7 + 2*1^2 10 | - 15 = 7 + 2*2^2 11 | - 21 = 3 + 2*3^2 12 | - 25 = 7 + 2*3^2 13 | - 27 = 19 + 2*2^2 14 | - 33 = 31 + 2*1^2 15 | - 16 | - It turns out that the conjecture was false. 17 | - 18 | - What is the smallest odd composite that cannot be written as the sum of 19 | - a prime and twice a square? 20 | -} 21 | import qualified Util.Prime as Prime 22 | 23 | solve :: String 24 | solve = show solveProblem 25 | 26 | fitsConjecture n = 27 | any Prime.test $ takeWhile (> 0) [n - 2 * i ^ 2 | i <- [0 ..]] 28 | 29 | solveProblem = 30 | let odd_composites = filter (not . Prime.test) [9,11 ..] 31 | in head $ filter (not . fitsConjecture) odd_composites 32 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P048.hs: -------------------------------------------------------------------------------- 1 | module Problems.P048 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. 7 | - 8 | - Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. 9 | -} 10 | solve :: String 11 | solve = show solveProblem 12 | 13 | powmod _ 0 _ = 1 14 | powmod b e m 15 | | even e = (x * x) `mod` m 16 | | odd e = (x * x * b) `mod` m 17 | where 18 | x = powmod b (e `div` 2) m 19 | 20 | solveProblem = 21 | let m = 10 ^ 10 22 | ans = sum [powmod i i m | i <- [1 .. 1000]] 23 | in ans `mod` m 24 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P053.hs: -------------------------------------------------------------------------------- 1 | module Problems.P053 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - There are exactly ten ways of selecting three from five, 12345: 7 | - 8 | - 123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 9 | - 10 | - In combinatorics, we use the notation, 5C3 = 10. 11 | - 12 | - In general, 13 | - nCr = n!/r!(n-r)! 14 | - where r <= n, n! = n*(n−1)*...*3*2*1, and 0! = 1. 15 | - 16 | - It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066. 17 | - 18 | - How many, not necessarily distinct, values of nCr, for 1 <= n <= 100, are 19 | - greater than one-million? 20 | -} 21 | import Util.Math (binomial) 22 | 23 | solve = show solveProblem 24 | 25 | solveProblem = 26 | length $ filter (> 1e6) [binomial n k | n <- [1 .. 100], k <- [0 .. n]] 27 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P056.hs: -------------------------------------------------------------------------------- 1 | module Problems.P056 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - A googol (10^100) is a massive number: one followed by one-hundred zeros; 7 | - 100^100 is almost unimaginably large: one followed by two-hundred zeros. 8 | - Despite their size, the sum of the digits in each number is only 1. 9 | - 10 | - Considering natural numbers of the form, a^b, where a, b < 100, what is the 11 | - maximum digital sum? 12 | -} 13 | import Util.Math (integerDigits) 14 | 15 | solve :: String 16 | solve = show solveProblem 17 | 18 | solveProblem = maximum $ map digitalSum [a ^ b | a <- [1 .. 99], b <- [1 .. 99]] 19 | 20 | digitalSum = sum . integerDigits 21 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P063.hs: -------------------------------------------------------------------------------- 1 | module Problems.P063 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit 7 | - number, 134217728=8^9, is a ninth power. 8 | - 9 | - How many n-digit positive integers exist which are also an nth power? 10 | -} 11 | {- 12 | - So observe that 10^n is always (n+1) digits. 13 | - 1^n is always 1 digit 14 | - Thus, for any particular exponent n > 1, we only care about bases [2..9] 15 | - 16 | - At some point, even 9^n will not be an n-digit number. Once that happens, 17 | - there are no more legit numbers. 18 | -} 19 | solve :: String 20 | solve = show solveProblem 21 | 22 | legit (b, e) = (length $ show (b ^ e)) == e 23 | 24 | upper_bound = length $ takeWhile legit [(9, e) | e <- [1 ..]] 25 | 26 | solveProblem = 27 | let be_pairs = [(b, e) | b <- [1 .. 9], e <- [1 .. upper_bound]] 28 | in length $ filter legit be_pairs 29 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P065.hs: -------------------------------------------------------------------------------- 1 | module Problems.P065 2 | ( solve 3 | ) where 4 | 5 | -- 065.hs 6 | {- 7 | - What is most surprising is that the important mathematical constant, 8 | - e = [2; 1,2,1, 1,4,1, 1,6,1 , ... , 1,2k,1, ...]. 9 | - 10 | - Find the sum of digits in the numerator of the 100th convergent of the continued fraction for e. 11 | -} 12 | import Util.Math (integerDigits, fromContinuedFraction) 13 | 14 | solve :: String 15 | solve = show solveProblem 16 | 17 | solveProblem = 18 | let cfrac = 2 : concat [[1, 2 * k, 1] | k <- [1 ..]] 19 | conv = fromContinuedFraction $ take 100 cfrac 20 | in sum $ integerDigits $ fst conv 21 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P069.hs: -------------------------------------------------------------------------------- 1 | module Problems.P069 2 | ( solve 3 | , solveProblem 4 | ) where 5 | 6 | import Util.Prime (primes) 7 | 8 | solve :: String 9 | solve = show $ solveProblem 1e6 10 | 11 | solveProblem :: Integer -> Integer 12 | solveProblem bound = last $ takeWhile (< bound) $ scanl lcm 1 primes 13 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P073.hs: -------------------------------------------------------------------------------- 1 | module Problems.P073 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - Consider the fraction, n/d, where n and d are positive integers. If n Int 24 | solveProblem bound = farey (1, 3) (1, 2) 25 | where 26 | farey (a, b) (e, f) = 27 | let (c, d) = (a + e, b + f) 28 | in if d > bound 29 | then 0 30 | else 1 + farey (a, b) (c, d) + farey (c, d) (e, f) 31 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P076.hs: -------------------------------------------------------------------------------- 1 | module Problems.P076 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - It is possible to write five as a sum in exactly six different ways: 7 | - 8 | - 4 + 1 9 | - 3 + 2 10 | - 3 + 1 + 1 11 | - 2 + 2 + 1 12 | - 2 + 1 + 1 + 1 13 | - 1 + 1 + 1 + 1 + 1 14 | - 15 | - How many different ways can one hundred be written as a sum of at least two positive integers? 16 | -} 17 | import Util.Math (coinCombos) 18 | 19 | solve :: String 20 | solve = show $ (coinCombos [1 .. 99]) !! 100 21 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P077.hs: -------------------------------------------------------------------------------- 1 | module Problems.P077 2 | ( solve 3 | ) where 4 | 5 | -- 077.hs 6 | {- 7 | - It is possible to write ten as the sum of primes in exactly five different 8 | - ways: 9 | - 10 | - 7 + 3 11 | - 5 + 5 12 | - 5 + 3 + 2 13 | - 3 + 3 + 2 + 2 14 | - 2 + 2 + 2 + 2 + 2 15 | - 16 | - What is the first value which can be written as the sum of primes in over 17 | - five thousand different ways? 18 | -} 19 | import Util.Math (coinCombos) 20 | import Util.Prime (primes) 21 | 22 | solve :: String 23 | solve = show $ solveProblem 5000 24 | 25 | solveProblem bound = 26 | let prime_parts = coinCombos (map fromIntegral primes) 27 | ans = filter (\(n, p) -> p > bound) $ zip [0 ..] prime_parts 28 | in fst $ head ans 29 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P087.hs: -------------------------------------------------------------------------------- 1 | module Problems.P087 2 | ( solve 3 | ) where 4 | 5 | import Data.Set (fromList, size) 6 | {- 7 | - The smallest number expressible as the sum of a prime square, prime cube, 8 | - and prime fourth power is 28. In fact, there are exactly four numbers below 9 | - fifty that can be expressed in such a way: 10 | - 11 | - 28 = 2^2 + 2^3 + 2^4 12 | - 33 = 3^2 + 2^3 + 2^4 13 | - 49 = 5^2 + 2^3 + 2^4 14 | - 47 = 2^2 + 3^3 + 2^4 15 | - 16 | - How many numbers below fifty million can be expressed as the sum of a prime 17 | - square, prime cube, and prime fourth power? 18 | -} 19 | import Util.Prime (primes) 20 | 21 | solve :: String 22 | solve = show $ solveProblem 50e6 23 | 24 | solveProblem bound = 25 | size $ 26 | fromList 27 | [ p2 + p3 + p4 28 | | p4 <- takeWhile (< bound) $ map (^ 4) primes 29 | , p3 <- takeWhile (< bound - p4) $ map (^ 3) primes 30 | , p2 <- takeWhile (< bound - p4 - p3) $ map (^ 2) primes 31 | ] 32 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P097.hs: -------------------------------------------------------------------------------- 1 | module Problems.P097 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - The first known prime found to exceed one million digits was discovered in 7 | - 1999, and is a Mersenne prime of the form 2^6972593−1; it contains exactly 8 | - 2,098,960 digits. Subsequently other Mersenne primes, of the form 2^p−1, 9 | - have been found which contain more digits. 10 | - 11 | - However, in 2004 there was found a massive non-Mersenne prime which contains 12 | - 2,357,207 digits: 28433×2^7830457+1. 13 | - 14 | - Find the last ten digits of this prime number. 15 | -} 16 | solve :: String 17 | solve = show solveProblem 18 | 19 | solveProblem = (28433 * 2 ^ 7830457 + 1) `mod` (10 ^ 10) 20 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P114.hs: -------------------------------------------------------------------------------- 1 | module Problems.P114 2 | ( solve 3 | , ways 4 | ) where 5 | 6 | {- 7 | - A row measuring seven units in length has red blocks with a minimum length of 8 | - three units placed on it, such that any two red blocks (which are allowed to be 9 | - different lengths) are separated by at least one black square. There are 10 | - exactly seventeen ways of doing this. 11 | - 12 | - How many ways can a row measuring fifty units in length be filled? 13 | - 14 | - NOTE: Although the example above does not lend itself to the possibility, in 15 | - general it is permitted to mix block sizes. For example, on a row measuring 16 | - eight units in length you could use red (3), black (1), and red (4). 17 | -} 18 | solve :: String 19 | solve = show $ ways !! 50 20 | 21 | ways :: [Integer] 22 | ways = zipWith (+) reds blacks 23 | where 24 | reds = replicate minRed 0 ++ zipWith (+) blacks (drop (minRed - 1) reds) 25 | blacks = 1 : zipWith (+) blacks reds 26 | 27 | minRed = 3 28 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P124.hs: -------------------------------------------------------------------------------- 1 | module Problems.P124 2 | ( solve 3 | ) where 4 | 5 | import Data.List (nub, sortBy) 6 | import Data.Ord (comparing) 7 | 8 | import Util.Prime (factors) 9 | 10 | solve :: String 11 | solve = show $ solveProblem (10 ^ 5) (10 ^ 4 - 1) 12 | 13 | rad = product . nub . factors 14 | 15 | solveProblem bound k = 16 | let xs = sortBy (comparing rad) [1 .. bound] 17 | in xs !! k 18 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P132.hs: -------------------------------------------------------------------------------- 1 | module Problems.P132 2 | ( solve 3 | ) where 4 | 5 | import Util.Math (powerMod) 6 | 7 | -- 132.hs 8 | {- 9 | - A number consisting entirely of ones is called a repunit. We shall define 10 | - R(k) to be a repunit of length k. 11 | - 12 | - For example, R(10) = 1111111111 = 11×41×271×9091, and the sum of these prime 13 | - factors is 9414. 14 | - 15 | - Find the sum of the first forty prime factors of R(10^9). 16 | -} 17 | {- 18 | - Observe that R(k) is divisible by p iff 19 | - R(k) = 0 (mod p) 20 | - (10^k - 1)/9 = 0 (mod p) 21 | - 10^k = 1 (mod 9*p) 22 | - Just test that. 23 | -} 24 | import qualified Util.Prime as Prime 25 | 26 | solve :: String 27 | solve = show $ solveProblem 40 (10 ^ 9) 28 | 29 | solveProblem count k = 30 | sum $ take count [p | p <- Prime.primes, powerMod 10 k (9 * p) == 1] 31 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P164.hs: -------------------------------------------------------------------------------- 1 | module Problems.P164 2 | ( solve 3 | ) where 4 | 5 | -- 164.hs 6 | {- 7 | - How many 20 digit numbers n (without any leading zero) exist such that no 8 | - three consecutive digits of n have a sum greater than 9? 9 | -} 10 | import Data.Array 11 | 12 | solve :: String 13 | solve = show $ g 20 14 | 15 | bound = 9 16 | 17 | f d = sum [f' (d - 1) i i | i <- [1 .. 9]] 18 | where 19 | f' 0 _ _ = 1 20 | f' d one two = sum [f' (d - 1) i (one + i) | i <- [0 .. bound - two]] 21 | 22 | g d = 23 | let arr = g' (d - 1) 24 | in sum [arr ! (i, i) | i <- [1 .. 9]] 25 | 26 | g' 0 = listArray ((0, 0), (bound, bound)) (repeat 1) 27 | g' d = 28 | let arr = g' (d - 1) 29 | in array 30 | ((0, 0), (bound, bound)) 31 | [ ((one, two), ans) 32 | | one <- [0 .. bound] 33 | , two <- [one .. bound] 34 | , let ans = sum [arr ! (i, one + i) | i <- [0 .. bound - two]] 35 | ] 36 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P188.hs: -------------------------------------------------------------------------------- 1 | module Problems.P188 2 | ( solve 3 | ) where 4 | 5 | -- 188.hs 6 | {- 7 | - The hyperexponentiation or tetration of a number a by a positive integer b, 8 | - denoted by a↑↑b or ba, is recursively defined by: 9 | - 10 | - a↑↑0 = 1, 11 | - a↑↑(k+1) = a^(a↑↑k). 12 | - 13 | - Thus we have e.g. 3↑↑2 = 3^3 = 27, hence 3↑↑3 = 3^27 = 7625597484987 and 3↑↑4 14 | - is roughly 10^(3.6383346400240996*10^12). 15 | - 16 | - Find the last 8 digits of 1777↑↑1855. 17 | -} 18 | {- 19 | - Recall that 20 | - a^(phi n) == 1 (mod n) 21 | - 22 | - Thus, consider (a ^^ (k+1)) `mod` n 23 | - a ^^ (k+1) `mod` n == a^( a^^k ) `mod` n 24 | - == a^( a^^k `mod` (phi n)) `mod` n 25 | -} 26 | import Util.Math (powerMod) 27 | import Util.Prime (phi) 28 | 29 | solve :: String 30 | solve = show solveProblem 31 | 32 | tetrateMod a 0 _ = 1 33 | tetrateMod a b m = 34 | let t = tetrateMod a (b - 1) (phi m) 35 | in powerMod a t m 36 | 37 | solveProblem = tetrateMod 1777 1855 (10 ^ 8) 38 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P190.hs: -------------------------------------------------------------------------------- 1 | module Problems.P190 2 | ( solve 3 | ) where 4 | 5 | -- 190.hs 6 | {- 7 | - Let Sm = (x1, x2, ... , xm) be the m-tuple of positive real numbers with x1 + x2 + ... + xm = m for which Pm = x1 * x22 * ... * xmm is maximised. 8 | - 9 | - For example, it can be verified that [P10] = 4112 ([ ] is the integer part function). 10 | - 11 | - Find Σ[Pm] for 2 ≤ m ≤ 15. 12 | -} 13 | solve :: String 14 | solve = show solveProblem 15 | 16 | solveProblem = sum [floor (p m) | m <- [2 .. 15]] 17 | 18 | p m = 19 | let lambda = fromIntegral (m + 1) / 2 20 | in product [(fromIntegral i / lambda) ^ i | i <- [1 .. m]] 21 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P235.hs: -------------------------------------------------------------------------------- 1 | module Problems.P235 2 | ( solve 3 | ) where 4 | 5 | {- 6 | - Given is the arithmetic-geometric sequence u(k) = (900-3k)rk-1. 7 | - Let s(n) = Σk=1...nu(k). 8 | - 9 | - Find the value of r for which s(5000) = -600,000,000,000. 10 | - 11 | - Give your answer rounded to 12 places behind the decimal point. 12 | -} 13 | import Text.Printf 14 | 15 | u k r = (900 - 3 * k) * (r ** (k - 1)) 16 | 17 | s n r = sum [u k r | k <- [1 .. n]] 18 | 19 | nestUntil pred f x 20 | | pred x = x 21 | | otherwise = nestUntil pred f (f x) 22 | 23 | findRoot f = 24 | let lo = 1.0 25 | hi = nestUntil (\x -> f x > 0) (2 *) lo 26 | in findRoot' f lo hi 27 | 28 | findRoot' f lo hi 29 | | hi - lo < 1e-13 = lo 30 | | otherwise = 31 | let mid = 0.5 * (lo + hi) 32 | in if f mid >= 0 33 | then findRoot' f lo mid 34 | else findRoot' f mid hi 35 | 36 | solve :: String 37 | solve = 38 | let targ = -600e9 39 | f r = -(s 5000 r - targ) 40 | ans = findRoot f :: Double 41 | in printf "%.12f" ans 42 | -------------------------------------------------------------------------------- /project-euler/src/Problems/P317.hs: -------------------------------------------------------------------------------- 1 | module Problems.P317 2 | ( solve 3 | ) where 4 | 5 | -- 317.hs 6 | {- 7 | - See 317.lhs for the problem statement and solution analysis. 8 | - Use: 9 | - lhs2TeX -o 317.tex 317.lhs 10 | - pdflatex 317.tex 11 | - 12 | - to generate 317.pdf, which contains the details. 13 | -} 14 | import Text.Printf (printf) 15 | 16 | solve :: String 17 | solve = printf "%.4f\n" (solveProblem 9.81 100.0 20.0) 18 | 19 | solveProblem :: Double -> Double -> Double -> Double 20 | solveProblem g h v0 = (0.25 * pi / g ** 3) * (v0 ** 3 + 2.0 * g * h * v0) ** 2 21 | -------------------------------------------------------------------------------- /project-euler/stack.yaml: -------------------------------------------------------------------------------- 1 | flags: {} 2 | extra-package-dbs: [] 3 | packages: 4 | - '.' 5 | extra-deps: 6 | - tasty-discover-1.0.1 7 | resolver: lts-7.2 8 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P001Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P001Test 2 | ( case_001_main 3 | ) where 4 | 5 | import Problems.P001 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_001_main :: Assertion 9 | case_001_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P002Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P002Test 2 | ( case_002_main 3 | ) where 4 | 5 | import Problems.P002 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_002_main :: Assertion 9 | case_002_main = solve @?= "4613732" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P003Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P003Test 2 | ( case_003_main 3 | ) where 4 | 5 | import Problems.P003 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_003_main :: Assertion 9 | case_003_main = solve @?= "6857" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P004Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P004Test 2 | ( case_004_main 3 | ) where 4 | 5 | import Problems.P004 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_004_main :: Assertion 9 | case_004_main = solve @?= "906609" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P005Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P005Test 2 | ( case_005_main 3 | ) where 4 | 5 | import Problems.P005 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_005_main :: Assertion 9 | case_005_main = solve @?= "232792560" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P006Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P006Test 2 | ( case_006_main 3 | ) where 4 | 5 | import Problems.P006 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_006_main :: Assertion 9 | case_006_main = solve @?= "25164150" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P007Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P007Test 2 | ( case_007_main 3 | ) where 4 | 5 | import Problems.P007 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_007_main :: Assertion 9 | case_007_main = solve @?= "104743" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P008Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P008Test 2 | ( case_008_main 3 | ) where 4 | 5 | import Problems.P008 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | import qualified TestData.P008 9 | 10 | case_008_main :: Assertion 11 | case_008_main = process TestData.P008.txt @?= "40824" 12 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P009Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P009Test 2 | ( case_009_main 3 | ) where 4 | 5 | import Problems.P009 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_009_main :: Assertion 9 | case_009_main = solve @?= "31875000" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P010Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P010Test 2 | ( case_010_main 3 | ) where 4 | 5 | import Problems.P010 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_010_main :: Assertion 9 | case_010_main = solve @?= "142913828922" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P011Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P011Test 2 | ( case_011_main 3 | ) where 4 | 5 | import Problems.P011 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | import qualified TestData.P011 8 | 9 | case_011_main :: Assertion 10 | case_011_main = process TestData.P011.txt @?= "70600674" 11 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P012Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P012Test 2 | ( case_012_main 3 | ) where 4 | 5 | import Problems.P012 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_012_main :: Assertion 9 | case_012_main = solve @?= "76576500" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P013Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P013Test 2 | ( case_013_main 3 | ) where 4 | 5 | import Problems.P013 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | import qualified TestData.P013 9 | 10 | case_013_main :: Assertion 11 | case_013_main = process TestData.P013.txt @?= "5537376230" 12 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P014Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P014Test 2 | ( case_014_main 3 | ) where 4 | 5 | import Problems.P014 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_014_main :: Assertion 9 | case_014_main = solve @?= "837799" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P015Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P015Test 2 | ( case_015_main 3 | ) where 4 | 5 | import Problems.P015 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_015_main :: Assertion 9 | case_015_main = solve @?= "137846528820" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P016Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P016Test 2 | ( case_016_main 3 | ) where 4 | 5 | import Problems.P016 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_016_main :: Assertion 9 | case_016_main = solve @?= "1366" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P017Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P017Test 2 | ( case_017_main 3 | ) where 4 | 5 | import Problems.P017 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_017_main :: Assertion 9 | case_017_main = solve @?= "21124" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P018Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P018Test 2 | ( case_018_main 3 | ) where 4 | 5 | import Problems.P018 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | import qualified TestData.P018 8 | 9 | case_018_main :: Assertion 10 | case_018_main = process TestData.P018.txt @?= "1074" 11 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P019Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P019Test 2 | ( case_019_main 3 | ) where 4 | 5 | import Problems.P019 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_019_main :: Assertion 9 | case_019_main = solve @?= "171" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P020Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P020Test 2 | ( case_020_main 3 | ) where 4 | 5 | import Problems.P020 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_020_main :: Assertion 9 | case_020_main = solve @?= "648" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P021Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P021Test 2 | ( case_021_main 3 | ) where 4 | 5 | import Problems.P021 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_021_main :: Assertion 9 | case_021_main = solve @?= "31626" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P022Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P022Test 2 | ( case_022_main 3 | ) where 4 | 5 | import Problems.P022 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | import qualified TestData.P022 9 | 10 | case_022_main :: Assertion 11 | case_022_main = process TestData.P022.txt @?= "871198282" 12 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P023Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P023Test 2 | ( case_023_main 3 | ) where 4 | 5 | import Problems.P023 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_023_main :: Assertion 9 | case_023_main = solve @?= "4179871" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P024Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P024Test 2 | ( case_024_main 3 | ) where 4 | 5 | import Problems.P024 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_024_main :: Assertion 9 | case_024_main = solve @?= "2783915460" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P025Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P025Test 2 | ( case_025_main 3 | ) where 4 | 5 | import Problems.P025 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_025_main :: Assertion 9 | case_025_main = solve @?= "4782" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P026Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P026Test 2 | ( case_026_main 3 | ) where 4 | 5 | import Problems.P026 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_026_main :: Assertion 9 | case_026_main = solve @?= "983" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P027Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P027Test 2 | ( case_027_main 3 | ) where 4 | 5 | import Problems.P027 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_027_main :: Assertion 9 | case_027_main = solve @?= "-59231" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P028Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P028Test 2 | ( case_028_main 3 | ) where 4 | 5 | import Problems.P028 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_028_main :: Assertion 9 | case_028_main = solve @?= "669171001" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P029Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P029Test 2 | ( case_029_main 3 | ) where 4 | 5 | import Problems.P029 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_029_main :: Assertion 9 | case_029_main = solve @?= "9183" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P030Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P030Test 2 | ( case_030_main 3 | ) where 4 | 5 | import Problems.P030 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_030_main :: Assertion 9 | case_030_main = solve @?= "443839" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P031Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P031Test 2 | ( case_031_main 3 | ) where 4 | 5 | import Problems.P031 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_031_main :: Assertion 9 | case_031_main = solve @?= "73682" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P032Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P032Test 2 | ( case_032_main 3 | ) where 4 | 5 | import Problems.P032 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_032_main :: Assertion 9 | case_032_main = solve @?= "45228" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P033Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P033Test 2 | ( case_033_main 3 | ) where 4 | 5 | import Problems.P033 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_033_main :: Assertion 9 | case_033_main = solve @?= "100" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P034Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P034Test 2 | ( case_034_main 3 | ) where 4 | 5 | import Problems.P034 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_034_main :: Assertion 9 | case_034_main = solve @?= "40730" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P035Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P035Test 2 | ( test_035 3 | ) where 4 | 5 | import Problems.P035 6 | import Test.Tasty.Discover 7 | 8 | test_035 :: [TestTree] 9 | test_035 = 10 | [ testCase "rotations" $ rotations 123 @?= [123, 312, 231] 11 | , testCase "main" $ solve @?= "55" 12 | ] 13 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P036Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P036Test 2 | ( case_036_main 3 | ) where 4 | 5 | import Problems.P036 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_036_main :: Assertion 9 | case_036_main = solve @?= "872187" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P037Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P037Test 2 | ( case_037_main 3 | ) where 4 | 5 | import Problems.P037 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_037_main :: Assertion 9 | case_037_main = solve @?= "748317" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P038Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P038Test 2 | ( case_038_main 3 | ) where 4 | 5 | import Problems.P038 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_038_main :: Assertion 9 | case_038_main = solve @?= "932718654" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P039Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P039Test 2 | ( case_039_main 3 | ) where 4 | 5 | import Problems.P039 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_039_main :: Assertion 9 | case_039_main = solve @?= "840" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P040Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P040Test 2 | ( case_040_main 3 | ) where 4 | 5 | import Problems.P040 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_040_main :: Assertion 9 | case_040_main = solve @?= "210" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P041Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P041Test 2 | ( case_041_main 3 | ) where 4 | 5 | import Problems.P041 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_041_main :: Assertion 9 | case_041_main = solve @?= "7652413" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P042Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P042Test 2 | ( case_042_main 3 | ) where 4 | 5 | import Problems.P042 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | import qualified TestData.P042 9 | 10 | case_042_main :: Assertion 11 | case_042_main = process TestData.P042.txt @?= "162" 12 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P043Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P043Test 2 | ( case_043_main 3 | ) where 4 | 5 | import Problems.P043 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_043_main :: Assertion 9 | case_043_main = solve @?= "16695334890" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P044Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P044Test 2 | ( case_044_main 3 | ) where 4 | 5 | import Problems.P044 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_044_main :: Assertion 9 | case_044_main = solve @?= "5482660" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P045Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P045Test 2 | ( case_045_main 3 | ) where 4 | 5 | import Problems.P045 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_045_main :: Assertion 9 | case_045_main = solve @?= "1533776805" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P046Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P046Test 2 | ( case_046_main 3 | ) where 4 | 5 | import Problems.P046 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_046_main :: Assertion 9 | case_046_main = solve @?= "5777" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P047Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P047Test 2 | ( case_047_main 3 | ) where 4 | 5 | import Problems.P047 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_047_main :: Assertion 9 | case_047_main = solve @?= "134043" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P048Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P048Test 2 | ( case_048_main 3 | ) where 4 | 5 | import Problems.P048 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_048_main :: Assertion 9 | case_048_main = solve @?= "9110846700" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P049Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P049Test 2 | ( case_049_main 3 | ) where 4 | 5 | import Problems.P049 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_049_main :: Assertion 9 | case_049_main = solve @?= "296962999629" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P050Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P050Test 2 | ( case_050_main 3 | ) where 4 | 5 | import Problems.P050 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_050_main :: Assertion 9 | case_050_main = solve @?= "997651" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P051Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P051Test 2 | ( case_051_main 3 | ) where 4 | 5 | import Problems.P051 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_051_main :: Assertion 9 | case_051_main = solve @?= "121313" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P052Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P052Test 2 | ( case_052_main 3 | ) where 4 | 5 | import Problems.P052 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_052_main :: Assertion 9 | case_052_main = solve @?= "142857" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P053Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P053Test 2 | ( case_053_main 3 | ) where 4 | 5 | import Problems.P053 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_053_main :: Assertion 9 | case_053_main = solve @?= "4075" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P054Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P054Test 2 | ( case_054_main 3 | ) where 4 | 5 | import Problems.P054 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | import qualified TestData.P054 8 | 9 | case_054_main :: Assertion 10 | case_054_main = process TestData.P054.txt @?= "376" 11 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P055Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P055Test 2 | ( case_055_main 3 | ) where 4 | 5 | import Problems.P055 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_055_main :: Assertion 9 | case_055_main = solve @?= "249" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P056Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P056Test 2 | ( case_056_main 3 | ) where 4 | 5 | import Problems.P056 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_056_main :: Assertion 9 | case_056_main = solve @?= "972" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P057Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P057Test 2 | ( case_057_main 3 | ) where 4 | 5 | import Problems.P057 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_057_main :: Assertion 9 | case_057_main = solve @?= "153" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P058Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P058Test 2 | ( case_058_main 3 | ) where 4 | 5 | import Problems.P058 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_058_main :: Assertion 9 | case_058_main = solve @?= "26241" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P059Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P059Test 2 | ( case_059_main 3 | ) where 4 | 5 | import Problems.P059 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | import qualified TestData.P059 8 | 9 | case_059_main :: Assertion 10 | case_059_main = process TestData.P059.txt @?= "107359" 11 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P062Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P062Test 2 | ( test_062 3 | ) where 4 | 5 | import Problems.P062 6 | import Test.Tasty.Discover 7 | 8 | test_062 :: [TestTree] 9 | test_062 = 10 | [ testCase "main" $ solve @?= "127035954683" 11 | , testCase "big" $ solveProblem 8 @?= 12620043581768 12 | ] 13 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P063Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P063Test 2 | ( case_063_main 3 | ) where 4 | 5 | import Problems.P063 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_063_main :: Assertion 9 | case_063_main = solve @?= "49" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P065Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P065Test 2 | ( case_065_main 3 | ) where 4 | 5 | import Problems.P065 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_065_main :: Assertion 9 | case_065_main = solve @?= "272" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P066Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P066Test 2 | ( test_066 3 | ) where 4 | 5 | import Problems.P066 6 | import Test.Tasty.Discover 7 | 8 | test_066 :: [TestTree] 9 | test_066 = 10 | [testCase "small" $ solveProblem 7 @?= 5, testCase "main" $ solve @?= "661"] 11 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P067Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P067Test 2 | ( case_067_main 3 | ) where 4 | 5 | import Problems.P067 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | import qualified TestData.P067 8 | 9 | case_067_main :: Assertion 10 | case_067_main = process TestData.P067.txt @?= "7273" 11 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P069Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P069Test 2 | ( test_069 3 | ) where 4 | 5 | import Problems.P069 6 | import Test.Tasty.Discover 7 | 8 | test_069 = 9 | [ testCase "main" $ solve @?= "510510" 10 | , testCase "10^9" $ solveProblem (10 ^ 9) @?= 223092870 11 | ] 12 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P071Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P071Test 2 | ( test_071 3 | ) where 4 | 5 | import Data.Ratio 6 | import Problems.P071 7 | 8 | import Test.Tasty.Discover 9 | 10 | test_071 :: [TestTree] 11 | test_071 = 12 | [ testCase "main" $ solve @?= "428570" 13 | , testCase "non-trivial" $ fareyNeighbor 1000 (15 % 17) @?= 877 % 994 14 | ] 15 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P072Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P072Test 2 | ( test_072 3 | ) where 4 | 5 | import Problems.P072 6 | import Test.Tasty.Discover 7 | 8 | test_072 :: [TestTree] 9 | test_072 = 10 | [ testCase "correctness" $ fastSolve (10 ^ 4) @?= bruteForceSolve (10 ^ 4) 11 | , testCase "main" $ solve @?= "303963552391" 12 | ] 13 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P073Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P073Test 2 | ( test_073 3 | ) where 4 | 5 | import Problems.P073 6 | import Test.Tasty.Discover 7 | 8 | test_073 :: [TestTree] 9 | test_073 = [testCase "main" $ solve @?= "7295372"] 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P074Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P074Test 2 | ( test_074 3 | ) where 4 | 5 | import Problems.P074 6 | import Test.Tasty.Discover 7 | 8 | test_074 :: [TestTree] 9 | test_074 = [testCase "main" $ solve @?= "402"] 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P076Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P076Test 2 | ( case_076_main 3 | ) where 4 | 5 | import Problems.P076 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_076_main :: Assertion 9 | case_076_main = solve @?= "190569291" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P077Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P077Test 2 | ( case_077_main 3 | ) where 4 | 5 | import Problems.P077 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_077_main :: Assertion 9 | case_077_main = solve @?= "71" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P079Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P079Test 2 | ( case_079_main 3 | ) where 4 | 5 | import Problems.P079 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | import qualified TestData.P079 8 | 9 | case_079_main :: Assertion 10 | case_079_main = process TestData.P079.txt @?= "73162890" 11 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P085Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P085Test 2 | ( case_085_main 3 | ) where 4 | 5 | import Problems.P085 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_085_main :: Assertion 9 | case_085_main = solve @?= "2772" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P087Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P087Test 2 | ( case_087_main 3 | ) where 4 | 5 | import Problems.P087 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_087_main :: Assertion 9 | case_087_main = solve @?= "1097343" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P091Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P091Test 2 | ( case_091_main 3 | ) where 4 | 5 | import Problems.P091 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_091_main :: Assertion 9 | case_091_main = solve @?= "14234" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P092Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P092Test 2 | ( test_092 3 | ) where 4 | 5 | import Problems.P092 6 | import Test.Tasty.Discover 7 | 8 | test_092 :: [TestTree] 9 | test_092 = 10 | [ testCase "correctness" $ numDigitsSolve 3 @?= bruteForceSolve (10 ^ 3) 11 | , testCase "main" $ solve @?= "8581146" 12 | ] 13 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P097Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P097Test 2 | ( case_097_main 3 | ) where 4 | 5 | import Problems.P097 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_097_main :: Assertion 9 | case_097_main = solve @?= "8739992577" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P099Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P099Test 2 | ( case_099_main 3 | ) where 4 | 5 | import Problems.P099 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | import qualified TestData.P099 8 | 9 | case_099_main :: Assertion 10 | case_099_main = process TestData.P099.txt @?= "709" 11 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P101Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P101Test 2 | ( case_101_main 3 | ) where 4 | 5 | import Problems.P101 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_101_main :: Assertion 9 | case_101_main = solve @?= "37076114526" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P102Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P102Test 2 | ( test_102 3 | ) where 4 | 5 | import Problems.P102 6 | import TestData.P102 (txt) 7 | import Test.Tasty.Discover 8 | 9 | test_102 :: [TestTree] 10 | test_102 = 11 | [ testCase "main" $ process txt @?= "228" ] 12 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P108Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P108Test 2 | ( case_108_main 3 | ) where 4 | 5 | import Problems.P108 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_108_main :: Assertion 9 | case_108_main = solve @?= "180180" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P110Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P110Test 2 | ( case_110_main 3 | ) where 4 | 5 | import Problems.P110 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_110_main :: Assertion 9 | case_110_main = solve @?= "9350130049860600" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P112Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P112Test 2 | ( case_112_main 3 | ) where 4 | 5 | import Problems.P112 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_112_main :: Assertion 9 | case_112_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P114Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P114Test 2 | ( test_114 3 | ) where 4 | 5 | import Problems.P114 6 | import Test.Tasty.Discover 7 | 8 | test_114 :: [TestTree] 9 | test_114 = 10 | [ testCase "small" $ ways !! 7 @?= 17 11 | , testCase "main" $ solve @?= "16475640049" 12 | ] 13 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P115Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P115Test 2 | ( test_115 3 | ) where 4 | 5 | import Problems.P115 6 | import Test.Tasty.Discover 7 | 8 | test_115 :: [TestTree] 9 | test_115 = 10 | [ testCase "small 3" $ solveProblem 3 @?= 30 11 | , testCase "small 10" $ solveProblem 10 @?= 57 12 | , testCase "main" $ solve @?= "168" 13 | ] 14 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P116Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P116Test 2 | ( case_116_main 3 | ) where 4 | 5 | import Problems.P116 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_116_main :: Assertion 9 | case_116_main = solve @?= "20492570929" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P117Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P117Test 2 | ( case_117_main 3 | ) where 4 | 5 | import Problems.P117 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_117_main :: Assertion 9 | case_117_main = solve @?= "100808458960497" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P119Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P119Test 2 | ( case_119_main 3 | ) where 4 | 5 | import Problems.P119 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_119_main :: Assertion 9 | case_119_main = solve @?= "248155780267521" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P122Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P122Test 2 | ( case_122_main 3 | ) where 4 | 5 | import Problems.P122 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_122_main :: Assertion 9 | case_122_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P124Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P124Test 2 | ( case_124_main 3 | ) where 4 | 5 | import Problems.P124 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_124_main :: Assertion 9 | case_124_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P125Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P125Test 2 | ( case_125_main 3 | ) where 4 | 5 | import Problems.P125 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_125_main :: Assertion 9 | case_125_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P129Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P129Test 2 | ( case_129_main 3 | ) where 4 | 5 | import Problems.P129 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_129_main :: Assertion 9 | case_129_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P130Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P130Test 2 | ( case_130_main 3 | ) where 4 | 5 | import Problems.P130 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_130_main :: Assertion 9 | case_130_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P132Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P132Test 2 | ( case_132_main 3 | ) where 4 | 5 | import Problems.P132 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_132_main :: Assertion 9 | case_132_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P147Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P147Test 2 | ( case_147_main 3 | ) where 4 | 5 | import Problems.P147 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_147_main :: Assertion 9 | case_147_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P149Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P149Test 2 | ( case_149_main 3 | ) where 4 | 5 | import Problems.P149 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_149_main :: Assertion 9 | case_149_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P150Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P150Test 2 | ( case_150_main 3 | ) where 4 | 5 | import Problems.P150 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_150_main :: Assertion 9 | case_150_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P157Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P157Test 2 | ( case_157_main 3 | ) where 4 | 5 | import Problems.P157 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_157_main :: Assertion 9 | case_157_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P164Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P164Test 2 | ( case_164_main 3 | ) where 4 | 5 | import Problems.P164 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_164_main :: Assertion 9 | case_164_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P173Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P173Test 2 | ( test_173 3 | ) where 4 | 5 | import Problems.P173 6 | import Test.Tasty.Discover 7 | 8 | test_173 :: [TestTree] 9 | test_173 = 10 | [ testCase "small" $ solveProblem 100 @?= 41 11 | , testCase "huge" $ solveProblem (10 ^ 12) @?= 3299894510984 12 | , testCase "main" $ solve @?= "1572729" 13 | ] 14 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P174Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P174Test 2 | ( test_174 3 | ) where 4 | 5 | import Problems.P174 6 | import Test.Tasty.Discover 7 | 8 | test_174 :: [TestTree] 9 | test_174 = [testCase "main" $ solve @?= "209566"] 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P178Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P178Test 2 | ( case_178_main 3 | ) where 4 | 5 | import Problems.P178 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_178_main :: Assertion 9 | case_178_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P181Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P181Test 2 | ( case_181_main 3 | ) where 4 | 5 | import Problems.P181 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_181_main :: Assertion 9 | case_181_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P183Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P183Test 2 | ( case_183_main 3 | ) where 4 | 5 | import Problems.P183 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_183_main :: Assertion 9 | case_183_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P188Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P188Test 2 | ( case_188_main 3 | ) where 4 | 5 | import Problems.P188 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_188_main :: Assertion 9 | case_188_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P190Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P190Test 2 | ( case_190_main 3 | ) where 4 | 5 | import Problems.P190 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_190_main :: Assertion 9 | case_190_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P191Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P191Test 2 | ( case_191_main 3 | ) where 4 | 5 | import Problems.P191 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_191_main :: Assertion 9 | case_191_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P197Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P197Test 2 | ( case_197_main 3 | ) where 4 | 5 | import Problems.P197 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_197_main :: Assertion 9 | case_197_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P200Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P200Test 2 | ( case_200_main 3 | ) where 4 | 5 | import Problems.P200 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_200_main :: Assertion 9 | case_200_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P201Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P201Test 2 | ( case_201_main 3 | ) where 4 | 5 | import Problems.P201 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_201_main :: Assertion 9 | case_201_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P203Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P203Test 2 | ( case_203_main 3 | ) where 4 | 5 | import Problems.P203 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_203_main :: Assertion 9 | case_203_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P204Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P204Test 2 | ( case_204_main 3 | ) where 4 | 5 | import Problems.P204 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_204_main :: Assertion 9 | case_204_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P205Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P205Test 2 | ( case_205_main 3 | ) where 4 | 5 | import Problems.P205 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_205_main :: Assertion 9 | case_205_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P206Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P206Test 2 | ( case_206_main 3 | ) where 4 | 5 | import Problems.P206 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_206_main :: Assertion 9 | case_206_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P209Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P209Test 2 | ( case_209_main 3 | ) where 4 | 5 | import Problems.P209 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_209_main :: Assertion 9 | case_209_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P235Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P235Test 2 | ( case_235_main 3 | ) where 4 | 5 | import Problems.P235 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_235_main :: Assertion 9 | case_235_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P240Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P240Test 2 | ( case_240_main 3 | ) where 4 | 5 | import Problems.P240 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_240_main :: Assertion 9 | case_240_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P249Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P249Test 2 | ( case_249_main 3 | ) where 4 | 5 | import Problems.P249 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_249_main :: Assertion 9 | case_249_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P250Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P250Test 2 | ( case_250_main 3 | ) where 4 | 5 | import Problems.P250 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_250_main :: Assertion 9 | case_250_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P301Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P301Test 2 | ( case_301_main 3 | ) where 4 | 5 | import Problems.P301 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_301_main :: Assertion 9 | case_301_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P317Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P317Test 2 | ( case_317_main 3 | ) where 4 | 5 | import Problems.P317 6 | import Test.Tasty.Discover (Assertion, (@?=)) 7 | 8 | case_317_main :: Assertion 9 | case_317_main = solve @?= "233168" 10 | -------------------------------------------------------------------------------- /project-euler/tests/Problems/P493Test.hs: -------------------------------------------------------------------------------- 1 | module Problems.P493Test 2 | ( test_493 3 | ) where 4 | 5 | import Problems.P493 6 | import Test.Tasty.Discover 7 | 8 | import Data.Ratio 9 | 10 | test_493 :: [TestTree] 11 | test_493 = 12 | [ testCase "main" $ solve @?= "6.818741802" 13 | , testCase "big" $ floor (solveProblem [1 .. 1000] 500) @?= 368 14 | ] 15 | -------------------------------------------------------------------------------- /project-euler/tests/Tasty.hs: -------------------------------------------------------------------------------- 1 | {-# OPTIONS_GHC -F -pgmF tasty-discover #-} 2 | -------------------------------------------------------------------------------- /project-euler/tests/TestData/P018.hs: -------------------------------------------------------------------------------- 1 | module TestData.P018 (txt) where 2 | 3 | txt = 4 | unlines 5 | [ "75" 6 | , "95 64" 7 | , "17 47 82" 8 | , "18 35 87 10" 9 | , "20 04 82 47 65" 10 | , "19 01 23 75 03 34" 11 | , "88 02 77 73 07 63 67" 12 | , "99 65 04 28 06 16 70 92" 13 | , "41 41 26 56 83 40 80 70 33" 14 | , "41 48 72 33 47 32 37 16 94 29" 15 | , "53 71 44 65 25 43 91 52 97 51 14" 16 | , "70 11 33 28 77 73 17 78 39 68 17 57" 17 | , "91 71 52 38 17 14 91 43 58 50 27 29 48" 18 | , "63 66 04 68 89 53 67 30 73 16 69 87 40 31" 19 | , "04 62 98 27 23 09 70 98 73 93 38 53 60 04 23" 20 | ] 21 | -------------------------------------------------------------------------------- /project-euler/tests/TestData/P079.hs: -------------------------------------------------------------------------------- 1 | module TestData.P079 (txt) where 2 | 3 | txt = 4 | unlines 5 | [ "319" 6 | , "680" 7 | , "180" 8 | , "690" 9 | , "129" 10 | , "620" 11 | , "762" 12 | , "689" 13 | , "762" 14 | , "318" 15 | , "368" 16 | , "710" 17 | , "720" 18 | , "710" 19 | , "629" 20 | , "168" 21 | , "160" 22 | , "689" 23 | , "716" 24 | , "731" 25 | , "736" 26 | , "729" 27 | , "316" 28 | , "729" 29 | , "729" 30 | , "710" 31 | , "769" 32 | , "290" 33 | , "719" 34 | , "680" 35 | , "318" 36 | , "389" 37 | , "162" 38 | , "289" 39 | , "162" 40 | , "718" 41 | , "729" 42 | , "319" 43 | , "790" 44 | , "680" 45 | , "890" 46 | , "362" 47 | , "319" 48 | , "760" 49 | , "316" 50 | , "729" 51 | , "380" 52 | , "319" 53 | , "728" 54 | , "716" 55 | ] 56 | -------------------------------------------------------------------------------- /project-euler/tests/Util/PrimeTest.hs: -------------------------------------------------------------------------------- 1 | module Util.PrimeTest 2 | ( test_primes 3 | ) where 4 | 5 | import Test.Tasty.Discover 6 | import Util.Prime 7 | 8 | import qualified Data.Array as A 9 | 10 | test_primes :: [TestTree] 11 | test_primes = [testCase "small sieve" sieve_small] 12 | 13 | sieve_small :: Assertion 14 | sieve_small = 15 | let small_sieve = sieve 30 16 | small_primes = filter (small_sieve A.!) [2 .. 30] 17 | in small_primes @?= [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] 18 | -------------------------------------------------------------------------------- /sheep-in-hilly-meadow/sheep_in_hilly_meadow.in: -------------------------------------------------------------------------------- 1 | 1 10 100 1000 2 | 10 10 100 1000 3 | 100 100 100 1000 4 | 1000 1000 1000 1000 5 | --------------------------------------------------------------------------------