Last active
December 2, 2017 23:05
-
-
Save danschultzer/76d4466758517a966f3bf0db7474297f to your computer and use it in GitHub Desktop.
Code of Advent 2017 Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Part one | |
defmodule CodeAdvent_1 do | |
def sum(captcha, acc) when is_integer(captcha) do | |
digits = Integer.digits(captcha) | |
first_digit = List.first(digits) | |
digits | |
|> Kernel.++([first_digit]) | |
|> sum(acc) | |
end | |
def sum([first, second | rest], acc) when first == second, do: sum([second | rest], acc + first) | |
def sum([_first, second | rest], acc), do: sum([second | rest], acc) | |
def sum([_first], acc), do: acc | |
def sum([], acc), do: acc | |
end | |
CodeAdvent_1.sum(1122, 0) |> IO.inspect() | |
CodeAdvent_1.sum(1111, 0) |> IO.inspect() | |
CodeAdvent_1.sum(1234, 0) |> IO.inspect() | |
CodeAdvent_1.sum(91212129, 0) |> IO.inspect() | |
8231753674683997878179259195565332579493378483264978184143341284379682788518559178822225126625428318115396632681141871952894291898364781898929292614792884883249356728741993224889167928232261325123447569829932951268292953928766755779761837993812528527484487298117739869189415599461746944992651752768158611996715467871381527675219481185217357632445748912726487669881876129192932995282777848496561259839781188719233951619188388532698519298142112853776942545211859134185231768952888462471642851588368445761489225786919778983848113833773768236969923939838755997989537648222217996381757542964844337285428654375499359997792679256881378967852376848812795761118139288152799921176874256377615952758268844139579622754965461884862647423491918913628848748756595463191585555385849335742224855473769411212376446591654846168189278959857681336724221434846946124915271196433144335482787432683848594487648477532498952572515118864475621828118274911298396748213136426357769991314661642612786847135485969889237193822718111269561741563479116832364485724716242176288642371849569664594194674763319687735723517614962575592111286177553435651952853878775431234327919595595658641534765455489561934548474291254387229751472883423413196845162752716925199866591883313638846474321161569892518574346226751366315311145777448781862222126923449311838564685882695889397531413937666673233451216968414288135984394249684886554812761191289485457945866524228415191549168557957633386991931186773843869999284468773866221976873998168818944399661463963658784821796272987155278195355579386768156718813624559264574836134419725187881514665834441359644955768658663278765363789664721736533517774292478192143934318399418188298753351815388561359528533778996296279366394386455544446922653976725113889842749182361253582433319351193862788433113852782596161148992233558144692913791714859516653421917841295749163469751479835492713392861519993791967927773114713888458982796514977717987598165486967786989991998142488631168697963816156374216224386193941566358543266646516247854435356941566492841213424915682394928959116411457967897614457497279472661229548612777155998358618945222326558176486944695689777438164612198225816646583996426313832539918 |> CodeAdvent_1.sum(0) |> IO.inspect() | |
# Part two | |
defmodule CodeAdvent_1_2 do | |
def sum(captcha) do | |
digits = Integer.digits(captcha) | |
jump_length = Enum.count(digits) / 2 |> round | |
{_, value} = Enum.reduce(digits, {0, 0}, fn digit, {pos, acc} -> | |
jump_pos = Integer.mod(pos + jump_length, Enum.count(digits)) | |
if Enum.at(digits, jump_pos) == digit, do: acc = acc + digit | |
{pos + 1, acc} | |
end) | |
value | |
end | |
end | |
6 = CodeAdvent_1_2.sum(1212) | |
0 = CodeAdvent_1_2.sum(1221) | |
4 = CodeAdvent_1_2.sum(123425) | |
12 = CodeAdvent_1_2.sum(123123) | |
4 = CodeAdvent_1_2.sum(12131415) | |
captcha = 8231753674683997878179259195565332579493378483264978184143341284379682788518559178822225126625428318115396632681141871952894291898364781898929292614792884883249356728741993224889167928232261325123447569829932951268292953928766755779761837993812528527484487298117739869189415599461746944992651752768158611996715467871381527675219481185217357632445748912726487669881876129192932995282777848496561259839781188719233951619188388532698519298142112853776942545211859134185231768952888462471642851588368445761489225786919778983848113833773768236969923939838755997989537648222217996381757542964844337285428654375499359997792679256881378967852376848812795761118139288152799921176874256377615952758268844139579622754965461884862647423491918913628848748756595463191585555385849335742224855473769411212376446591654846168189278959857681336724221434846946124915271196433144335482787432683848594487648477532498952572515118864475621828118274911298396748213136426357769991314661642612786847135485969889237193822718111269561741563479116832364485724716242176288642371849569664594194674763319687735723517614962575592111286177553435651952853878775431234327919595595658641534765455489561934548474291254387229751472883423413196845162752716925199866591883313638846474321161569892518574346226751366315311145777448781862222126923449311838564685882695889397531413937666673233451216968414288135984394249684886554812761191289485457945866524228415191549168557957633386991931186773843869999284468773866221976873998168818944399661463963658784821796272987155278195355579386768156718813624559264574836134419725187881514665834441359644955768658663278765363789664721736533517774292478192143934318399418188298753351815388561359528533778996296279366394386455544446922653976725113889842749182361253582433319351193862788433113852782596161148992233558144692913791714859516653421917841295749163469751479835492713392861519993791967927773114713888458982796514977717987598165486967786989991998142488631168697963816156374216224386193941566358543266646516247854435356941566492841213424915682394928959116411457967897614457497279472661229548612777155998358618945222326558176486944695689777438164612198225816646583996426313832539918 | |
captcha|> CodeAdvent_1_2.sum() |> IO.inspect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Part 1 | |
defmodule CodeAdvent_2 do | |
def checksum(string) when is_binary(string) do | |
string | |
|> String.split("\n") | |
|> Enum.filter(&(String.length(&1) > 0)) | |
|> Enum.map(fn row -> | |
row | |
|> String.split(~r/\s+/) | |
|> Enum.map(&String.to_integer(&1)) | |
end) | |
|> checksum(0) | |
end | |
def checksum([row | rest], acc) do | |
checksum(rest, acc + checksum_row(row)) | |
end | |
def checksum([], acc), do: acc | |
def checksum_row(row) do | |
Enum.max(row) - Enum.min(row) | |
end | |
end | |
test = """ | |
5 1 9 5 | |
7 5 3 | |
2 4 6 8 | |
""" | |
18 = CodeAdvent_2.checksum(test) | |
spreadsheet = """ | |
626 2424 2593 139 2136 163 1689 367 2235 125 2365 924 135 2583 1425 2502 | |
183 149 3794 5221 5520 162 5430 4395 2466 1888 3999 3595 195 181 6188 4863 | |
163 195 512 309 102 175 343 134 401 372 368 321 350 354 183 490 | |
2441 228 250 2710 200 1166 231 2772 1473 2898 2528 2719 1736 249 1796 903 | |
3999 820 3277 3322 2997 1219 1014 170 179 2413 183 3759 3585 2136 3700 188 | |
132 108 262 203 228 104 205 126 69 208 235 311 313 258 110 117 | |
963 1112 1106 50 186 45 154 60 1288 1150 986 232 872 433 48 319 | |
111 1459 98 1624 2234 2528 93 1182 97 583 2813 3139 1792 1512 1326 3227 | |
371 374 459 83 407 460 59 40 42 90 74 163 494 250 488 444 | |
1405 2497 2079 2350 747 1792 2412 2615 89 2332 1363 102 81 2346 122 1356 | |
1496 2782 2257 2258 961 214 219 2998 400 230 2676 3003 2955 254 2250 2707 | |
694 669 951 455 2752 216 1576 3336 251 236 222 2967 3131 3456 1586 1509 | |
170 2453 1707 2017 2230 157 2798 225 1891 945 943 2746 186 206 2678 2156 | |
3632 3786 125 2650 1765 1129 3675 3445 1812 3206 99 105 1922 112 1136 3242 | |
6070 6670 1885 1994 178 230 5857 241 253 5972 7219 252 806 6116 4425 3944 | |
2257 155 734 228 204 2180 175 2277 180 2275 2239 2331 2278 1763 112 2054 | |
""" | |
spreadsheet |> CodeAdvent_2.checksum() |> IO.inspect() | |
# Part 2 | |
defmodule CodeAdvent_2_2 do | |
def checksum(string) when is_binary(string) do | |
string | |
|> String.split("\n") | |
|> Enum.filter(&(String.length(&1) > 0)) | |
|> Enum.map(fn row -> | |
row | |
|> String.split(~r/\s+/) | |
|> Enum.map(&String.to_integer(&1)) | |
end) | |
|> checksum(0) | |
end | |
def checksum([row | rest], acc) do | |
checksum(rest, acc + checksum_row(row)) | |
end | |
def checksum([], acc), do: acc | |
def checksum_row(items) do | |
Enum.find_value(items, fn item -> | |
Enum.find_value(items, fn item_2 -> | |
numbers = [item, item_2] | |
max = Enum.max(numbers) | |
min = Enum.min(numbers) | |
cond do | |
max == min -> nil | |
Integer.mod(max, min) == 0 -> max / min |> round | |
true -> nil | |
end | |
end) | |
end) | |
end | |
end | |
test = """ | |
5 9 2 8 | |
9 4 7 3 | |
3 8 6 5 | |
""" | |
9 = CodeAdvent_2_2.checksum(test) | |
spreadsheet = """ | |
626 2424 2593 139 2136 163 1689 367 2235 125 2365 924 135 2583 1425 2502 | |
183 149 3794 5221 5520 162 5430 4395 2466 1888 3999 3595 195 181 6188 4863 | |
163 195 512 309 102 175 343 134 401 372 368 321 350 354 183 490 | |
2441 228 250 2710 200 1166 231 2772 1473 2898 2528 2719 1736 249 1796 903 | |
3999 820 3277 3322 2997 1219 1014 170 179 2413 183 3759 3585 2136 3700 188 | |
132 108 262 203 228 104 205 126 69 208 235 311 313 258 110 117 | |
963 1112 1106 50 186 45 154 60 1288 1150 986 232 872 433 48 319 | |
111 1459 98 1624 2234 2528 93 1182 97 583 2813 3139 1792 1512 1326 3227 | |
371 374 459 83 407 460 59 40 42 90 74 163 494 250 488 444 | |
1405 2497 2079 2350 747 1792 2412 2615 89 2332 1363 102 81 2346 122 1356 | |
1496 2782 2257 2258 961 214 219 2998 400 230 2676 3003 2955 254 2250 2707 | |
694 669 951 455 2752 216 1576 3336 251 236 222 2967 3131 3456 1586 1509 | |
170 2453 1707 2017 2230 157 2798 225 1891 945 943 2746 186 206 2678 2156 | |
3632 3786 125 2650 1765 1129 3675 3445 1812 3206 99 105 1922 112 1136 3242 | |
6070 6670 1885 1994 178 230 5857 241 253 5972 7219 252 806 6116 4425 3944 | |
2257 155 734 228 204 2180 175 2277 180 2275 2239 2331 2278 1763 112 2054 | |
""" | |
spreadsheet |> CodeAdvent_2_2.checksum() |> IO.inspect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment