-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.coffee
59 lines (53 loc) · 1.05 KB
/
19.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
{_log,_print,test,expect,testAndRun} = require './util'
ansi = require('ansicolor').nice
class Solver
constructor: ( @input )->
@map = @input.split '\n'
@map.shift() if @map[0][0] is '.'
return
solve: ->
y = 0
x = @map[0].indexOf '|'
dx = 0
dy = 1
out = ''
steps = 1
loop
x+=dx
y+=dy
c=@map[y][x]
# _log x, y, c
switch c
when ' ', undefined, null
#_log.darkGray 'path end', x, y, dx, dy, "'#{c}'"
return [out, steps]
when '|', '-'
break # does nothing
when '+'
[dx,dy]=[dy,dx]
if (@map[y+dy]?[x+dx] ? ' ') is ' '
dx = -dx
dy = -dy
else
out = out + c
++steps
_log.red 'this should never execute'
return
test.solve = ->
s = new Solver '''
.
|
| +--+
A | C
F---|----E|--+
| | | D
+B-+ +--+
'''
expect.equal ['ABCDEF',38], s.solve()
return
testAndRun ->
s = new Solver require './input/19.txt'
[out,steps] = s.solve()
_log.yellow '1:', out
_log.yellow '2:', steps
return