Initial commit. Day 1 solved

This commit is contained in:
Franchioping 2025-12-04 20:40:13 +00:00
commit 2c7db5c4a0
6 changed files with 4183 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build/
.cache/

22
Makefile Normal file
View File

@ -0,0 +1,22 @@
# Advent of Code C Makefile
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -g
SRCS = $(wildcard src/day*.c)
TARGETS = $(SRCS:src/%.c=build/%)
.PHONY: all
all: $(TARGETS)
@echo "Compilation complete. Executables: $(TARGETS)"
build/%: src/%.c
$(CC) $(CFLAGS) $< -o $@
.PHONY: clean
clean:
rm -f $(TARGETS)
@echo "Cleaned up all Advent of Code executables."
.PHONY: all clean

25
flake.lock Normal file
View File

@ -0,0 +1,25 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1763948260,
"narHash": "sha256-dY9qLD0H0zOUgU3vWacPY6Qc421BeQAfm8kBuBtPVE0=",
"rev": "1c8ba8d3f7634acac4a2094eef7c32ad9106532c",
"revCount": 813095,
"type": "tarball",
"url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.2505.813095%2Brev-1c8ba8d3f7634acac4a2094eef7c32ad9106532c/019ab6d8-0005-7317-844d-5d868444249f/source.tar.gz"
},
"original": {
"type": "tarball",
"url": "https://flakehub.com/f/NixOS/nixpkgs/0"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

26
flake.nix Normal file
View File

@ -0,0 +1,26 @@
{
description = "A Nix-flake-based C/C++ development environment";
inputs.nixpkgs.url =
"https://flakehub.com/f/NixOS/nixpkgs/0"; # stable Nixpkgs
outputs = { self, ... }@inputs:
let
supportedSystems =
[ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
forEachSupportedSystem = f:
inputs.nixpkgs.lib.genAttrs supportedSystems
(system: f { pkgs = import inputs.nixpkgs { inherit system; }; });
in {
devShells = forEachSupportedSystem ({ pkgs }: {
default = pkgs.mkShell.override {
# stdenv = pkgs.clangStdenv;
} {
packages = with pkgs;
[ clang-tools clang cmake ]
++ (if system == "aarch64-darwin" then [ ] else [ gdb ]);
};
});
};
}

4046
inputs/day1.txt Normal file

File diff suppressed because it is too large Load Diff

62
src/day1.c Normal file
View File

@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 64
char line[MAX_LINE + 1];
int part1();
int part2();
int main() {
// printf("Result: %d", part1());
printf("Result: %d", part2());
return 0;
}
int part2() {
int position = 50;
int count = 0;
while (fgets(line, MAX_LINE, stdin) != NULL) {
position = position % 100;
line[strlen(line) - 1] = '\0';
int direction = 0;
if (line[0] == 'L') {
direction = -1;
} else {
direction = 1;
}
int offset = direction * atoi(&line[1]);
int full_rot = offset / 100;
int part_rot = offset - (100 * full_rot);
count += abs(full_rot) + ((part_rot < 0 && abs(part_rot) > position) ||
(part_rot > 0 && (100 - position) <= part_rot));
position += offset;
}
return count;
}
int part1() {
int position = 50;
int count = 0;
while (fgets(line, MAX_LINE, stdin) != NULL) {
line[strlen(line) - 1] = '\0';
int direction = 0;
if (line[0] == 'L') {
direction = -1;
} else {
direction = 1;
}
int offset = direction * atoi(&line[1]);
position += offset;
count += (position % 100 == 0);
}
printf("part1 count: %d\n", count);
return count;
}