Skip to content

Commit

Permalink
day 20
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnBakhmat committed Dec 20, 2022
1 parent a133b62 commit 1d1229a
Show file tree
Hide file tree
Showing 5 changed files with 5,284 additions and 1 deletion.
2 changes: 1 addition & 1 deletion day-19/sol.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.13 (main, Aug 25 2022, 18:29:29) \n[Clang 12.0.0 ]"
},
"orig_nbformat": 4,
"vscode": {
Expand Down
59 changes: 59 additions & 0 deletions day-20/1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export {}
const text = await Deno.readTextFile('input.txt')

const key = 811589153
let array = text
.split('\n')
.map(Number)
.map((n) => n * key)

const swap = (i: number, j: number) => {
const temp = array[i]
array[i] = array[j]
array[j] = temp
}

const getCircularIndex = (i: number) => {
return ((i % array.length) + array.length) % array.length
}

const swapForward = (value: number) => {
const i = array.indexOf(value)

for (let j = 0; j < value; j++) {
const a = getCircularIndex(i + j)
const b = getCircularIndex(i + j + 1)
swap(a, b)
}
}
const swapBackward = (value: number) => {
const i = array.indexOf(value)

for (let j = 0; j < Math.abs(value); j++) {
const a = getCircularIndex(i - j)
const b = getCircularIndex(i - j - 1)
swap(a, b)
}
}
const persistArray = [...array]

persistArray.forEach((value, i) => {
if (value > 0) {
swapForward(value)
} else {
swapBackward(value)
}
})

const zeroIndex = array.indexOf(0)

const thousandth = array[getCircularIndex(zeroIndex + 1000)]
const twoThousandth = array[getCircularIndex(zeroIndex + 2000)]
const threeThousandth = array[getCircularIndex(zeroIndex + 3000)]

console.log(array, {
thousandth,
twoThousandth,
threeThousandth,
sum: thousandth + twoThousandth + threeThousandth,
})
1 change: 1 addition & 0 deletions day-20/2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
Loading

0 comments on commit 1d1229a

Please sign in to comment.