Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- declare
- {
- record Name
- {
- string name;
- int id;
- string cksum;
- }
- record State
- {
- seq<Name> reals;
- }
- }
- function parse(string ln) : Name
- {
- Name n;
- seq<string> toks = strtok(ln, "[]");
- n.cksum = toks[1u];
- uint pos = strrchr(toks[0u], '-');
- n.name = substr(toks[0u], 0u, pos);
- n.id = toi32(substr(toks[0u], pos+1u, strlen(toks[0u])));
- return n;
- }
- function cksum(Name n) : string
- {
- seq<uint> ap = seqfill(0u, 26u);
- uint m = 0u;
- foreach(ch in n.name which '-'!=ch)
- {
- uint idx = tou32(ch - 'a');
- ap[idx] = ap[idx] + 1u;
- if(m<ap[idx]) { m = ap[idx]; }
- }
- string s;
- foreach(cnt in 1u .. m reverse)
- {
- foreach(idx in 0u .. seqlen(ap) exclusive which ap[idx]==cnt)
- {
- s = s + tos('a' + idx);
- if(strlen(s)==5u)
- {
- return s;
- }
- }
- }
- return s;
- }
- function decrypt(Name n) : string
- {
- int rot = n.id % 26;
- string s = n.name;
- foreach(i in 0u .. strlen(s) exclusive)
- {
- if('-'!=s[i])
- {
- int offs = s[i] - 'a';
- s[i] = 'a' + ((offs + rot) % 26);
- }
- else
- {
- s[i] = ' ';
- }
- }
- return s;
- }
- impure function part1(mutable State s)
- {
- file f = fopen("input", 'r');
- string ln;
- for(ln=freadln(f);!empty(ln);ln=freadln(f))
- {
- Name n = parse(ln);
- if(n.cksum==cksum(n))
- {
- s.reals = hiext(s.reals, n);
- }
- }
- }
- main
- {
- State s;
- part1(s);
- int sum = 0;
- foreach(n in s.reals) { sum = sum + n.id; }
- write(tos(sum) + "\n");
- // part2
- foreach(n in s.reals)
- {
- string d = decrypt(n);
- if(strstr(d, "north")<strlen(d))
- {
- write(d + ": " + tos(n.id) + "\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement