summaryrefslogtreecommitdiffstats
path: root/day11/day11-1.js
diff options
context:
space:
mode:
authorGravatar Piotr Russ <piotr.russ@betvictor.com> 2020-12-12 10:38:29 +0100
committerGravatar Piotr Russ <piotr.russ@betvictor.com> 2020-12-12 10:38:29 +0100
commit2fdbbd79bed58399ab63f92ece74fe6abb450b9c (patch)
tree0db1e2966276d22d3972c2d3ef0a89fc22affdb0 /day11/day11-1.js
parent468e261bccd8fc44ee6ffd3e7fda1c3659a7ab1d (diff)
downloadadvent_of_code_2020-2fdbbd79bed58399ab63f92ece74fe6abb450b9c.tar.gz
advent_of_code_2020-2fdbbd79bed58399ab63f92ece74fe6abb450b9c.tar.bz2
advent_of_code_2020-2fdbbd79bed58399ab63f92ece74fe6abb450b9c.zip
day11
Diffstat (limited to 'day11/day11-1.js')
-rw-r--r--day11/day11-1.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/day11/day11-1.js b/day11/day11-1.js
new file mode 100644
index 0000000..7d495b6
--- /dev/null
+++ b/day11/day11-1.js
@@ -0,0 +1,43 @@
+const data = require('./data')
+const rows = data.split(/\n/g).filter(r => r)
+const rowLn = rows[0].length
+const string = rows.join('')
+
+const checkSeat = (i, s) => {
+ const around = [
+ i >= rowLn ? s[i-rowLn] : '.', // top
+ i >= rowLn && (i+1)%rowLn !== 0 ? s[i+1-rowLn] : '.', // top-right
+ (i+1)%rowLn !== 0 ? s[i+1] : '.', // right
+ i < s.length -rowLn && (i+1)%rowLn !== 0 ? s[i+1+rowLn] : '.', // bottom-right
+ i < s.length -rowLn ? s[i+rowLn] : '.', // bottom
+ i%rowLn !== 0 && i < s.length -rowLn ? s[i-1+rowLn] : '.', // bottom-left
+ i%rowLn !== 0 ? s[i-1] : '.', // left
+ i > rowLn && i%rowLn > 0 ? s[i-1-rowLn] : '.', // top-left
+ ]
+
+ const taken = around.filter(s => s === '#').length
+ const free = around.filter(s => s === 'L').length
+
+ return [taken, free]
+}
+
+const round = (str) => {
+ const newArray = []
+ str.split('').forEach((s, i) => {
+ const [taken, free] = checkSeat(i, str)
+ if (s === 'L' && taken === 0) {
+ newArray[i] = '#'
+ } else if (s === '#' && taken >= 4) {
+ newArray[i] = 'L'
+ } else {
+ newArray[i] = str[i]
+ }
+ })
+
+ const newString = newArray.join('')
+ return newString === str
+ ? newString.split('').filter(s => s === '#').length
+ : round(newString)
+}
+
+console.log(round(string))