Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- impure function part1() : uint
- {
- uint num;
- file f = fopen("input", 'r');
- string ln;
- for(ln=freadln(f);!empty(ln);ln=freadln(f))
- {
- bool ok, abba, bracket = true, false, false;
- char t1, t2, t3, t4 = '\x01', '\x02', '\x03', '\x04';
- foreach(ch in ln while ok)
- {
- t1, t2, t3, t4 = t2, t3, t4, ch;
- if('['==ch)
- {
- bracket = true;
- }
- elif(']'==ch)
- {
- bracket = false;
- }
- elif(t1!=t2 &&& t1==t4 &&& t2==t3)
- {
- if(bracket) { ok = false; }
- else { abba = true; }
- }
- }
- if(ok &&& abba)
- {
- num = num + 1u;
- }
- }
- return num;
- }
- declare
- {
- record Pair
- {
- char first;
- char second;
- }
- }
- function has(seq<Pair> ps, Pair p) : bool
- {
- foreach(x in ps)
- {
- if(x.first==p.first &&& x.second==p.second)
- {
- return true;
- }
- }
- return false;
- }
- impure function part2() : uint
- {
- uint num;
- file f = fopen("input", 'r');
- string ln;
- for(ln=freadln(f);!empty(ln);ln=freadln(f))
- {
- seq<Pair> aba, bab;
- bool bracket = false;
- char t1, t2, t3 = '\x01', '\x02', '\x03';
- foreach(ch in ln)
- {
- t1, t2, t3 = t2, t3, ch;
- if('['==ch)
- {
- bracket = true;
- }
- elif(']'==ch)
- {
- bracket = false;
- }
- if(t1!=t2 &&& t1==t3)
- {
- if(bracket)
- {
- Pair p = Pair { t2, t1 };
- if(!has(bab, p)) { bab = hiext(bab, p); }
- }
- else
- {
- Pair p = Pair { t1, t2 };
- if(!has(aba, p)) { aba = hiext(aba, p); }
- }
- }
- }
- bool found = false;
- foreach(a in aba while !found)
- {
- found = has(bab, a);
- }
- if(found)
- {
- num = num + 1u;
- }
- }
- return num;
- }
- main
- {
- write(tos(part1()) + "\n");
- write(tos(part2()) + "\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement