aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-01-10 19:34:39 +0100
committerThomas Schmucker <ts@its1.de>2025-01-10 19:34:39 +0100
commit5f92ea475dadc173b2172834227f9392fd7d5a7c (patch)
tree1313db95ed27ccaa44d3a825efad96c369fb05bf
parent8f53e809e312021596be70317ea053e5bec6c816 (diff)
downloadadvent-of-code-5f92ea475dadc173b2172834227f9392fd7d5a7c.tar.gz
advent-of-code-5f92ea475dadc173b2172834227f9392fd7d5a7c.tar.bz2
advent-of-code-5f92ea475dadc173b2172834227f9392fd7d5a7c.zip
aoc 2016, day 1
-rw-r--r--2016/data/.gitkeep0
-rw-r--r--2016/src/day01.cpp122
-rw-r--r--makefile7
3 files changed, 128 insertions, 1 deletions
diff --git a/2016/data/.gitkeep b/2016/data/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/2016/data/.gitkeep
diff --git a/2016/src/day01.cpp b/2016/src/day01.cpp
new file mode 100644
index 0000000..f5d74a5
--- /dev/null
+++ b/2016/src/day01.cpp
@@ -0,0 +1,122 @@
1#include <array>
2#include <fstream>
3#include <iostream>
4#include <set>
5#include <string>
6#include <tuple>
7#include <vector>
8
9using namespace std;
10
11vector<string>
12split(string_view line, string_view delimiter)
13{
14 size_t pos_start = 0;
15 size_t pos_end = 0;
16
17 vector<string> res;
18
19 while ( (pos_end = line.find(delimiter, pos_start)) != string::npos ) {
20 auto token = line.substr(pos_start, pos_end - pos_start);
21 pos_start = pos_end + delimiter.length();
22
23 res.emplace_back(token);
24 }
25
26 if ( pos_start != line.size() ) {
27 res.emplace_back(line.substr(pos_start));
28 }
29 return res;
30}
31
32vector<tuple<char, long>>
33read_file(string_view filename)
34{
35 fstream input{ filename };
36 string content{ istreambuf_iterator<char>{ input }, istreambuf_iterator<char>{} };
37
38 vector<tuple<char, long>> result;
39 for ( const auto& part: split(content, ", ") ) {
40 result.emplace_back(part[0], stol(part.substr(1)));
41 }
42 return result;
43}
44
45void
46part1(const vector<tuple<char, long>>& input)
47{
48 array<tuple<long, long>, 4> dirs = {
49 make_tuple(1L, 0L),
50 make_tuple(0L, 1L),
51 make_tuple(-1L, 0L),
52 make_tuple(0L, -1L)
53 };
54
55 size_t dir = 0;
56 long pos_x = 0;
57 long pos_y = 0;
58
59 for ( const auto& [change, length]: input ) {
60 if ( change == 'L' ) {
61 dir = (dir + 1) % 4;
62 }
63 else {
64 dir = (dir + 3) % 4;
65 }
66
67 auto [delta_x, delta_y] = dirs.at(dir);
68
69 pos_x += length * delta_x;
70 pos_y += length * delta_y;
71 }
72
73 cout << abs(pos_x) + abs(pos_y) << endl;
74}
75
76void
77part2(const vector<tuple<char, long>>& input)
78{
79 array<tuple<long, long>, 4> dirs = {
80 make_tuple(1L, 0L),
81 make_tuple(0L, 1L),
82 make_tuple(-1L, 0L),
83 make_tuple(0L, -1L)
84 };
85
86 size_t dir = 0;
87 long pos_x = 0;
88 long pos_y = 0;
89
90 set<tuple<long, long>> visited;
91
92 for ( auto [change, length]: input ) {
93 if ( change == 'L' ) {
94 dir = (dir + 1) % 4;
95 }
96 else {
97 dir = (dir + 3) % 4;
98 }
99
100 auto [delta_x, delta_y] = dirs.at(dir);
101
102 while ( length-- > 0 ) {
103 pos_x += delta_x;
104 pos_y += delta_y;
105
106 if ( visited.contains({ pos_x, pos_y }) ) {
107 cout << abs(pos_x) + abs(pos_y) << endl;
108 return;
109 }
110
111 visited.emplace(pos_x, pos_y);
112 }
113 }
114}
115
116int
117main()
118{
119 auto input = read_file("data/day01.txt");
120 part1(input);
121 part2(input);
122}
diff --git a/makefile b/makefile
index 928c1bb..5ce2c9a 100644
--- a/makefile
+++ b/makefile
@@ -1,6 +1,7 @@
1include config.mk 1include config.mk
2 2
3all: $(patsubst 2015/src/%.cpp,2015/bin/%,$(wildcard 2015/src/*.cpp)) \ 3all: $(patsubst 2015/src/%.cpp,2015/bin/%,$(wildcard 2015/src/*.cpp)) \
4 $(patsubst 2016/src/%.cpp,2016/bin/%,$(wildcard 2016/src/*.cpp)) \
4 $(patsubst 2020/src/%.cpp,2020/bin/%,$(wildcard 2020/src/*.cpp)) \ 5 $(patsubst 2020/src/%.cpp,2020/bin/%,$(wildcard 2020/src/*.cpp)) \
5 $(patsubst 2022/src/%.cpp,2022/bin/%,$(wildcard 2022/src/*.cpp)) \ 6 $(patsubst 2022/src/%.cpp,2022/bin/%,$(wildcard 2022/src/*.cpp)) \
6 $(patsubst 2023/src/%.cpp,2023/bin/%,$(wildcard 2023/src/*.cpp)) \ 7 $(patsubst 2023/src/%.cpp,2023/bin/%,$(wildcard 2023/src/*.cpp)) \
@@ -16,6 +17,10 @@ all: $(patsubst 2015/src/%.cpp,2015/bin/%,$(wildcard 2015/src/*.cpp)) \
162015/bin/day04: 2015/src/day04.cpp | 2015/bin 172015/bin/day04: 2015/src/day04.cpp | 2015/bin
17 c++ $(CPPFLAGS) $^ -lmd -o $@ 18 c++ $(CPPFLAGS) $^ -lmd -o $@
18 19
20# 2016
212016/bin/%: 2016/src/%.cpp | 2016/bin
22 c++ $(CPPFLAGS) $^ -o $@
23
19# 2020 24# 2020
202020/bin/%: 2020/src/%.cpp | 2020/bin 252020/bin/%: 2020/src/%.cpp | 2020/bin
21 c++ $(CPPFLAGS) $^ -o $@ 26 c++ $(CPPFLAGS) $^ -o $@
@@ -39,6 +44,6 @@ all: $(patsubst 2015/src/%.cpp,2015/bin/%,$(wildcard 2015/src/*.cpp)) \
39 c++ $(CPPFLAGS) $^ -L/usr/local/lib -lz3 -o $@ 44 c++ $(CPPFLAGS) $^ -L/usr/local/lib -lz3 -o $@
40 45
41clean: 46clean:
42 rm -rf 2015/bin 2020/bin 2022/bin 2023/bin 2024/bin 47 rm -rf 2015/bin 2016/bin 2020/bin 2022/bin 2023/bin 2024/bin
43 48
44.PHONY: all clean 49.PHONY: all clean