|
| 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