summaryrefslogtreecommitdiffstats
path: root/day20/day20-1.js
diff options
context:
space:
mode:
authorGravatar Piotr Russ <mail@pruss.it> 2020-12-21 08:00:20 +0100
committerGravatar Piotr Russ <mail@pruss.it> 2020-12-21 08:00:20 +0100
commit50fd0627cc74b72b48775e78271a11ab1bcd50dd (patch)
treedbdc36f0e23740e7f43b709bc45d0cc0bb8e8adf /day20/day20-1.js
parent00614a0fcedb45b9d986d92912c6ad84e7217e49 (diff)
downloadadvent_of_code_2020-50fd0627cc74b72b48775e78271a11ab1bcd50dd.tar.gz
advent_of_code_2020-50fd0627cc74b72b48775e78271a11ab1bcd50dd.tar.bz2
advent_of_code_2020-50fd0627cc74b72b48775e78271a11ab1bcd50dd.zip
day20
Diffstat (limited to 'day20/day20-1.js')
-rw-r--r--day20/day20-1.js40
1 files changed, 40 insertions, 0 deletions
diff --git a/day20/day20-1.js b/day20/day20-1.js
new file mode 100644
index 0000000..865410d
--- /dev/null
+++ b/day20/day20-1.js
@@ -0,0 +1,40 @@
+const data = require('./data')
+const tiles = []
+const getSides = (t) => {
+ const sides = [t[0], t.map(r=>r[r.length-1]).join(''), t[t.length-1], t.map(r=>r[0]).join('')]
+ const reversed = sides.map(s => s.split("").reverse().join(""))
+ return [...sides, ...reversed]
+}
+
+data
+ .split('\n\n')
+ .map(s => s.split('\n').filter(Boolean))
+ .forEach(s => tiles.push({
+ id: parseInt(s[0].match(/[0-9]+/g)[0]),
+ array: s.slice(1),
+ sides: getSides(s.slice(1)),
+ matches: []
+ }))
+
+const check = (t1, t2) => {
+ for (const s1 of t1.sides) for (const s2 of t2.sides) {
+ if (s1 === s2) {return s1}
+ }
+ return null
+}
+
+for (let i=0; i< tiles.length; i++) for (let j=i+1; j<tiles.length; j++) {
+ const match = check(tiles[i], tiles[j])
+ if (match) {
+ tiles[i].matches.push({ id: tiles[j].id, side: match })
+ tiles[j].matches.push({ id: tiles[i].id, side: match })
+ }
+}
+
+const res = tiles
+ .filter(t => t.matches.length === 2)
+ .map(t => t.id)
+ .reduce((a, b) => a * b, 1)
+
+console.log('Answer: ', res)
+