-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.js
91 lines (70 loc) · 2.35 KB
/
day02.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict'
// Part 1
// ======
// K like didn't wanna waste time w/ fs sorry
var values = require('./files/day02')
const splitStringIntoArrays = (string) => {
const newlinePattern = /\n/
const tabPattern = /\t/
const splitStringIntoRows = values.split(newlinePattern)
const splitRowsIntoCells = splitStringIntoRows.reduce((acc, current) => {
acc.push(current.split(tabPattern))
return acc
}, [])
return splitRowsIntoCells
}
const sumValues = (arr) => {
return arr.reduce((acc, current) => {
return acc + current
}, 0)
}
const part1 = input => {
const rowsWithCells = splitStringIntoArrays(values)
const getDifferencePerRow = rowsWithCells.reduce((acc, current) => {
let high, low
for (let i = 0; i < current.length; i += 1) {
if (i === 0) {
high = parseInt(current[0])
low = parseInt(current[0])
} else {
high = parseInt(current[i]) > high ? parseInt(current[i]) : high
low = parseInt(current[i]) < low ? parseInt(current[i]) : low
}
}
acc.push(high - low)
return acc
}, [])
const checkSum = sumValues(getDifferencePerRow)
return checkSum
}
// Part 2
// ======
const part2 = input => {
const rowsWithCells = splitStringIntoArrays(values)
// Per row
const quotientPerRow = rowsWithCells.reduce((acc, current, i) => {
let quotient = 0
// Per cell
for (let cellIndex = 0; cellIndex < current.length; cellIndex += 1) {
// Iterate over all other cells in order to compare
for (let compareIndex = 0; compareIndex < current.length; compareIndex += 1) {
// And unless current cell's index === cell to compare's index (cause x % x = 0)
if (cellIndex !== compareIndex) {
// Compare if current / comparer has no remainder (or inverse), and immediately short circuit via return
if (current[cellIndex] % current[compareIndex] === 0) {
quotient = current[cellIndex] / current[compareIndex]
acc.push(quotient)
return acc
} else if (current[compareIndex] % current[cellIndex] === 0) {
quotient = current[compareIndex] / current[cellIndex]
acc.push(quotient)
return acc
}
}
}
}
}, [])
const sumQuotients = sumValues(quotientPerRow)
return sumQuotients
}
module.exports = { part1, part2 }