summaryrefslogtreecommitdiffstats
path: root/day8/day8-2.js
diff options
context:
space:
mode:
authorGravatar Piotr Russ <piotr.russ@betvictor.com> 2020-12-10 20:24:16 +0100
committerGravatar Piotr Russ <piotr.russ@betvictor.com> 2020-12-10 20:24:16 +0100
commitb50c9c40ae474cd0e4eb543504e47dc6dbea31e9 (patch)
tree9e03fce1d603e013847c5fbea780dd0bd2a62a47 /day8/day8-2.js
parentc0617b6edbae172752b58276a7678e8c97ade3e7 (diff)
downloadadvent_of_code_2020-b50c9c40ae474cd0e4eb543504e47dc6dbea31e9.tar.gz
advent_of_code_2020-b50c9c40ae474cd0e4eb543504e47dc6dbea31e9.tar.bz2
advent_of_code_2020-b50c9c40ae474cd0e4eb543504e47dc6dbea31e9.zip
day8
Diffstat (limited to 'day8/day8-2.js')
-rw-r--r--day8/day8-2.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/day8/day8-2.js b/day8/day8-2.js
new file mode 100644
index 0000000..c092f65
--- /dev/null
+++ b/day8/day8-2.js
@@ -0,0 +1,44 @@
+const data = require('./data')
+const array = data.map(s => s.split(' ')).map(s => [s[0], parseInt(s[1])])
+let cont, field, accu, spl, splitted
+let success = false, change = 0
+
+while (!success && change < array.length) {
+ splitted = JSON.parse(JSON.stringify(array))
+ if (splitted[change][0] === 'nop') {
+ splitted[change][0] = 'jmp'
+ } else if (splitted[change][0] === 'jmp') {
+ splitted[change][0] = 'nop'
+ }
+ cont = true
+ field = 0
+ accu = 0
+ spl = JSON.parse(JSON.stringify(splitted))
+ while (cont && !spl[field][2]) {
+ if (field === array.length - 1) {
+ success = accu
+ cont = false
+ }
+
+ switch(spl[field][0]) {
+ case 'nop':
+ spl[field].push(true)
+ field += 1
+ break
+ case 'acc':
+ spl[field].push(true)
+ accu += spl[field][1]
+ field += 1
+ break
+ case 'jmp':
+ spl[field].push(true)
+ field += spl[field][1]
+ break;
+ default:
+ cont = false
+ }
+ }
+ change += 1
+}
+
+console.log(success)