From 4276edc6ef6bf1465158f6dfbce86b785cf74958 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 21 Dec 2017 13:55:55 +0100 Subject: [PATCH] Part 1 for day 21. --- 2017/README.md | 3 +- 2017/day-21/input.txt | 108 ++++++++++++++++++++++++++++++++++++ 2017/day-21/solution.coffee | 81 +++++++++++++++++++++++++++ 3 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 2017/day-21/input.txt create mode 100644 2017/day-21/solution.coffee diff --git a/2017/README.md b/2017/README.md index 5701402..99e090e 100644 --- a/2017/README.md +++ b/2017/README.md @@ -12,7 +12,8 @@ The current plan, in no particular order: - [x] C++ - [Day 01](./day-01/solution.cpp) - [x] C# - [Day 07](./day-07/solution.cs) - [x] Clojure - [Day 03](./day-03/solution.clj) -- [ ] Coffeescript +- [ ] Coffeescript - [Day 21](./day-21/solution.coffee) (Need to finish + part 2) - [x] Go - [Day 13](./day-13/solution.go) - [x] Groovy - [Day 16](./day-16/solution.groovy) - [x] Haskell - [Day 15](./day-15/solution.hs) diff --git a/2017/day-21/input.txt b/2017/day-21/input.txt new file mode 100644 index 0000000..bda8318 --- /dev/null +++ b/2017/day-21/input.txt @@ -0,0 +1,108 @@ +../.. => .##/#../..# +#./.. => .##/#../### +##/.. => ..#/#.#/#.. +.#/#. => #../#../.#. +##/#. => .#./#../#.. +##/## => .##/.../.## +.../.../... => #.#./###./####/#..# +#../.../... => .###/####/##../#.## +.#./.../... => ###./.###/#..#/#.## +##./.../... => ..../..../.#../##.. +#.#/.../... => ...#/.##./..../##.. +###/.../... => ##../##../##.#/..## +.#./#../... => .#../###./##../#### +##./#../... => ####/##.#/..../..## +..#/#../... => ..#./####/...#/#.## +#.#/#../... => #.#./##../##../.##. +.##/#../... => ##../####/..#./...# +###/#../... => #..#/#.#./##.#/#.#. +.../.#./... => .#.#/..#./#.../.... +#../.#./... => ##../..##/..##/.#.. +.#./.#./... => ..../##../##../#.## +##./.#./... => ...#/##../#..#/.### +#.#/.#./... => ####/##.#/###./..## +###/.#./... => ..../...#/.###/.#.. +.#./##./... => #.#./#..#/.##./.#.# +##./##./... => .###/#.../#..#/#.#. +..#/##./... => .###/####/..../#.## +#.#/##./... => ...#/.###/.###/.### +.##/##./... => ..##/..##/.###/##.# +###/##./... => ####/#..#/####/#.#. +.../#.#/... => #.##/..#./.###/#.#. +#../#.#/... => ####/##.#/##.#/.... +.#./#.#/... => #.../...#/#.##/#..# +##./#.#/... => .#.#/##../##../.... +#.#/#.#/... => ##.#/#.../##../.#.. +###/#.#/... => ...#/###./.#.#/...# +.../###/... => .###/#.##/#.../###. +#../###/... => ..##/.#../.###/..#. +.#./###/... => ..../.##./#.##/#.## +##./###/... => .#.#/##.#/#.../#.#. +#.#/###/... => ..#./#.../#.#./.##. +###/###/... => ..##/.#.#/#..#/.##. +..#/.../#.. => ..##/.#../##.#/##.. +#.#/.../#.. => ..#./..../#.../...# +.##/.../#.. => .##./..##/####/#... +###/.../#.. => #.##/..../##../#.## +.##/#../#.. => .###/...#/###./.... +###/#../#.. => .#.#/#.#./#.##/..#. +..#/.#./#.. => ...#/..#./..##/.#.# +#.#/.#./#.. => #.../##.#/.###/#.#. +.##/.#./#.. => ###./####/#..#/##.# +###/.#./#.. => ..../..#./..../#... +.##/##./#.. => .#.#/.##./.#.#/#.## +###/##./#.. => ..../##../###./.#.# +#../..#/#.. => ...#/#.../#.##/.### +.#./..#/#.. => #..#/.#../###./#.#. +##./..#/#.. => #.#./..#./###./###. +#.#/..#/#.. => .#.#/##.#/##../#### +.##/..#/#.. => ###./..../.#../...# +###/..#/#.. => #.#./.##./.#.#/#..# +#../#.#/#.. => #.#./##.#/.#../.### +.#./#.#/#.. => ##.#/#.#./#.../#### +##./#.#/#.. => .#.#/#.../..#./#.## +..#/#.#/#.. => ##.#/.##./#.../.### +#.#/#.#/#.. => ..##/..../..../#### +.##/#.#/#.. => ####/#.#./###./.#.# +###/#.#/#.. => #.##/..#./##../#... +#../.##/#.. => ..##/##.#/####/.#.. +.#./.##/#.. => ..##/##../.#../..## +##./.##/#.. => ..##/.#.#/#..#/.... +#.#/.##/#.. => #.../##../...#/.#.# +.##/.##/#.. => ##../...#/.###/.#.# +###/.##/#.. => ####/..#./.##./#.## +#../###/#.. => .#.#/##.#/#.#./#.#. +.#./###/#.. => .###/#..#/.#.#/###. +##./###/#.. => ##../.#../###./.#.# +..#/###/#.. => #.##/..../...#/..#. +#.#/###/#.. => #.../#..#/..../.#.. +.##/###/#.. => ####/#..#/..#./.#.# +###/###/#.. => .##./##../.#../..#. +.#./#.#/.#. => #.#./.###/#.#./..## +##./#.#/.#. => .##./..../..##/##.. +#.#/#.#/.#. => ...#/..../.#.#/..## +###/#.#/.#. => .#../####/#.#./#.## +.#./###/.#. => #..#/.#.#/#..#/#.#. +##./###/.#. => .#../##../#..#/..## +#.#/###/.#. => #.#./.##./##.#/.#.# +###/###/.#. => #.#./...#/..##/#... +#.#/..#/##. => ..#./..#./...#/#..# +###/..#/##. => #..#/###./..../##.# +.##/#.#/##. => #.##/.#.#/...#/..## +###/#.#/##. => #.##/...#/.##./.### +#.#/.##/##. => ..../##.#/..../...# +###/.##/##. => .###/#.../###./###. +.##/###/##. => #.../#.#./.###/..#. +###/###/##. => #.##/.#../..#./.#.# +#.#/.../#.# => .##./##../###./.### +###/.../#.# => ..##/...#/###./.#.. +###/#../#.# => ##.#/..#./#.##/.#.. +#.#/.#./#.# => .#../#.##/...#/###. +###/.#./#.# => ..#./..../####/#### +###/##./#.# => ###./#..#/..../#..# +#.#/#.#/#.# => ##.#/###./..../#... +###/#.#/#.# => ##../.###/#..#/.#.. +#.#/###/#.# => #.../###./.###/..#. +###/###/#.# => ..../.##./.#../###. +###/#.#/### => ##../#.../.###/#... +###/###/### => .###/###./#.##/..#. diff --git a/2017/day-21/solution.coffee b/2017/day-21/solution.coffee new file mode 100644 index 0000000..a1979cd --- /dev/null +++ b/2017/day-21/solution.coffee @@ -0,0 +1,81 @@ +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..5] + 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)) + view state