Skip to content

Commit 84b5427

Browse files
committed
Day-20
1 parent 98af586 commit 84b5427

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

day-20/day-20.dylan

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
Module: day-20
2+
Synopsis:
3+
Author:
4+
Copyright:
5+
6+
define function make-binary (str :: <string>) => (_ :: <string>)
7+
for (i from 0 below size(str))
8+
if (str[i] == '.')
9+
str[i] := '0';
10+
else
11+
str[i] := '1';
12+
end
13+
end;
14+
str;
15+
end;
16+
17+
define function print-image (image :: <sequence>) => ()
18+
format-out("image:\n");
19+
for (i in image)
20+
format-out(" %=\n", i);
21+
end;
22+
end;
23+
24+
define function read-input (file :: <string>) => (str :: <string>, image :: <sequence>)
25+
with-open-file(file-stream = file)
26+
let image-enhancement-algorithm = make-binary(read-line(file-stream));
27+
// Blank
28+
read-line(file-stream);
29+
let image = make(<stretchy-vector>);
30+
while (~stream-at-end?(file-stream))
31+
add!(image, make-binary(read-line(file-stream)));
32+
end;
33+
values(image-enhancement-algorithm, image);
34+
end;
35+
end;
36+
37+
define function add-fill-around-twice (image :: <sequence>, fill :: <character>) => (larger-image :: <sequence>)
38+
let col-size = size(image);
39+
let row-size = size(image[0]);
40+
let new-image = make(<stretchy-vector>);
41+
add!(new-image, make(<string>, size: row-size + 4, fill: fill));
42+
add!(new-image, make(<string>, size: row-size + 4, fill: fill));
43+
for (i in image)
44+
let new-i = format-to-string("%c%c%s%c%c", fill, fill, i, fill, fill);
45+
add!(new-image, new-i);
46+
end;
47+
add!(new-image, make(<string>, size: row-size + 4, fill: fill));
48+
add!(new-image, make(<string>, size: row-size + 4, fill: fill));
49+
new-image;
50+
end;
51+
52+
define function get-3-3-box-int(image :: <sequence>, row :: <integer>, col :: <integer>) => (int :: <integer>)
53+
let rows = make(<stretchy-vector>);
54+
for (i from row - 1 to row + 1)
55+
add!(rows, copy-sequence(image[i], start: col - 1, end: col + 2));
56+
end;
57+
// print-image(rows);
58+
let int-string = format-to-string("%s%s%s", rows[0], rows[1], rows[2]);
59+
string-to-integer(int-string, base: 2);
60+
end;
61+
62+
define function increase-level(iea :: <string>, image :: <sequence>, #key first? = #f) => (new-image :: <sequence>)
63+
let fill = image[0][0];
64+
// format-out("fill: %c\n", fill);
65+
let larger-image = add-fill-around-twice(image, fill);
66+
let copy-larger-image = map(copy-sequence, larger-image);
67+
for (row from 0 below size(larger-image))
68+
for (col from 0 below size(larger-image[0]))
69+
let iea-index = 0;
70+
if (row = 0
71+
| col = 0
72+
| row = (size(larger-image) - 1)
73+
| col = (size(larger-image[0]) - 1))
74+
if (fill = '1')
75+
// format-out ("iea-index 511 char: %c\n", iea[iea-index]);
76+
iea-index := 511;
77+
end;
78+
else
79+
iea-index := get-3-3-box-int(larger-image, row, col);
80+
end;
81+
let iea-char = iea[iea-index];
82+
copy-larger-image[row][col] := iea-char;
83+
end;
84+
end;
85+
//print-image(copy-larger-image);
86+
copy-larger-image;
87+
end;
88+
89+
define function count-on(image :: <sequence>) => (count :: <integer>)
90+
//let int-image = map(get-int-vector-from-string, image);
91+
//let int-values = map(method (a) reduce1(\+, a) end, int-image);
92+
//reduce1(\+, int-values);
93+
let sum = 0;
94+
for (i in image)
95+
for (j in i)
96+
if (j = '1')
97+
sum := sum + 1;
98+
end;
99+
end;
100+
end;
101+
sum
102+
end;
103+
104+
define function part-1 (filename) => ()
105+
let (iea, image) = read-input(filename);
106+
image := add-fill-around-twice(image, '0');
107+
image := increase-level(iea, image, first?: #t);
108+
for (i from 1 below 2)
109+
image := increase-level(iea, image);
110+
end;
111+
format-out("Part 1 On count: %d \n", count-on(image));
112+
end;
113+
114+
define function part-2 (filename) => ()
115+
let (iea, image) = read-input(filename);
116+
image := add-fill-around-twice(image, '0');
117+
image := increase-level(iea, image, first?: #t);
118+
for (i from 1 below 50)
119+
image := increase-level(iea, image);
120+
end;
121+
format-out("Part 2 On count: %d \n", count-on(image));
122+
end;
123+
124+
define function main
125+
(name :: <string>, arguments :: <vector>)
126+
part-1(arguments[0]);
127+
part-2(arguments[0]);
128+
exit-application(0);
129+
end function main;
130+
131+
main(application-name(), application-arguments());

day-20/day-20.lid

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Library: day-20
2+
Files: library
3+
day-20

day-20/library.dylan

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Module: dylan-user
2+
3+
define library day-20
4+
use common-dylan;
5+
use io;
6+
use generic-arithmetic;
7+
use system;
8+
use strings;
9+
use shared;
10+
end library day-20;
11+
12+
define module day-20
13+
use common-dylan;
14+
use common-extensions;
15+
use strings;
16+
use file-system;
17+
use format-out;
18+
use format;
19+
use streams;
20+
use shared;
21+
end module day-20;

registry/generic/day-20

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
abstract://dylan/day-20.lid

0 commit comments

Comments
 (0)