Skip to content

Commit 162ef5a

Browse files
committed
Day 10
1 parent 420e1a3 commit 162ef5a

File tree

4 files changed

+239
-0
lines changed

4 files changed

+239
-0
lines changed

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ day9:
4242
npx ts-node day9.ts
4343
day9-2:
4444
npx ts-node day9-2.ts
45+
46+
day10:
47+
npx ts-node day10.ts
48+
day10-2:
49+
npx ts-node day10-2.ts

day10-2.ts

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import * as fs from "fs"
2+
import { fileURLToPath } from "url"
3+
4+
const abs = Math.abs
5+
6+
function flip<T>(a: T[][]) {
7+
const flipped = Array.from(Array(a[0].length)).map(() => Array.from(Array(a.length)))
8+
for (let x = 0; x < flipped.length; x++) {
9+
for (let y = 0; y < flipped[0].length; y++) {
10+
flipped[x][y] = a[y][x]
11+
}
12+
}
13+
return flipped
14+
}
15+
16+
function print<T>(a: T[][]) {
17+
for (let y = 0; y < a[0].length; y++) {
18+
console.log(Array(a.length).fill(0).map((_, x) => a[x][y]).join(""))
19+
}
20+
}
21+
22+
function gcd(a: number, b: number) {
23+
if (a == 0) return abs(b)
24+
if (b == 0) return abs(a)
25+
26+
let h: number
27+
do {
28+
h = a % b
29+
a = b
30+
b = h
31+
} while (b != 0)
32+
return abs(a)
33+
}
34+
35+
function cancel(f: [number, number]) {
36+
let [a, b] = f
37+
let d: number
38+
do {
39+
d = gcd(a, b)
40+
a /= d
41+
b /= d
42+
} while (d !== 1)
43+
return [a, b]
44+
}
45+
46+
//const input = ".#....#####...#..\n##...##.#####..##\n##...#...#.#####.\n..#.....X...###..\n..#.#.....#....##"
47+
const input = fs.readFileSync("input-day10.txt", "utf8")
48+
49+
const image = flip(input
50+
.split("\n")
51+
.map(l => l.split("")))
52+
53+
const w = image.length
54+
const h = image[0].length
55+
56+
const ds = new Set<string>()
57+
for (let x = -w + 1; x < w; x++) {
58+
for (let y = -h + 1; y < h; y++) {
59+
if (x === 0 && y === 0) {
60+
continue
61+
}
62+
ds.add(JSON.stringify(cancel([x, y])))
63+
}
64+
}
65+
66+
const dirs = Array.from(ds).map(d => JSON.parse(d) as [number, number])
67+
.map(d => [...d, Math.atan2(d[1], d[0]) * 180 / Math.PI - 270])
68+
.map(d => [d[0], d[1], (2 * 360 + d[2]) % 360])
69+
.sort((a, b) => a[2] - b[2])
70+
.map(d => [d[0], d[1]] as const)
71+
72+
function shoot(x: number, y: number, d: readonly [number, number]): [number, number] | undefined {
73+
do {
74+
x += d[0]
75+
y += d[1]
76+
if (x < 0 || x >= w) {
77+
return undefined
78+
}
79+
if (y < 0 || y >= h) {
80+
return undefined
81+
}
82+
if (image[x][y] == "#") {
83+
image[x][y] = "x"
84+
return [x, y]
85+
}
86+
} while (true)
87+
}
88+
89+
let p = [17, 23]
90+
//let p = [8, 3]
91+
let count = 0
92+
let i = 0
93+
94+
while (true) {
95+
const d = dirs[i++ % dirs.length]
96+
const shot = shoot(p[0], p[1], d)
97+
if (shot) {
98+
count++
99+
print(image)
100+
}
101+
if (count == 200) {
102+
console.log(shot)
103+
console.log("---")
104+
break
105+
}
106+
}
107+

day10.ts

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import * as fs from "fs"
2+
3+
const abs = Math.abs
4+
5+
function flip<T>(a: T[][]) {
6+
const flipped = Array.from(Array(a[0].length)).map(() => Array.from(Array(a.length)))
7+
for (let x = 0; x < flipped.length; x++) {
8+
for (let y = 0; y < flipped[0].length; y++) {
9+
flipped[x][y] = a[y][x]
10+
}
11+
}
12+
return flipped
13+
}
14+
15+
function print<T>(a: T[][]) {
16+
for (let y = 0; y < a[0].length; y++) {
17+
console.log(Array(a.length).fill(0).map((_, x) => a[x][y]).join(""))
18+
}
19+
}
20+
21+
function gcd(a: number, b: number) {
22+
if (a == 0) return abs(b)
23+
if (b == 0) return abs(a)
24+
25+
let h: number
26+
do {
27+
h = a % b
28+
a = b
29+
b = h
30+
} while (b != 0)
31+
return abs(a)
32+
}
33+
34+
function cancel(f: [number, number]) {
35+
let [a, b] = f
36+
let d: number
37+
do {
38+
d = gcd(a, b)
39+
a /= d
40+
b /= d
41+
} while (d !== 1)
42+
return [a, b]
43+
}
44+
45+
const input = fs.readFileSync("input-day10.txt", "utf8")
46+
47+
const image = flip(input
48+
.split("\n")
49+
.map(l => l.split("")))
50+
51+
const w = image.length
52+
const h = image[0].length
53+
54+
const ds = new Set<string>()
55+
for (let x = -w + 1; x < w; x++) {
56+
for (let y = -h + 1; y < h; y++) {
57+
if (x === 0 && y === 0) {
58+
continue
59+
}
60+
ds.add(JSON.stringify(cancel([x, y])))
61+
}
62+
}
63+
64+
const dirs = Array.from(ds).map(d => JSON.parse(d))
65+
66+
function count(x: number, y: number): number {
67+
if (image[x][y] != "#") {
68+
return 0
69+
}
70+
71+
return dirs.map(d => countDir(x, y, d)).reduce((a, v) => a + v, 0)
72+
}
73+
74+
function countDir(x: number, y: number, d: [number, number]): number {
75+
do {
76+
x += d[0]
77+
y += d[1]
78+
if (x < 0 || x >= w) {
79+
return 0
80+
}
81+
if (y < 0 || y >= h) {
82+
return 0
83+
}
84+
if (image[x][y] == "#") {
85+
return 1
86+
}
87+
} while (true)
88+
}
89+
90+
let best = [0, -1, -1]
91+
for (let x = 0; x < w; x++) {
92+
for (let y = 0; y < h; y++) {
93+
const c = count(x, y)
94+
if (c > best[0]) {
95+
best = [c, x, y]
96+
}
97+
}
98+
}
99+
print(image)
100+
console.log(best)

input-day10.txt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
.#.#####.##...#...#.##...#.

0 commit comments

Comments
 (0)