Skip to content

Commit 486ee2b

Browse files
committed
2018 06 (rs) part 1
1 parent 381ed7d commit 486ee2b

File tree

4 files changed

+366
-0
lines changed

4 files changed

+366
-0
lines changed

2018/Cargo.lock

+82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2018/day-06/Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "day-06"
3+
version = "0.0.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
anyhow = "1.0.66"
8+
chrono = "0.4.23"
9+
colored = "2.0.0"
10+
itertools = "0.10.5"
11+
lazy_static = "1.4.0"
12+
num = "0.4.0"
13+
regex = "1.7.0"
14+
15+
[dev-dependencies]
16+
pretty_assertions = "1.3.0"

2018/day-06/input

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
336, 308
2+
262, 98
3+
352, 115
4+
225, 205
5+
292, 185
6+
166, 271
7+
251, 67
8+
266, 274
9+
326, 85
10+
191, 256
11+
62, 171
12+
333, 123
13+
160, 131
14+
211, 214
15+
287, 333
16+
231, 288
17+
237, 183
18+
211, 272
19+
116, 153
20+
336, 70
21+
291, 117
22+
156, 105
23+
261, 119
24+
216, 171
25+
59, 343
26+
50, 180
27+
251, 268
28+
169, 258
29+
75, 136
30+
305, 102
31+
154, 327
32+
187, 297
33+
270, 225
34+
190, 185
35+
339, 264
36+
103, 301
37+
90, 92
38+
164, 144
39+
108, 140
40+
189, 211
41+
125, 157
42+
77, 226
43+
177, 168
44+
46, 188
45+
216, 244
46+
346, 348
47+
272, 90
48+
140, 176
49+
109, 324
50+
128, 132

2018/day-06/src/main.rs

+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
#![allow(dead_code, unused_imports, unused_variables)]
2+
3+
use colored::Colorize;
4+
use itertools::Itertools;
5+
use num::Complex;
6+
use std::fs;
7+
8+
fn main() {
9+
let input = "`1, 1
10+
1, 6
11+
8, 3
12+
3, 4
13+
5, 5
14+
8, 9
15+
`"
16+
.replace('`', "");
17+
let input = read_and_trim_input();
18+
19+
let lines = get_lines(&input);
20+
// dbg!(&lines);
21+
22+
let coords = lines
23+
.iter()
24+
.map(|lines| {
25+
lines
26+
.split(',')
27+
.map(|s| s.trim().parse().unwrap())
28+
.collect_tuple::<(i32, i32)>()
29+
.unwrap()
30+
})
31+
.collect_vec();
32+
// dbg!(&coords);
33+
34+
let part_1a = {
35+
let min_x = *coords.iter().map(|(x, y)| x).min().unwrap();
36+
// let min_x = 0;
37+
let max_x = *coords.iter().map(|(x, y)| x).max().unwrap();
38+
// let max_x = 9;
39+
let min_y = *coords.iter().map(|(x, y)| y).min().unwrap();
40+
// let min_y = 0;
41+
let max_y = *coords.iter().map(|(x, y)| y).max().unwrap();
42+
// let max_y = 9;
43+
// dbg!(min_x, max_x, min_y, max_y);
44+
45+
let closest_coord_by_location = (min_x..=max_x)
46+
.cartesian_product(min_y..=max_y)
47+
.map(|l| (l, closest_coord(&coords, l)))
48+
.collect_vec();
49+
// dbg!(&closest_coord_by_location);
50+
51+
// print(
52+
// &closest_coord_by_location,
53+
// &coords,
54+
// min_y,
55+
// max_y,
56+
// min_x,
57+
// max_x,
58+
// );
59+
60+
let closest_coord_by_location_larger = (min_x - 1..=max_x + 1)
61+
.cartesian_product(min_y - 1..=max_y + 1)
62+
.map(|l| (l, closest_coord(&coords, l)))
63+
.collect_vec();
64+
65+
let counts = closest_coord_by_location
66+
.iter()
67+
.filter_map(|&cc| cc.1)
68+
.counts();
69+
// dbg!(&counts);
70+
71+
let counts_larger = closest_coord_by_location_larger
72+
.iter()
73+
.filter_map(|&cc| cc.1)
74+
.counts();
75+
// dbg!(&counts_larger);
76+
let same = counts
77+
.iter()
78+
.filter(|&c| counts_larger.get(c.0).unwrap() == c.1)
79+
.collect_vec();
80+
// dbg!(&same);
81+
82+
let safest = same.iter().max_by_key(|&s| s.1).unwrap();
83+
// dbg!(&safest);
84+
85+
safest.1.to_owned()
86+
};
87+
println!("{}: {}", stringify!(part_1a), part_1a);
88+
89+
let part_2a = "?";
90+
println!("{}: {}", stringify!(part_2a), part_2a);
91+
}
92+
93+
fn print(
94+
closest_coord_by_location: &[((i32, i32), Option<(i32, i32)>)],
95+
coords: &[(i32, i32)],
96+
min_y: i32,
97+
max_y: i32,
98+
min_x: i32,
99+
max_x: i32,
100+
) {
101+
let prepare_print = closest_coord_by_location
102+
.iter()
103+
.map(|&(l, c)| {
104+
let char = get_char_for_location(c, coords, l);
105+
(l, char)
106+
})
107+
.collect_vec();
108+
// dbg!(&prepare_print);
109+
110+
// Känns knöligt, vore det bättre att ha koordinaterna i en array av array?
111+
println!();
112+
for y in min_y..=max_y {
113+
for x in min_x..=max_x {
114+
let char = &prepare_print.iter().find(|&pp| pp.0 == (x, y)).unwrap().1;
115+
print!("{}", char);
116+
}
117+
println!();
118+
}
119+
println!();
120+
}
121+
122+
fn get_char_for_location(c: Option<(i32, i32)>, coords: &[(i32, i32)], l: (i32, i32)) -> String {
123+
match c {
124+
Some(sc) => {
125+
let letters = ('a'..='z').collect_vec();
126+
let position = coords.iter().position(|&cc| cc == sc).unwrap();
127+
let char = if position < letters.len() {
128+
letters[position]
129+
} else {
130+
'?'
131+
};
132+
if l == sc {
133+
char.to_ascii_uppercase().to_string().bold().to_string()
134+
} else {
135+
char.to_string()
136+
}
137+
}
138+
None => '.'.to_string(),
139+
}
140+
}
141+
142+
fn closest_coord(coords: &[(i32, i32)], l: (i32, i32)) -> Option<(i32, i32)> {
143+
let coord_dists = coords
144+
.iter()
145+
.map(|&c| (c, manhattan_distance(l, c)))
146+
.collect_vec();
147+
let closest_coord_dist = &coord_dists.iter().min_by_key(|&(c, d)| d).unwrap();
148+
let closest_coords = coord_dists
149+
.iter()
150+
.filter(|&&(c, d)| d == closest_coord_dist.1)
151+
.collect_vec();
152+
153+
if closest_coords.len() == 1 {
154+
Some(closest_coords[0].0)
155+
} else {
156+
None
157+
}
158+
}
159+
160+
fn read_and_trim_input() -> String {
161+
// Kanske inte ha trim här?
162+
let file = fs::read_to_string("input").unwrap();
163+
file.trim().to_owned()
164+
}
165+
166+
fn get_lines(s: &str) -> Vec<&str> {
167+
s.trim_matches('`')
168+
.split_terminator('\n')
169+
.map(|x| x.trim())
170+
.filter(|x| !x.is_empty())
171+
.collect_vec()
172+
}
173+
174+
fn manhattan_distance((x1, y1): (i32, i32), (x2, y2): (i32, i32)) -> i32 {
175+
num::abs(x2 - x1) + num::abs(y2 - y1)
176+
}
177+
178+
// fn manhattan_distance(a: Complex<i32>, b: Complex<i32>) -> i32 {
179+
// num::abs(a.re - b.re) + num::abs(a.im - b.im)
180+
// }
181+
182+
#[cfg(test)]
183+
mod tests {
184+
use super::*;
185+
use pretty_assertions::assert_eq;
186+
187+
// #[test]
188+
// fn manhattan_distance_test() {
189+
// assert_eq!(
190+
// manhattan_distance(Complex::new(1, 1), Complex::new(5, 10)),
191+
// 4 + 9
192+
// );
193+
// }
194+
195+
#[test]
196+
fn part_1_examples() {
197+
assert_eq!(1, 1);
198+
}
199+
200+
#[test]
201+
fn part_1_input() {
202+
let input = read_and_trim_input();
203+
204+
assert_eq!(1, 1);
205+
}
206+
207+
#[test]
208+
fn part_2_examples() {
209+
assert_eq!(1, 1);
210+
}
211+
212+
#[test]
213+
fn part_2_input() {
214+
let input = read_and_trim_input();
215+
216+
assert_eq!(1, 1);
217+
}
218+
}

0 commit comments

Comments
 (0)