-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10_part1.jakt
146 lines (122 loc) · 4.47 KB
/
day10_part1.jakt
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
134
135
136
137
138
139
140
141
142
143
144
145
146
/// Expect:
/// - output: ".####...######....##....#....#..######..#....#..#....#.....###\n#....#..#........#..#...##...#..#.......#....#..#...#.......#.\n#.......#.......#....#..##...#..#.......#....#..#..#........#.\n#.......#.......#....#..#.#..#..#.......#....#..#.#.........#.\n#.......#####...#....#..#.#..#..#####...######..##..........#.\n#..###..#.......######..#..#.#..#.......#....#..##..........#.\n#....#..#.......#....#..#..#.#..#.......#....#..#.#.........#.\n#....#..#.......#....#..#...##..#.......#....#..#..#....#...#.\n#...##..#.......#....#..#...##..#.......#....#..#...#...#...#.\n.###.#..#.......#....#..#....#..######..#....#..#....#...###..\n"
import reader
import utility
struct CurrentPosition {
x: i32
y: i32
}
struct Velocity {
x: i32
y: i32
}
class Point {
public current_position: CurrentPosition
public velocity: Velocity
}
function main() {
mut points: [Point] = []
for line in reader::lines_string(day: 10).iterator() {
let numbers = utility::format_match(line, format: "position=< %, %> velocity=< %, %>")
if numbers.has_value() and numbers!.size() == 4 {
let point = Point(
current_position: CurrentPosition(
x: numbers![0]
y: numbers![1]
)
velocity: Velocity(
x: numbers![2]
y: numbers![3]
)
)
points.push(point)
}
}
mut last_canvas_size: i64? = None
for i in 0..100000 {
for point in points.iterator() {
point.current_position.x += point.velocity.x
point.current_position.y += point.velocity.y
}
let canvas_size = get_canvas_size(points)
if not last_canvas_size.has_value() {
last_canvas_size = canvas_size
} else if canvas_size.has_value() {
if canvas_size! > last_canvas_size! {
// go back 1 iteration and print
for point in points.iterator() {
point.current_position.x -= point.velocity.x
point.current_position.y -= point.velocity.y
}
print_points(points)
break
} else {
last_canvas_size = canvas_size
}
}
}
}
function get_canvas_size(points: [Point]) throws -> i64? {
mut plot: [i32:[i32:i32]] = [:]
mut min_x: i32? = None
mut min_y: i32? = None
mut max_y: i32? = None
mut max_x: i32? = None
for point in points.iterator() {
if not min_x.has_value() or point.current_position.x < min_x! {
min_x = point.current_position.x
}
if not min_y.has_value() or point.current_position.y < min_y! {
min_y = point.current_position.y
}
if not max_x.has_value() or point.current_position.x > max_x! {
max_x = point.current_position.x
}
if not max_y.has_value() or point.current_position.y > max_y! {
max_y = point.current_position.y
}
}
if (max_x! - min_x!) < 2000 and (max_y! - min_y!) < 2000 {
return Some(((max_x! - min_x!) * (max_y! - min_y!)) as! i64)
}
return None
}
function print_points(points: [Point]) throws {
mut min_x: i32? = None
mut min_y: i32? = None
mut max_y: i32? = None
mut max_x: i32? = None
mut plot: [i32:[i32:i32]] = [:]
for point in points.iterator() {
if not min_x.has_value() or point.current_position.x < min_x! {
min_x = point.current_position.x
}
if not min_y.has_value() or point.current_position.y < min_y! {
min_y = point.current_position.y
}
if not max_x.has_value() or point.current_position.x > max_x! {
max_x = point.current_position.x
}
if not max_y.has_value() or point.current_position.y > max_y! {
max_y = point.current_position.y
}
if not plot.contains(point.current_position.x) {
plot[point.current_position.x] = [:]
}
plot[point.current_position.x][point.current_position.y] = 1
}
for y in min_y!..(max_y!+1) {
for x in min_x!..(max_x!+1) {
if plot.contains(x) {
if plot[x].contains(y) {
print("#")
} else {
print(".")
}
} else {
print(".")
}
}
print("\n")
}
}