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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
use std::{vec, fs};
#[derive(Copy, Clone, Debug)]
struct Coord(u32, u32, u32);
fn main() {
let mut numbers: Vec<u32> = vec![];
let mut array: Vec<Vec<char>> = vec![];
let f = fs::read_to_string("input").unwrap();
// Convert file to array
for line in f.lines() {
let xline: Vec<char> = line.chars().collect();
array.push(xline);
}
// No longer mutable
let array = array;
// Make an array the same size for the purpose of number coord tracking and matching
// initialize it with -1 for nothing there
let mut numarray: Vec<Vec<i32>> = vec![vec![-1; array[0].len()]; array.len()];
// Find Numbers
let mut num = false;
let mut numcoords = Coord(0, 0, 0);
let mut yidx = 0;
for y in array.clone() {
let mut xidx = 0;
for x in y {
match x.to_string().parse::<u32>() {
Ok(_) => {
// if num is not set, start recording the number
if !num {
numcoords = Coord(yidx, xidx, 0);
num = true;
}
},
Err(_) => {
// if we were making a number
if num {
num = false;
// handle number being at the end of the line
// and xidx wraps back to 0
if xidx == 0 {
numcoords = Coord(numcoords.0, numcoords.1, (array[0].len() -1) as u32);
} else {
numcoords = Coord(numcoords.0, numcoords.1, xidx - 1);
}
numbers.push(range_to_number(numcoords, &array));
// Set numarray coords to the numidx
let numidx = numbers.len() - 1;
for i in numcoords.1..=numcoords.2 {
numarray[numcoords.0 as usize][i as usize] = numidx as i32;
}
}
},
}
xidx += 1;
}
yidx += 1;
}
// Search for all symbols that are not numbers or '.' and search around them in numarray for anything not -1
const AROUND: [(i8, i8); 8] = [
(0, 1),
(1, 1),
(1, 0),
(-1, 0),
(-1, -1),
(0, -1),
(1, -1),
(-1, 1),
];
let mut outvec = vec![];
let mut gears = 0;
let mut yidx = 0;
for y in array {
let mut xidx = 0;
for c in y {
let ctest = match c.to_string().parse::<u32>() {
Ok(_) => true,
Err(_) => false,
};
// if ctest is a number or '.', then continue
if ctest || c == '.' {
xidx += 1;
continue
}
// Character is a symbol
let mut newgearvec = vec![];
for pos in AROUND {
let i = match numarray.get(u32_calc(yidx, pos.0)) {
Some(n) => {
match n.get(u32_calc(xidx, pos.1)) {
Some(n) => n,
None => &-1,
}
},
None => continue
};
if i > &-1 {
outvec.push(*i)
}
// part two, if also a gear
if i > &-1 && c == '*' {
newgearvec.push(*i);
}
}
// process newgearvec
newgearvec.sort();
newgearvec.dedup();
if newgearvec.len() == 2 {
println!("{:?}", newgearvec);
gears += numbers[newgearvec[0] as usize] * numbers[newgearvec[1] as usize];
}
xidx += 1;
}
yidx += 1;
}
// Get only a single of each number and add them all up
outvec.sort();
outvec.dedup();
let outvec: Vec<u32> = outvec.iter().map(|i| {
match numbers.get(*i as usize) {
Some(n) => *n,
None => 0,
}
}).collect();
// Add It Up
let mut final_num = 0;
for x in outvec {
final_num += x;
}
println!("part one:{}", final_num);
println!("part two:{}", gears);
}
fn u32_calc(x: u32, y: i8) -> usize {
// nothing should matter if x is greater than 0
if x < 1 && y == -1 {
0
} else {
(x as i64 + y as i64) as usize
}
}
fn range_to_number(coords: Coord, array: &Vec<Vec<char>>) -> u32 {
let mut outstring = "".to_string();
for i in coords.1..=coords.2 {
outstring.push(*array.get(coords.0 as usize).unwrap().get(i as usize).unwrap())
}
outstring.parse().unwrap()
}
|