Skip to content

Instantly share code, notes, and snippets.

@guaracy
Created December 3, 2023 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guaracy/144c4ef4f0dba3433686dfbb8a5fd419 to your computer and use it in GitHub Desktop.
Save guaracy/144c4ef4f0dba3433686dfbb8a5fd419 to your computer and use it in GitHub Desktop.
day01
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