problem 1
- given a string of lines, for each line, there is a character that appears in both first half of the string and second half of the string.
- find that character (for each line).
- change the character to a number, find the sum.
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
Clear[ xinput, xLines, xlen, xpouch1, xpouch2, x2, xlist ]
xinput = "vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw";
Clear[ charToNum ]
xlist =
Map[ If[ UpperCaseQ[ # ], ToCharacterCode[#]-64 + 26, ToCharacterCode[#]-96 ]&,
(StringCases[Last@#, RegularExpression[StringJoin["[", First@#, "]"]], 1] & )
/@ (StringPartition[#, StringLength[#]/2] &) /@ StringSplit[xinput] , {2}]
Total[xlist]
problem 2
Clear[ xinput, x1, x2, x3, x4, x5 ]
xinput = "vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw";
x1= StringSplit[ xinput ]
x2 = Partition[ x1, 3 ]
x3 = Map[ Characters , x2 , {-1} ]
x4 = Map[ Intersection@@#& ,x3 , {1} ]
x5 = Map[ Function[ char, If[ UpperCaseQ[ char ], ToCharacterCode[char]-64 + 26, ToCharacterCode[char]-96 ] ] , x4 , {2} ]
x6 = Total[ x5 ]