-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5pt1.ts
70 lines (53 loc) · 1.4 KB
/
day5pt1.ts
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
const input = await Deno.readTextFile('./day5input.txt')
const inputData = input.split(/\n{2}/g)
// const inputData = `47|53
// 97|13
// 97|61
// 97|47
// 75|29
// 61|13
// 75|53
// 29|13
// 97|29
// 53|29
// 61|53
// 97|53
// 61|29
// 47|13
// 75|47
// 97|75
// 47|61
// 75|61
// 47|29
// 75|13
// 53|13
// 75,47,61,53,29
// 97,61,53,29,13
// 75,29,13
// 75,97,47,61,53
// 61,13,29
// 97,13,75,29,47`.split(/\n{2}/g)
const pageOrderingRules = inputData[0].split(/\n/g)
const pagesToProduce = inputData[1].split(/\n/g)
console.log(pageOrderingRules, pagesToProduce)
const updatesSplitted = pagesToProduce.map((item) => item.split(','))
console.log(updatesSplitted)
const correctUpdates = updatesSplitted.reduce((acc, item: array) => {
// console.log(splittedItems)
//
const pagesDict = item.reduce( (acc, item, i) => { return {...acc, [item]: i} }, {} )
const corretlyOrdered = pageOrderingRules.every(orderRule => {
const rule = orderRule.split('|')
if(pagesDict[rule[0]] === undefined || pagesDict[rule[1]] === undefined) return true
return pagesDict[rule[0]] < pagesDict[rule[1]]
})
console.log('Is correctly ordered: ', corretlyOrdered)
if(corretlyOrdered) {
const medianIndex = parseInt(Number(item.length) / 2)
console.log(item, medianIndex, item[medianIndex])
return acc + Number(item[medianIndex])
}
return acc
}, 0)
console.log(correctUpdates)
console.log('Correct response:', 4959)