Skip to content

Commit

Permalink
2021 18
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 23, 2023
1 parent dc22014 commit a2bdb41
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 165 deletions.
194 changes: 31 additions & 163 deletions 2021/18/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ export async function taskOne(input: string[]): Promise<void> {
let sum = input[0]
for (let i = 1; i < input.length; i++) {
sum = add(sum, input[i])
console.log("sum" + sum)
console.log(" ")
}
console.log(magnitude(sum))
}

export async function taskTwo(input: string[]): Promise<void> {
console.log("Unimplemented");
let max = 0
for (let i = 0; i < input.length; i++) {
for (let j = 0; j < input.length; j++) {
const mag = magnitude(add(input[i], input[j]))
if (mag > max) {
max = mag
}
}
}
console.log(max)
}

const NUMS = [0,1,2,3,4,5,6,7,8,9].map(i=>i.toString())
Expand All @@ -21,33 +29,34 @@ function reduce(s: string) {
let didAction = false
let _i = 0
do {
console.log(s)
_i++
if (_i > 20 ) return ""
let open = 0
didAction = false
for (let i = 0; i < s.length; i++) {
if (s[i] == '[') open++
if (s[i] == ']') open--
if (open > 4) {
console.log(1)
const pairEnd = s.indexOf(']', i+1)
const toMuch = s.slice(i,pairEnd+1)
//console.log(toMuch)
const r = /\[([0-9]+),([0-9]+)\]/.exec(toMuch)
if (r == null) throw toMuch
for (let j = pairEnd+1; j < s.length; j++) {
if (NUMS.includes(s[j])) {
const goal = (parseInt(s[j]) + parseInt(r[2])).toString()
s = s.slice(0, j) + goal + s.slice(j+1)
const st = j
let en = j
while(NUMS.includes(s[en])) {en++}
const goal = (parseInt(s.slice(st,en)) + parseInt(r[2])).toString()
s = s.slice(0, j) + goal + s.slice(en)
break;
}
}
s = s.slice(0, i) + '0' + s.slice(pairEnd+1)
for (let j = i-1; j >= 0; j--) {
if (NUMS.includes(s[j])) {
const goal = (parseInt(s[j]) + parseInt(r[1])).toString()
s = s.slice(0, j) +goal+s.slice(j+1)
const en = j + 1
let st = j
while(NUMS.includes(s[st])) {st--}
const goal = (parseInt(s.slice(st+1, en)) + parseInt(r[1])).toString()
s = s.slice(0, st+1) +goal+s.slice(en)
break;
}
}
Expand All @@ -66,168 +75,27 @@ function reduce(s: string) {
while(NUMS.includes(s[j])) {j++}
const en = j
if (en-st > 1) {
console.log(2)
const num = parseInt(s.slice(st,en))

//console.log(num, s.slice(0, st), s.slice(en))
s = s.slice(0,st) + '['+Math.floor(num/2).toString()+','+ Math.ceil(num/2).toString() + ']'+ s.slice(en)
didAction = true
break;
}
}
//console.log("2", s)
} while(didAction)
return s
}



type Pair = {
left: Pair|number
right: Pair|number
parent: Pair
depth: number
}
/*
function add(a:Pair, b:Pair) {
console.log("add")
function increaseD(x: Pair) {
x.depth++
if ((x.left as Pair).depth != undefined) increaseD(x.left as Pair)
if ((x.right as Pair).depth != undefined) increaseD(x.right as Pair)
}
const temp: Pair = {left:a,right:b, depth:0, parent: {} as Pair}
a.parent = temp
b.parent = temp
console.log("added to: ", toString(temp))
increaseD(a)
increaseD(b)
console.log("increased depth")
function split(x: Pair): boolean {
if((x.left as Pair).depth != undefined) {
if (split(x.left as Pair)) return true
} else if ((x.left as number) > 9) {
const num = x.left as number
x.left = {
left: Math.floor(num/2),
right: Math.ceil(num/2),
parent: x,
depth: x.depth + 1
}
return true
}
if((x.right as Pair).depth != undefined) {
if (split(x.right as Pair)) return true
} else if ((x.right as number) > 9) {
const num = x.right as number
x.left = {
left: Math.floor(num/2),
right: Math.ceil(num/2),
parent: x,
depth: x.depth + 1
}
return true
}
return false
}
function explode(x: Pair): boolean {
if (x.depth >= 3) {
if (x.parent?.left == x) {
const newR = (x.parent.right as number) + (x.right as number)
x.parent.right = newR
if (x.parent.parent.left != x.parent) {
const newL = (x.parent.parent.left as number) + (x.left as number)
x.parent.parent.left = newL
} else if (x.parent.parent.parent.left != x.parent.parent) {
const newL = (x.parent.parent.parent.left as number) + (x.left as number)
x.parent.parent.parent.left = newL
}
} else {
const newL = (x.parent.left as number) + (x.left as number)
x.parent.left = newL
if (x.parent.parent.right != x.parent) {
const newR = (x.parent.parent.right as number) + (x.right as number)
x.parent.parent.right = newR
} else if (x.parent.parent.parent?.right != x.parent?.parent) {
const newR = (x.parent.right as number) + (x.right as number)
x.parent.parent.parent.right = newR
}
}
return true
}
if ((x.left as Pair) != undefined) {
if (explode(x.left as Pair)) {
return true
}
}
if ((x.right as Pair) != undefined) {
if (explode(x.right as Pair)) {
return true
}
}
return false
}
let didAction = true
while(didAction) {
didAction = false
if (explode(temp)) {
didAction = true
console.log("explode", toString(temp))
break
}
if (split(temp)) {
console.log("split")
didAction = true
}
}
return temp
}
function parse(input: string) {
type _P = [_P|number, _P|number]
const inp = JSON.parse(input) as _P
function par(x: _P, d: number, p?: Pair) {
let left = x[0]
let right = x[1]
const temp = {
depth: d,
parent: p,
left: undefined as unknown,
right: undefined as unknown
}
if ((left as _P).length != undefined) {
temp.left = par(left as _P, d+1, temp as Pair)
} else {
temp.left = left
}
if ((right as _P).length != undefined) {
temp.right = par(right as _P, d+1, temp as Pair)
function magnitude(num: string) {
type Pair =number|[Pair,Pair]
const snail = JSON.parse(num) as Pair
function mag(p: Pair): number {
if ((p as any[]).length != undefined) {
const _p = p as [Pair, Pair]
return 3*mag(_p[0]) + 2*mag(_p[1])
} else {
temp.right = right
return p as number
}
return temp as Pair
}
return par(inp, 0, {} as Pair)
}
function toString(p: Pair): string {
let temp = '['
if ((p.left as Pair).left != undefined) {
temp += toString(p.left as Pair)
} else {
temp += p.left
}
temp += ','
if ((p.right as Pair).left != undefined) {
temp += toString(p.right as Pair)
} else {
temp += p.right
}
temp += ']'
return temp
}*/
return mag(snail)
}
2 changes: 1 addition & 1 deletion 2021/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 2021
![](https://img.shields.io/badge/stars%20⭐-24-yellow)
![](https://img.shields.io/badge/stars%20⭐-36-yellow)

Puzzles not solved during event.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

<h3><a href="2021/README.md">2021</a></h3>

![](https://img.shields.io/badge/stars%20⭐-24-yellow)
![](https://img.shields.io/badge/stars%20⭐-36-yellow)

## Try it yourself
https://adventofcode.com
Expand Down

0 comments on commit a2bdb41

Please sign in to comment.