-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.p
59 lines (42 loc) · 2.67 KB
/
day5.p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
--- Day 5: Alchemical Reduction ---
You've managed to sneak in to the prototype suit manufacturing lab. The Elves are making decent progress, but are still struggling with the suit's size reduction capabilities.
While the very latest in 1518 alchemical technology might have solved their problem eventually, you can do better. You scan the chemical composition of the suit's material and discover that it is formed by extremely long polymers (one of which is available as your puzzle input).
The polymer is formed by smaller units which, when triggered, react with each other such that two adjacent units of the same type and opposite polarity are destroyed. Units' types are represented by letters; units' polarity is represented by capitalization. For instance, r and R are units with the same type but opposite polarity, whereas r and s are entirely different types and do not react.
For example:
In aA, a and A react, leaving nothing behind.
In abBA, bB destroys itself, leaving aA. As above, this then destroys itself, leaving nothing.
In abAB, no two adjacent units are of the same type, and so nothing happens.
In aabAAB, even though aa and AA are of the same type, their polarities match, and so nothing happens.
Now, consider a larger example, dabAcCaCBAcCcaDA:
dabAcCaCBAcCcaDA The first 'cC' is removed.
dabAaCBAcCcaDA This creates 'Aa', which is removed.
dabCBAcCcaDA Either 'cC' or 'Cc' are removed (the result is the same).
dabCBAcaDA No further actions can be taken.
After all possible reactions, the resulting polymer contains 10 units.
How many units remain after fully reacting the polymer you scanned? (Note: in this puzzle and others, the input is large; if you copy/paste your input, make sure you get the whole thing.)
*/
DEFINE VARIABLE c AS CHARACTER NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE iLength AS INTEGER INITIAL ? NO-UNDO.
DEFINE VARIABLE lcPolymer AS LONGCHAR CASE-SENSITIVE NO-UNDO.
FUNCTION react RETURNS LOGICAL:
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE iLength AS INTEGER NO-UNDO.
iLength = LENGTH(lcPolymer).
DO i = 1 TO 26:
lcPolymer = REPLACE(
REPLACE(lcPolymer, CHR(96 + i) + CHR(64 + i), ""),
CHR(64 + i) + CHR(96 + i), "").
END.
RETURN iLength <> LENGTH(lcPolymer).
END FUNCTION.
ETIME(YES).
COPY-LOB FILE "C:\User\JCCARDOT\Perso\Travail\aoc\aoc2018\day5.txt" TO lcPolymer.
DO WHILE react():
END.
MESSAGE ETIME SKIP
LENGTH(lcPolymer)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/* 1238 */
/* 10804 */