advent2024

Advent of Code 2024
git clone git://bsandro.tech/advent2024
Log | Files | Refs

commit 2cd8129387019cd02e58cc2fb6c774b059f2588e
parent fefd390804d34ac89197d30d464e427df833a85b
Author: bsandro <email@bsandro.tech>
Date:   Sun,  8 Dec 2024 19:39:26 +0200

Day 08 p1

Diffstat:
Aday08/Makefile | 28++++++++++++++++++++++++++++
Aday08/input.txt | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Aday08/main.cpp | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday08/test1.txt | 12++++++++++++
4 files changed, 191 insertions(+), 0 deletions(-)

diff --git a/day08/Makefile b/day08/Makefile @@ -0,0 +1,28 @@ +NAME=$(shell basename ${PWD}) +SRC=$(wildcard *.cpp) +DEPS:=$(wildcard *.hpp) +OBJ:=$(SRC:.cpp=.o) +CXXFLAGS=-O2 -std=c++23 -Werror -Wall -Wextra -I. +LDFLAGS=-lstdc++ + +all: $(NAME) + +.PHONY: clean run + +clean: + rm -f $(OBJ) $(NAME) + +%.o : %.c $(DEPS) + @$(CC) $(CFLAGS) -c $< -o $@ + +$(NAME): $(OBJ) + @$(CC) $(OBJ) -o $@ $(LDFLAGS) + +run: $(NAME) + @./$(NAME) input.txt + +test1: $(NAME) + @./$(NAME) test1.txt + +test2: $(NAME) + @./$(NAME) test2.txt diff --git a/day08/input.txt b/day08/input.txt @@ -0,0 +1,50 @@ +...............................s.................. +..................s..............q.............p.. +.....a............................................ +........c......Y.......Q.......................... +............................................4..... +........Y.........y............m..........4....... +......................Y...s..........S............ +.........................................S........ +...............N.............y.................... +...........a.......y..................1........... +................................................S. +...c........k.............q....t............S..... +.............................qM................... +........a......................................... +.................................................. +.................................................. +..c..........k...Q..q....P........................ +5.................Q...................8........... +......yc.......................................... +........................E............4............ +.........6........................u..p.....4...... +.........5.............P..n......1.........N...... +6..............................1.........J.t...... +..6..................................3.u..t.....p. +....5...k..........................u.............. +.......................E..................u....x.. +..................E.................x............. +...k..................P.............3............. +...........0.....9.5...........E.........31e....N. +......0.................................N......... +.................CU.....................t....x.... +......7....................e...................... +....0..........K......C........................... +.....6....j......M............................J... +......K.................................p......... +.....9........................U................... +............................3....n................ +.............K.........2.....C..................x. +....................P........UJ................... +.....0......X...C.........T..............U........ +.......M.....8j....7.............2........Q....... +9...............K................................. +....e.....8.........................2.A.m......... +..e......8.........s...n.......................... +.....................................T..nm........ +...................X............2.........m......A +......................X..j....................T... +.........7..M......j.............T................ +....9...7....................................A.... +..........................................A....... diff --git a/day08/main.cpp b/day08/main.cpp @@ -0,0 +1,101 @@ +#include <iostream> +#include <filesystem> +#include <fstream> +#include <vector> +#include <cstring> +#include <cstdio> +#include <tuple> +#include <algorithm> +#include <map> +#include <set> + +typedef std::pair<int, int> Vec2; +typedef std::map<char, std::vector<Vec2>> Data; + +// I got too lazy these days +[[ maybe_unused ]] static int sFieldSize = 0; + +std::vector<Vec2> calc_an(Vec2 a1, Vec2 a2) { + std::vector<Vec2> out; + Vec2 d1(a1.first-a2.first, a1.second-a2.second); + Vec2 d2(-d1.first, -d1.second); + Vec2 p1(a1.first+d1.first, a1.second+d1.second); + Vec2 p2(a2.first+d2.first, a2.second+d2.second); + if (p1.first>=0&&p1.second>=0&&p1.first<sFieldSize&&p1.second<sFieldSize) { + out.push_back(p1); + } + if (p2.first>=0&&p2.second>=0&&p2.first<sFieldSize&&p2.second<sFieldSize) { + out.push_back(p2); + } + return out; +} + +std::set<Vec2> calc_antinodes(const std::vector<Vec2> &antennas) { + std::set<Vec2> out; + for (auto it1=antennas.begin(); it1!=antennas.end(); it1++) { + for (auto it2=it1+1; it2!=antennas.end(); it2++) { + auto [x1, y1] = *it1; + auto [x2, y2] = *it2; + std::printf("%d:%d -> %d:%d\n", x1, y1, x2, y2); + std::vector<Vec2> an = calc_an(*it1, *it2); + for (auto &v:an) out.insert(v); + } + } + return out; +} + +template<typename T> +T read_file(const std::string &path) { + std::ifstream ifs(path, std::ios::binary); + if (!ifs.is_open()) { + throw std::runtime_error(path+":"+std::strerror(errno)); + } + T buf; + int y=0; + while (1) { + std::string str; + std::getline(ifs, str); + if (!str.empty()) { + for (int x=0; x<(int)str.size(); ++x) { + if (str[x]!='.') buf[str[x]].push_back(Vec2(x, y)); + } + } + ++y; + if (!ifs) break; + } + sFieldSize = y-1; + return buf; +} + +int part1(Data &input [[ maybe_unused ]]) { + std::set<Vec2> out; + std::cout << std::endl; + for (auto &[k, v] : input) { + std::cout << "key:" << k << std::endl; + for (auto &p : v) { + std::cout << "[" << p.first << "," << p.second << "]"; + } + std::cout << std::endl; + std::set<Vec2> an = calc_antinodes(v); + std::printf("%ld antinodes:", an.size()); + for (auto &n:an) { + std::printf("[%d,%d]", n.first, n.second); + out.insert(n); + } + std::cout << std::endl; + } + return (int)out.size(); +} +int part2(Data &input [[ maybe_unused ]]) { + return 0; +} + +int main(int argc, char **argv) { + const std::string fname = argc>1 ? argv[1] : "test1.txt"; + std::cout << "AoC 2024 day 08 " << fname << std::endl; + Data input = read_file<Data>(fname); + std::cout << "part1: " << part1(input) << std::endl; + std::cout << "part2: " << part2(input) << std::endl; + + return 0; +} diff --git a/day08/test1.txt b/day08/test1.txt @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............