Day 3 part 1 in pascal!

This commit is contained in:
Steph 2022-12-01 21:02:00 +01:00
parent b55cc9aa20
commit e6481079d6
3 changed files with 70 additions and 0 deletions

5
2021/3/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
diag.o
diag
diag2
diag2.o
input

11
2021/3/Makefile Normal file
View File

@ -0,0 +1,11 @@
.PHONY := all build run
all: build run
build:
fpc -odiag diag.pas
fpc -odiag2 diag2.pas
run:
./diag
./diag2

54
2021/3/diag.pas Normal file
View File

@ -0,0 +1,54 @@
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.