Skip to content

Commit 266c557

Browse files
committed
09 refactor
1 parent da16e91 commit 266c557

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/09/solve.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,18 @@ class Head extends Point {
3838
class Tail extends Point {
3939
history: string[] = []
4040
head: Point
41-
constructor(x: number, y: number, head: Point) {
41+
withHistory: boolean
42+
constructor(x: number, y: number, head: Point, withHistory = false) {
4243
super(x, y)
4344
this.head = head
45+
this.withHistory = withHistory
4446
this.addToHistory(this.x, this.y)
4547
}
4648

4749
addToHistory = (x: number, y: number) => {
50+
if (!this.withHistory) {
51+
return
52+
}
4853
const key = x + "_" + y
4954
if (this.history.includes(key)) {
5055
return
@@ -53,6 +58,9 @@ class Tail extends Point {
5358
}
5459

5560
addToHistoryRange = (a1: number, a2: number, b: number, xrange: boolean) => {
61+
if (!this.withHistory) {
62+
return
63+
}
5664
const diff = a2 - a1
5765
const step = Math.sign(diff)
5866
let i = a1
@@ -77,11 +85,8 @@ class Tail extends Point {
7785
const diffY = this.head.y - y
7886
const diffX = this.head.x - x
7987

80-
if (Math.abs(diffX) === 1 && diffY === 0
81-
|| Math.abs(diffY) === 1 && diffX === 0
82-
|| diffX === 0 && diffY === 0
83-
|| Math.abs(diffX) === 1 && Math.abs(diffY) === 1) {
84-
break
88+
if ((diffX === 0 || Math.abs(diffX) === 1) && (diffY === 0 || Math.abs(diffY) === 1)) {
89+
break;
8590
}
8691
if (diffY === 0) {
8792
const tmp = x
@@ -119,7 +124,7 @@ class Solve09 extends FileReader {
119124

120125
process1 = (data: string[]) => {
121126
const head = new Head(0, 0)
122-
const tail = new Tail(0, 0, head)
127+
const tail = new Tail(0, 0, head, true)
123128
data.forEach(row => {
124129
const data = row.split(' ');
125130
head.move(data[0] as Dir, parseInt(data[1], 10))
@@ -134,15 +139,19 @@ class Solve09 extends FileReader {
134139
const size = 9
135140
let last: Point = head
136141
for (let i=0;i<size;i++) {
137-
const tail = new Tail(0, 0, last)
142+
const tail = new Tail(0, 0, last, i === size -1)
138143
tails.push(tail)
139144
last = tail
140145
}
141146

142147
data.forEach(row => {
143148
const data = row.split(' ');
144-
head.move(data[0] as Dir, parseInt(data[1], 10))
145-
tails.forEach(t => t.moveToHead())
149+
const moves = parseInt(data[1], 10)
150+
const dir = data[0] as Dir
151+
for (let i=0;i<moves;i++) {
152+
head.move(dir, 1)
153+
tails.forEach(t => t.moveToHead())
154+
}
146155
})
147156
console.log((last as Tail).history.length)
148157
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy