Skip to content

Instantly share code, notes, and snippets.

@dtinth
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtinth/f6850729a3513b19dd45 to your computer and use it in GitHub Desktop.
Save dtinth/f6850729a3513b19dd45 to your computer and use it in GitHub Desktop.
My Attempt at Hyper Hackathon

โจทย์ : http://expert-programming-tutor.com/tutorial/hyperhackathon.php

ป.ล. ไม่ได้ไปแข่ง

1.1 Prime Number

require 'prime'; Prime.find { |c| c > 1000000 } #=> 1000003

1.3 Factorial Digit Sum

1.upto(10000).reduce(&:*).to_s.chars.map(&:to_i).reduce(&:+) #=> 149346

2.1 Sum of Two Primes

-> n { -> a { a && [a, n - a] }[Prime.take_while { |c| c <= n }.find { |c| (n - c).prime? }] }[14] #=> [3, 11]
  1. Pattern

-> n { n -= 1; m = 2 * n + 1; puts Array.new(m) { |i| Array.new(m) { |j| [0, n].include?((n - i).abs + (n - j).abs) ? 'x' : '-' }.join } }[5]
----x----
---x-x---
--x---x--
-x-----x-
x---x---x
-x-----x-
--x---x--
---x-x---
----x----

4 Consecutive Numbers

stuff = <<EOF
34 90 61 34 10 42 01 90 54 69 20 50 58 70 58 42 48 82 73 84
20 43 78 99 49 30 15 70 41 27 61 36 96 66 44 78 58 76 79 05
76 39 32 74 28 16 90 88 37 94 47 51 78 04 17 61 65 39 35 05
36 01 64 17 37 55 90 39 12 39 71 78 77 25 09 36 20 67 44 06
72 06 78 03 62 28 99 87 39 65 44 07 24 72 04 50 46 51 83 35
57 39 22 39 17 92 44 59 24 36 81 66 87 13 57 87 55 14 15 91
83 25 10 22 35 13 69 24 47 49 48 78 14 23 56 39 24 45 49 05
19 95 97 39 82 73 66 33 01 80 98 30 44 11 56 39 51 00 41 73
41 07 36 88 74 65 88 26 12 29 16 16 87 94 25 10 27 44 18 34
00 06 06 46 93 53 84 93 87 92 57 42 74 44 11 30 50 69 65 53
34 52 89 81 48 98 53 13 91 35 15 43 26 05 19 30 32 00 12 01
86 27 93 90 26 52 84 73 65 04 33 99 50 34 79 32 27 54 69 86
95 82 36 96 75 15 68 17 28 35 46 15 49 54 68 07 29 42 56 85
53 60 22 29 26 78 87 60 37 73 35 57 91 60 49 64 29 23 87 94
75 07 72 03 82 79 83 62 06 06 93 35 93 31 63 65 56 72 60 62
97 51 78 55 65 06 43 29 53 08 90 46 80 96 71 55 75 45 74 76
39 15 27 39 67 23 30 96 10 08 75 06 67 02 16 40 67 76 97 80
15 11 73 19 70 95 64 16 00 30 00 16 59 31 77 18 40 86 98 98
05 32 40 25 12 26 00 68 64 23 59 40 29 30 42 11 26 22 80 38
36 77 48 18 62 50 00 85 67 72 39 93 03 17 26 38 89 96 86 68 
EOF

table = stuff.lines.map(&:strip).map { |row| row.split.map(&:to_i) }    # original table
vertical = table.transpose                                              # vertical table

diagonals = -> m { m.each_with_index.map { |c, i| (c + [nil] * (m.length - 1)).rotate(i) }.transpose.map(&:compact) }
diagonal1 = diagonals[table]
diagonal2 = diagonals[table.reverse]

adjacent = (table + vertical + diagonal1 + diagonal2).flat_map { |c| c.each_cons(4).to_a }

sum = -> a { a.reduce(&:+) }
adjacent.max_by(&sum)   #=> [97, 88, 93, 98]
adjacent.min_by(&sum)   #=> [0, 16, 10, 8]

How diagonals works

Imgur

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment