summaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2024-12-08 10:56:25 +0100
committernekineki <nekineki@nekineki.net>2024-12-08 10:56:25 +0100
commit88fa6b22f8546a5f10658eb84951fd744b67aa98 (patch)
treef4ee903da1f418315d7cc5cad50cd69981381cb4 /2024
parent15117b94410f53379957bd29d3953f7ed96ba774 (diff)
day08
Diffstat (limited to '2024')
-rw-r--r--2024/day08.rs99
-rw-r--r--2024/in/day08.pzl50
-rw-r--r--2024/in/day08.ref12
3 files changed, 161 insertions, 0 deletions
diff --git a/2024/day08.rs b/2024/day08.rs
new file mode 100644
index 0000000..30a1913
--- /dev/null
+++ b/2024/day08.rs
@@ -0,0 +1,99 @@
+#![allow(dead_code)]
+#![allow(unused_variables)]
+#![allow(unused_mut)]
+use itertools::Itertools;
+use std::collections::HashMap;
+use std::collections::HashSet;
+use std::env;
+use std::fs::File;
+use std::io::Read;
+
+fn get_antinodes(a: (i32, i32), b: (i32, i32)) -> Vec<(i32, i32)> {
+ let mut ans = Vec::new();
+ let (ax, ay) = (a.0, a.1);
+ let (bx, by) = (b.0, b.1);
+ let dx = bx - ax;
+ let dy = by - ay;
+ ans.push((bx + dx, by + dy));
+ ans.push((ax - dx, ay - dy));
+ return ans;
+}
+
+fn bounded(min: i32, max: i32, val: i32) -> bool {
+ min <= val && val < max
+}
+
+fn get_antinodes2(xmax: i32, ymax: i32, a: (i32, i32), b: (i32, i32)) -> Vec<(i32, i32)> {
+ let mut ans = Vec::new();
+ let (ax, ay) = (a.0, a.1);
+ let (bx, by) = (b.0, b.1);
+ let dx = bx - ax;
+ let dy = by - ay;
+
+ ans.push((ax, ay));
+ ans.push((bx, by));
+
+ for n in 1.. {
+ let (ux, uy) = (bx + n * dx, by + n * dy);
+ if !bounded(0, xmax, ux) || !bounded(0, ymax, uy) {
+ break;
+ }
+ ans.push((ux, uy));
+ }
+
+ for n in 1.. {
+ let (ux, uy) = (ax - n * dx, ay - n * dy);
+ if !bounded(0, xmax, ux) || !bounded(0, ymax, uy) {
+ break;
+ }
+ ans.push((ux, uy));
+ }
+ return ans;
+}
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ let filename = if args.len() == 1 {
+ "in/".to_owned() + args[0].split('/').last().unwrap() + ".pzl"
+ } else {
+ args[1].clone()
+ };
+ let mut f = File::open(filename).expect("cannot open file");
+ let mut content = String::new();
+ f.read_to_string(&mut content).expect("cannot read file");
+ let lines: Vec<_> = content.trim_end().split("\n").collect();
+
+ let ymax = lines.len() as i32;
+ let xmax = lines[0].len() as i32;
+
+ let mut hm: HashMap<char, Vec<(i32, i32)>> = HashMap::new();
+ for (y, line) in lines.iter().enumerate() {
+ for (x, c) in line.chars().enumerate() {
+ if c != '.' {
+ hm.entry(c).or_default().push((x as i32, y as i32));
+ }
+ }
+ }
+
+ let mut antinodes: HashSet<(i32, i32)> = HashSet::new();
+ let mut antinodes2: HashSet<(i32, i32)> = HashSet::new();
+ for (key, val) in hm.clone().into_iter() {
+ for (a, b) in val.clone().into_iter().tuple_combinations() {
+ for (x, y) in get_antinodes(a, b) {
+ if bounded(0, xmax, x) && bounded(0, ymax, y) {
+ antinodes.insert((x, y));
+ }
+ }
+ for (x, y) in get_antinodes2(xmax, ymax, a, b) {
+ antinodes2.insert((x, y));
+ }
+ }
+ }
+ let res1 = antinodes.len();
+ let res2 = antinodes2.len();
+
+ println!("res1: {}", res1);
+ println!("res2: {}", res2);
+ assert_eq!(res1, 293);
+ assert_eq!(res2, 934);
+}
diff --git a/2024/in/day08.pzl b/2024/in/day08.pzl
new file mode 100644
index 0000000..07030f2
--- /dev/null
+++ b/2024/in/day08.pzl
@@ -0,0 +1,50 @@
+.....................................O..V.........
+..................................................
+................................O.........Z.......
+....W....................................V....v...
+........................m................8........
+.....................................n........Z..v
+.............F.....3...n....5m....................
+................................................V.
+................3............iv....Z.............V
+...........................O..n..i........p......H
+......W..6..............................i.........
+......................................b...........
+..................................n........p......
+........M.......c...........m..5......1...........
+...M............................L..5..A...........
+...w...........9.............F5..................q
+.W.....................................q....p.....
+.......W........r.......H.....LA......q...........
+................4.F....................A..........
+........3.......a.....F...................A..L....
+....ME...............................Q..........q.
+.E..................ih...................Z........
+................E...H...........h.................
+.........m.........X..............................
+..................0......C.................h......
+.M......l.................Q.h.....................
+..........C..............0........................
+.............lX............3.c....................
+......8.X.........c....r..a......H.....9..........
+.................QE.....C.........................
+..R................a........Q...................7.
+...........................a......................
+l..........X.R............1..I..........9.........
+.................0R..............b.....z......x...
+.......l.....w....r..........................b....
+.8..........0...................P1z...............
+.............c.........................L..........
+.................C..N............o............9...
+...........e..f..N................................
+8.............................B...................
+...........4...............................x......
+....w....RY..........4.......................P....
+.........yw.....Y.............o2...............7..
+..6y........4..............fo..............7......
+.........Y..6............o......................x.
+.....Y....e.....y..I.r...........2................
+....e.............................P.......z.bB....
+.............6.................B........7......x..
+..y.N........f...........1....I....z....B.........
+.....e....f.............I.................2.......
diff --git a/2024/in/day08.ref b/2024/in/day08.ref
new file mode 100644
index 0000000..78a1e91
--- /dev/null
+++ b/2024/in/day08.ref
@@ -0,0 +1,12 @@
+............
+........0...
+.....0......
+.......0....
+....0.......
+......A.....
+............
+............
+........A...
+.........A..
+............
+............