-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.coffee
executable file
·81 lines (57 loc) · 1.72 KB
/
solution.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env coffee
String.prototype.count = (pattern) ->
(this.match(pattern) || []).length
stdin = process.openStdin()
stdin.setEncoding 'utf8'
data = ''
stdin.on 'data', (input) ->
data += input
stdin.on 'end', () ->
mappings = {}
rotate = (line) ->
rows = line.split '/'
newrows = []
len = rows.length
for i in [0...len]
newrows[i] = ''
for j in [0...len]
newrows[i] += rows[rows.length - 1 - j].substr i, 1
newrows.join '/'
flip = (line) ->
(row.split('').reverse().join '' for row in line.split '/').join '/'
view = (state) ->
console.log state.split('/').join "\n"
for line in data.trim().split('\n')
do (line) ->
[source, enhanced] = line.split ' => '
for i in [1..4]
mappings[source] = enhanced
mappings[flip source] = enhanced
source = rotate source
state = '.#./..#/###'
view state
for _ in [1..18]
newrows = []
oldrows = state.split '/'
if oldrows.length % 2 == 0
for i in [0...(oldrows.length / 2)]
currows = ['', '', '']
for j in [0...(oldrows.length / 2)]
curblock = (oldrows[2 * i].substr 2 * j, 2) + '/' + (oldrows[2 * i + 1].substr 2 * j, 2)
blockrows = mappings[curblock].split '/'
for r in [0..2]
currows[r] += blockrows[r]
for row in currows
newrows.push row
else
for i in [0...(oldrows.length / 3)]
currows = ['', '', '', '']
for j in [0...(oldrows.length / 3)]
curblock = (oldrows[3 * i].substr 3 * j, 3) + '/' + (oldrows[3 * i + 1].substr 3 * j, 3) + '/' + (oldrows[3 * i + 2].substr 3 * j, 3)
blockrows = mappings[curblock].split '/'
for r in [0..3]
currows[r] += blockrows[r]
for row in currows
newrows.push row
state = newrows.join '/'
console.log _, state.count(/#/g)