- commit
- 6743a7fe4a6cc500a9c1d980bcafc344b0abbeb9
- parent
- ea72176167c2c061bbd5df61cd7e0e985fa922e5
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-12-08 15:35
2024-12-08
Diffstat
A | 2024/08/input.txt | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2024/08/solution.rs | 80 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2024/08/test.txt | 12 | ++++++++++++ |
3 files changed, 142 insertions, 0 deletions
diff --git a/2024/08/input.txt b/2024/08/input.txt
@@ -0,0 +1,50 @@ -1 1 ..f........................8...................... -1 2 G............8..u................................. -1 3 ........G...p..................................... -1 4 ......d.....................n..................... -1 5 .................................................. -1 6 .....................................K............ -1 7 ..................F...8.n...B.K................... -1 8 ....b.........8..u................................ -1 9 ...............F.p.......B.............4.....5.... -1 10 ....f..d..U.........................c............. -1 11 ...........U.....d.n.u.0................5......... -1 12 ...Y.......f..........................5........... -1 13 ..........................u.....d.....e.........4. -1 14 ..F...p.............v........n.................... -1 15 ....s.............0............................... -1 16 ...US.s....g.....D..........................4..... -1 17 ......wG...............S.......................... -1 18 .............................B.....e.............. -1 19 .........w.........................A.............. -1 20 .............9w.g..........................4...... -1 21 U....9..g.....v.....P....D.....f.K................ -1 22 .s.............0..9........pP..........5.......... -1 23 ..9s...................P.......................... -1 24 .............b..................0.....A..2....e... -1 25 ....................b.....V..v.................... -1 26 .......7........B......................A.......... -1 27 ..................D6...V....q..................... -1 28 ...v............D....PV........................... -1 29 .........Y...........g.......................e..y. -1 30 .......SW......V..7....................c.......... -1 31 .......bY7.....................N........A......... -1 32 .....................q.N..........y............... -1 33 ........................N.........c............... -1 34 .................................................. -1 35 .........C..7..................q........2......... -1 36 ............................N...q................. -1 37 ...W......C3...Q................a1.........y...... -1 38 .......W.......................3......2........... -1 39 ........3...........6.............1............... -1 40 ....3............C.1....................k......... -1 41 E..................................a.....c........ -1 42 .............................................w.... -1 43 ..S.......................Q..........2......k..... -1 44 ......................C....6.......Q......ak...... -1 45 .................................................. -1 46 .................E.............a1............y.... -1 47 W..........E...................................... -1 48 ......E...........6...........Q................... -1 49 ...........................k...................... -1 50 ..................................................
diff --git a/2024/08/solution.rs b/2024/08/solution.rs
@@ -0,0 +1,80 @@ -1 1 #[path = "../lib.rs"] -1 2 mod lib; -1 3 -1 4 fn parse_input() -> ([Vec<(usize, usize)>; 256], usize, usize) { -1 5 let mut x = 0; -1 6 let mut y = 0; -1 7 let mut antennas = core::array::from_fn(|_| vec![]); -1 8 for line in lib::iter_input() { -1 9 x = 0; -1 10 for b in line.bytes() { -1 11 if b != b'.' { -1 12 antennas[b as usize].push((x, y)); -1 13 } -1 14 x += 1; -1 15 } -1 16 y += 1; -1 17 } -1 18 return (antennas, x, y); -1 19 } -1 20 -1 21 fn gcd(a: usize, b: usize) -> usize { -1 22 let mut x = a.max(b); -1 23 let mut y = a.min(b); -1 24 while y > 0 { -1 25 (x, y) = (y, x % y); -1 26 } -1 27 return x; -1 28 } -1 29 -1 30 fn main() { -1 31 let mut sum1 = 0; -1 32 let mut sum2 = 0; -1 33 -1 34 let (antennas, w, h) = parse_input(); -1 35 let mut points1 = vec![vec![false; w]; h]; -1 36 let mut points2 = vec![vec![false; w]; h]; -1 37 -1 38 for set in antennas.iter() { -1 39 for i in 0..set.len() { -1 40 for j in 0..set.len() { -1 41 if i != j { -1 42 let (x1, y1) = set[i]; -1 43 let (x2, y2) = set[j]; -1 44 -1 45 if 2 * x1 >= x2 && 2 * x1 < w + x2 && 2 * y1 >= y2 && 2 * y1 < h + y2 { -1 46 // p3 = (1 - (-1)) * p1 + (-1) * p2 -1 47 let x3 = 2 * x1 - x2; -1 48 let y3 = 2 * y1 - y2; -1 49 if !points1[y3][x3] { -1 50 points1[y3][x3] = true; -1 51 sum1 += 1; -1 52 } -1 53 } -1 54 -1 55 let d = gcd(x1.abs_diff(x2), y1.abs_diff(y2)); -1 56 for t in 0.. { -1 57 if (d + t) * x1 < t * x2 -1 58 || (d + t) * y1 < t * y2 -1 59 || (d + t) * x1 >= w * d + t * x2 -1 60 || (d + t) * y1 >= h * d + t * y2 -1 61 { -1 62 break; -1 63 } -1 64 -1 65 // p3 = (1 - (-t/d)) * p1 + (-t/d) * p2 -1 66 let x3 = ((d + t) * x1 - t * x2) / d; -1 67 let y3 = ((d + t) * y1 - t * y2) / d; -1 68 if !points2[y3][x3] { -1 69 points2[y3][x3] = true; -1 70 sum2 += 1; -1 71 } -1 72 } -1 73 } -1 74 } -1 75 } -1 76 } -1 77 -1 78 println!("part1: {}", sum1); -1 79 println!("part2: {}", sum2); -1 80 }
diff --git a/2024/08/test.txt b/2024/08/test.txt
@@ -0,0 +1,12 @@ -1 1 ............ -1 2 ........0... -1 3 .....0...... -1 4 .......0.... -1 5 ....0....... -1 6 ......A..... -1 7 ............ -1 8 ............ -1 9 ........A... -1 10 .........A.. -1 11 ............ -1 12 ............