Mercurial > hg > aoc
comparison 2017/day19/app.d @ 33:bc652fa0a645
Move all solutions to per-day subdirs
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Tue, 09 Jan 2018 21:50:37 -0500 |
parents | 2017/day19.d@836d284fff87 |
children |
comparison
equal
deleted
inserted
replaced
32:763c88851b91 | 33:bc652fa0a645 |
---|---|
1 import std.stdio; | |
2 import std.array: array; | |
3 import std.algorithm: map; | |
4 import std.string: indexOf; | |
5 import std.typecons: tuple; | |
6 | |
7 auto walkPath(T)(T grid) { | |
8 char[] path; | |
9 long i = grid[0].indexOf("|"), j = 1; | |
10 int steps = 1; | |
11 enum DIR {u, d, l, r}; | |
12 DIR dir = DIR.d; | |
13 while(i > 0) { | |
14 steps++; | |
15 auto next = grid[j][i]; | |
16 | |
17 switch(next) { | |
18 case 'A': .. case 'Z': | |
19 path ~= next; | |
20 break; | |
21 case '+': | |
22 if(dir == DIR.u || dir == DIR.d) { | |
23 if(grid[j][i-1] == '-') { | |
24 dir = DIR.l; | |
25 } | |
26 else { | |
27 dir = DIR.r; | |
28 } | |
29 } | |
30 else { | |
31 if(grid[j-1][i] == '|') { | |
32 dir = DIR.u; | |
33 } | |
34 else { | |
35 dir = DIR.d; | |
36 } | |
37 } | |
38 break; | |
39 default: | |
40 } | |
41 | |
42 final switch(dir){ | |
43 case DIR.d: | |
44 j++; | |
45 break; | |
46 case DIR.u: | |
47 j--; | |
48 break; | |
49 case DIR.l: | |
50 i--; | |
51 break; | |
52 case DIR.r: | |
53 i++; | |
54 break; | |
55 } | |
56 | |
57 } | |
58 return tuple(path, steps); | |
59 } | |
60 | |
61 void main(string[] args) { | |
62 auto grid = File(args[1]).byLineCopy.array.dup; | |
63 auto ret = grid.walkPath; | |
64 writeln(ret[0]); | |
65 writeln(ret[1]); | |
66 } |