diff --git a/2021/3/.gitignore b/2021/3/.gitignore new file mode 100644 index 0000000..b0b7ede --- /dev/null +++ b/2021/3/.gitignore @@ -0,0 +1,5 @@ +diag.o +diag +diag2 +diag2.o +input diff --git a/2021/3/Makefile b/2021/3/Makefile new file mode 100644 index 0000000..10cca24 --- /dev/null +++ b/2021/3/Makefile @@ -0,0 +1,11 @@ +.PHONY := all build run + +all: build run + +build: + fpc -odiag diag.pas + fpc -odiag2 diag2.pas + +run: + ./diag + ./diag2 diff --git a/2021/3/diag.pas b/2021/3/diag.pas new file mode 100644 index 0000000..9d150f2 --- /dev/null +++ b/2021/3/diag.pas @@ -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. + +