-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6pt1.ts
133 lines (99 loc) · 2.99 KB
/
day6pt1.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
let input = await Deno.readTextFile('./day6input.txt')
// input = `....#.....
// .........#
// ..........
// ..#.......
// .......#..
// ..........
// .#..^.....
// ........#.
// #.........
// ......#...`
const parsedInput = input.split(/\n/)
// console.log(parsedInput)
const dirs = {
up: [ -1, 0 ],
down: [ 1, 0 ],
left: [ 0, -1 ],
right: [ 0, 1 ]
}
const guardChars = new Map([['^', 'up'], ['>', 'right'], ['v', 'down'], ['<', 'left']])
const rows = parsedInput.length
const cols = parsedInput[0].length
console.log(rows, cols, rows*cols)
function getGuardInfo(){
let guardPos = []
let guardDirChar
// let obstacles = new Map()
parsedInput.forEach((row, i) => {
row.split('').forEach((cell, j) => {
if (guardChars.has(cell)){
// console.log(cell, i, j)
guardDirChar = cell
guardPos = [i, j]
}
// if (cell == '#')
// if(obstacles.get[i])
// obstacles.set(i, obstacles.get[i].push(j))
// else
// obstacles.set(i, [j])
})
})
// console.log(obstacles)
return [guardPos, guardChars.get(guardDirChar)]
}
function getNextPosition(position = [0, 0], direction='up'){
let nextRowCoord = position[0] + dirs[direction][0]
let nextColCoord = position[1] + dirs[direction][1]
return [nextRowCoord, nextColCoord]
}
function isBlocked(nextpos){
return parsedInput[nextpos[0]][nextpos[1]] === '#' ||
parsedInput[nextpos[0]][nextpos[1]] === 'O'
}
function isOutbounds(nextpos){
return !parsedInput[nextpos[0]] || !parsedInput[nextpos[0]][nextpos[1]]
}
function getNewDirection(position, direction) {
const guardDirsOrder = ['up','right', 'down', 'left']
let dirIndex = guardDirsOrder.indexOf(direction)
dirIndex = dirIndex + 1
if(dirIndex == guardDirsOrder.length)
dirIndex = 0
return guardDirsOrder[dirIndex]
}
function getDirection(position = [0, 0], direction='down'){
let nextpos = getNextPosition(position, direction)
if (!isOutbounds(nextpos) && isBlocked(nextpos)) {
const newDir = getNewDirection(position, direction)
return [getNextPosition(position, newDir), newDir]
}
return [nextpos, direction]
}
function simulateWalkingPath(guardPos, guardDir){
let direction = guardDir
let position = guardPos
let visitedSpotsHash = new Map()
let visitedSpots = []
console.log(position, direction, isBlocked(getNextPosition(position, direction)))
// simulate guard's path
while (true) {
[position, direction] = getDirection(position, direction)
// console.log(position)
if(isOutbounds(position)) break
visitedSpots.push(position)
const hashKey = position[0]+','+position[1]
if (!visitedSpotsHash.has(hashKey)){
visitedSpotsHash.set(hashKey, 1)
}
}
console.log(position, visitedSpotsHash.size)
console.assert(visitedSpotsHash.size === 5269, 'Value should be 5269')
return [position, visitedSpotsHash]
}
// get guard dir and pos
// check if there is any obstacle in front of guard
// mark the unique visited spots
// if its blocked, then turn right
let [guardPos, guardDir] = getGuardInfo()
simulateWalkingPath(guardPos, guardDir)