aboutsummaryrefslogtreecommitdiffstats
path: root/src/01/main.c
blob: 6acbe11d4236aa4da058043b6cba4e3dc4efabe8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "aoc.h"
#include "util.h"

#include <err.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>

void
part1(void)
{
	char buf[256];
	const char *pos, *end;
	int64_t fuel, ans;

	pos = aoc.input;
	end = aoc.input + aoc.input_size;

	ans = 0;
	while (readtok(buf, sizeof(buf), '\n', &pos, end)) {
		fuel = strtoll(buf, NULL, 0);
		ans += fuel / 3 - 2;
	}

	aoc.answer = aprintf("%llu", ans);
	aoc.solution = "3286680";
}

void
part2(void)
{
	char buf[256];
	const char *pos, *end;
	int64_t fuel, ans;

	pos = aoc.input;
	end = aoc.input + aoc.input_size;

	ans = 0;
	while (readtok(buf, sizeof(buf), '\n', &pos, end)) {
		fuel = strtoll(buf, NULL, 0);
		while ((fuel = fuel / 3 - 2) > 0) {
			ans += fuel;
		}
	}

	aoc.answer = aprintf("%llu", ans);
	aoc.solution = "4927158";
}