adventofcode/2021/3/diag.pas
2022-12-01 21:02:00 +01:00

55 lines
991 B
ObjectPascal

PROGRAM diagnostics;
TYPE
vector = array [0..11] of integer;
VAR
filein : text;
mychar : char;
column, rows, i, gamma, epsilon : integer;
counts : vector;
BEGIN
column := 0;
rows := 0;
gamma := 0;
epsilon := 0;
for i := 0 to 11 do
counts[i] := 0;
assign(filein, 'input');
reset(filein);
repeat
begin
(* Keeping track of the total number of lines in the file *)
if(column = 0) then
rows := rows + 1;
read(filein, mychar);
if(mychar = '1') then
counts[column] := counts[column] + 1;
(* The mod 13 is not mod 12 because the newline
characher is also getting looped over. *)
column := (column + 1) mod 13;
end;
until eof(filein);
close(filein);
for i := 0 to 11 do begin
gamma := gamma shl 1;
epsilon := epsilon shl 1;
if(counts[i] > rows - counts[i]) then
gamma := gamma or 1
else
epsilon := epsilon or 1;
end;
writeln('gamma: ', gamma, ', epsilon: ', epsilon, ', power consumption: ', gamma * epsilon);
END.