-
Notifications
You must be signed in to change notification settings - Fork 0
/
2313.ts
111 lines (88 loc) · 2.25 KB
/
2313.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let day = 13
import { config } from "https://deno.land/x/dotenv/mod.ts"
config()
let session = Deno.env.get("AOC_SESSION")
let get_stuff = (day): Promise<string> => {
let url: string = `https://adventofcode.com/2023/day/${day}/input`
console.log(session)
let headers = new Headers({ 'Cookie': `session=${session}` })
return fetch(url, { headers })
.then((resp) => resp.text())
.catch((err) => { throw err })
}
get_stuff(day).then((infile) => {
let raw: string[] = infile.split('\n')
raw.pop()
let lines:string[][][] = []
let temp:string[][] = []
for (let line of raw) {
if ( line.length === 0 ) {
lines.push( temp )
temp = []
} else {
temp.push( Array.from( line ) )
}
}
// get the last left-alone block
if (temp.length) {
lines.push( temp )
}
let [part1, part2] = [0, 0]
//console.log(lines)
// Solver
let coordinates: number[][] = []
let p2 = true
let lhs, rhs, axis
let i = -1
while (++i < lines.length) {
// for (const bloc of lines) {
// transpose it yea !
// ++nth_bloc
let bloc: string[][] = lines[i]
let tp: string[][] = bloc[0].map((_, col) => bloc.map((row) => row[col]))
lhs = horver_calculator(bloc)
rhs = horver_calculator(tp)
part1 += lhs * 100 + rhs
axis = lhs !== 0 ? 1 : 2
coordinates.push([1, i, axis, lhs + rhs])
lhs = horver_calculator(bloc, p2)
rhs = horver_calculator(tp, p2)
part2 += lhs * 100 + rhs
axis = lhs !== 0 ? 1 : 2
coordinates.push([2, i, axis, lhs + rhs])
}
console.log("part 1:", part1)
console.log("part 2:", part2)
}).catch((err) => { throw err })
const horver_calculator = (bloc: string[][], part2 = false): number => {
let index = 0
let i = 0
while (++i < bloc.length) {
let diff = 0,
u = i - 1,
d = i
let busted = false
while (u > -1 && d < bloc.length) {
let j = -1
while (++j < bloc[0].length) {
if (bloc[u][j] != bloc[d][j]) {
diff += 1
}
if (diff > 1) {
busted = true
break
}
}
if (busted) {
break
}
u -= 1
d += 1
}
if ((!part2 && diff == 0) || (part2 && diff == 1)) {
index = i
break
}
}
return index
}