Skip to content

Instantly share code, notes, and snippets.

@ghouston
Last active December 3, 2017 02:51
Show Gist options
  • Save ghouston/09f8b44e1f73feaf711d4faf0468ba74 to your computer and use it in GitHub Desktop.
Save ghouston/09f8b44e1f73feaf711d4faf0468ba74 to your computer and use it in GitHub Desktop.
advent of code 2017
digits = "77736991856689225253142335214746294932318813454849177823468674346512426482777696993348135287531487622845155339235443718798255411492778415157351753377959586612882455464736285648473397681163729345143319577258292849619491486748832944425643737899293811819448271546283914592546989275992844383947572926628695617661344293284789225493932487897149244685921644561896799491668147588536732985476538413354195246785378443492137893161362862587297219368699689318441563683292683855151652394244688119527728613756153348584975372656877565662527436152551476175644428333449297581939357656843784849965764796365272113837436618857363585783813291999774718355479485961244782148994281845717611589612672436243788252212252489833952785291284935439662751339273847424621193587955284885915987692812313251556836958571335334281322495251889724281863765636441971178795365413267178792118544937392522893132283573129821178591214594778712292228515169348771198167462495988252456944269678515277886142827218825358561772588377998394984947946121983115158951297156321289231481348126998584455974277123213413359859659339792627742476688827577318285573236187838749444212666293172899385531383551142896847178342163129883523694183388123567744916752899386265368245342587281521723872555392212596227684414269667696229995976182762587281829533181925696289733325513618571116199419759821597197636415243789757789129824537812428338192536462468554399548893532588928486825398895911533744671691387494516395641555683144968644717265849634943691721391779987198764147667349266877149238695714118982841721323853294642175381514347345237721288281254828745122878268792661867994785585131534136646954347165597315643658739688567246339618795777125767432162928257331951255792438831957359141651634491912746875748363394329848227391812251812842263277229514125426682179711184717737714178235995431465217547759282779499842892993556918977773236196185348965713241211365895519697294982523166196268941976859987925578945185217127344619169353395993198368185217391883839449331638641744279836858188235296951745922667612379649453277174224722894599153367373494255388826855322712652812127873536473277"
defmodule Day1 do
def rotate( string, amount ) do
parts = String.split_at( string, amount )
elem(parts,1) <> elem(parts,0)
end
def value( {same,same} ) do
same
end
def value( {_not,_same} ) do
0
end
def captcha( str, amount ) do
str
|> String.graphemes
|> Enum.map( fn(x) -> String.to_integer(x) end ) # not sure how to simplify this line
|> Enum.zip( str
|> Day1.rotate( amount )
|> String.graphemes
|> Enum.map( fn(x) -> String.to_integer(x) end)
)
|> Enum.reduce(0, fn(x, acc) -> Day1.value(x) + acc end)
end
end
IO.puts "part 1: #{ Day1.captcha(digits, 1) }"
IO.puts "part 2: #{ Day1.captcha(digits, trunc(String.length(digits)/ 2)) }"
digits = "77736991856689225253142335214746294932318813454849177823468674346512426482777696993348135287531487622845155339235443718798255411492778415157351753377959586612882455464736285648473397681163729345143319577258292849619491486748832944425643737899293811819448271546283914592546989275992844383947572926628695617661344293284789225493932487897149244685921644561896799491668147588536732985476538413354195246785378443492137893161362862587297219368699689318441563683292683855151652394244688119527728613756153348584975372656877565662527436152551476175644428333449297581939357656843784849965764796365272113837436618857363585783813291999774718355479485961244782148994281845717611589612672436243788252212252489833952785291284935439662751339273847424621193587955284885915987692812313251556836958571335334281322495251889724281863765636441971178795365413267178792118544937392522893132283573129821178591214594778712292228515169348771198167462495988252456944269678515277886142827218825358561772588377998394984947946121983115158951297156321289231481348126998584455974277123213413359859659339792627742476688827577318285573236187838749444212666293172899385531383551142896847178342163129883523694183388123567744916752899386265368245342587281521723872555392212596227684414269667696229995976182762587281829533181925696289733325513618571116199419759821597197636415243789757789129824537812428338192536462468554399548893532588928486825398895911533744671691387494516395641555683144968644717265849634943691721391779987198764147667349266877149238695714118982841721323853294642175381514347345237721288281254828745122878268792661867994785585131534136646954347165597315643658739688567246339618795777125767432162928257331951255792438831957359141651634491912746875748363394329848227391812251812842263277229514125426682179711184717737714178235995431465217547759282779499842892993556918977773236196185348965713241211365895519697294982523166196268941976859987925578945185217127344619169353395993198368185217391883839449331638641744279836858188235296951745922667612379649453277174224722894599153367373494255388826855322712652812127873536473277"
def captcha( digits, rotate )
d=digits
.scan(/./)
.map(&:to_i)
d.zip(d.rotate(rotate))
.select{|x|x[0]==x[1]}
.reduce(0){|sum,x|sum+=x[0]}
end
puts "part 1: #{captcha( digits, 1 )}"
puts "part 2: #{captcha( digits, digits.size/2 )}"
#
# tonight its getting late... so this is quick and dirty
# i didn't try to recreate in elixir.
# however i see evantravers did an exilir version at
# https://github.com/evantravers/adventofcode/blob/master/elixir/day2/checksum.ex
#
data = <<EOF
1640 590 93 958 73 1263 1405 1363 737 712 1501 390 68 1554 959 79
4209 128 131 2379 2568 2784 2133 145 3618 1274 3875 158 1506 3455 1621 3799
206 1951 2502 2697 2997 74 76 78 1534 81 2775 2059 3026 77 2600 3067
373 1661 94 102 2219 1967 1856 417 1594 75 100 2251 2200 1825 1291 1021
57 72 51 1101 1303 60 1227 421 970 1058 138 333 1320 1302 402 1210
4833 5427 179 3934 4533 5124 4832 2088 94 200 199 1114 4151 1795 208 3036
759 876 110 79 1656 1691 185 544 616 312 757 1712 92 97 1513 1683
1250 1186 284 107 1190 1233 573 1181 1041 655 132 547 395 146 119 515
505 1726 79 180 86 1941 1597 1785 1608 1692 968 1177 94 184 91 31
1366 2053 1820 1570 70 506 53 415 717 1263 82 366 74 1255 2020 1985
2365 5585 2285 4424 5560 3188 3764 187 88 223 1544 5023 4013 5236 214 196
1487 1305 1359 1615 6579 2623 4591 150 5030 188 146 4458 5724 5828 1960 221
3114 688 3110 334 1921 153 4083 131 2234 3556 3573 3764 127 919 3293 104
1008 78 1196 607 135 1409 296 475 915 157 1419 1304 153 423 163 704
235 4935 4249 3316 1202 221 1835 380 249 1108 1922 5607 4255 238 211 3973
1738 207 179 137 226 907 1468 1341 1582 1430 851 213 393 1727 1389 632
EOF
require 'stringio'
checksum = 0
StringIO.open( data ).each_line do |line|
sorted = line.scan(/(?=\s*)\d+(?=\s*)/)
.map(&:to_i)
.sort
checksum += sorted.last - sorted.first
end
puts "part 1: #{checksum}"
checksum = 0
StringIO.open( data ).each_line do |line|
numbers = line.scan(/(?=\s*)\d+(?=\s*)/)
.map(&:to_i)
pairs = numbers.combination(2)
found = pairs.find do |pair|
x,y = pair.sort
(y % x) == 0
end
x,y = found.sort
checksum += y/x
end
puts "part 2: #{checksum}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment