Created
December 3, 2023 18:53
-
-
Save guaracy/144c4ef4f0dba3433686dfbb8a5fd419 to your computer and use it in GitHub Desktop.
day01
This file contains hidden or 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
PROGRAM day01; | |
{$mode objfpc}{$H+} | |
USES | |
StrUtils, | |
SysUtils, | |
uSuporte; | |
CONST | |
numbers1 = '0,1,2,3,4,5,6,7,8,9'; | |
numbers2 = '0,1,2,3,4,5,6,7,8,9,one,two,three,four,five,six,seven,eight,nine'; | |
FUNCTION resolve(fn, n: STRING): INTEGER; | |
VAR | |
line: STRING; | |
A: TStringArray; | |
len, p, v, v1, ix, v2: SizeInt; | |
BEGIN | |
A := n.Split(','); | |
Lines.LoadFromFile(fn); | |
Result := 0; | |
FOR line IN Lines DO BEGIN | |
len := line.Length; | |
p := line.IndexOfAny(A, 0, len, v); | |
IF v > 9 THEN v := v - 9; | |
v1 := v; | |
// l.LastIndexOfAny só aceita array de caracteres; | |
FOR ix := len DOWNTO 0 DO BEGIN | |
p := line.IndexOfAny(A, ix, 6, v); | |
IF p >= 0 THEN | |
break; | |
END; | |
IF v > 9 THEN v := v - 9; | |
v2 := v; | |
Result += v1 * 10 + v2; | |
END; | |
END; | |
BEGIN | |
writeln('sample1: ', resolve('day01.sample1', numbers1)); | |
writeln(' data1: ', resolve('day01.data1', numbers1)); | |
writeln('sample2: ', resolve('day01.sample2', numbers2)); | |
writeln(' data2: ', resolve('day01.data2', numbers2)); | |
END. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment