-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday 03 redo.cpp
96 lines (83 loc) · 2.1 KB
/
day 03 redo.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// day3_redo.cpp : Defines the entry point for the console application.
// Advent of Code 2017
// http://adventofcode.com/
#include <iostream>
#include <string>
#include <limits>
#include <vector>
#include <map>
typedef enum dir {e=0,n,w,s} direction;
direction next_dir[] = {n,w,s,e};
direction d=e;
int x=0, y=0, count=1, done=0;
// kinda ugly generator of spiral coords
void next_coord(int& x, int&y)
{
x += d==e ? 1 : (d==w) ? -1 : 0; // east or west
y += d==n ? 1 : (d==s) ? -1 : 0; // north or south
if (++done==count) done=0; // moved required number of spots
if (!done && (d==n || d==s)) count++; // bump count on n or s
if (!done) d = next_dir[d]; // switch direction
}
// get a value and iterate through the spiral, counting the x,y
// distance is then just abs(x)+abs(y)
int main1(int argc, char* argv[])
{
std::cout << "Enter value (-1 to quit part 1)" << std::endl;
int val;
while (1) {
std::cin >> val;
if (val==-1) break;
for (int i=1; i<val; ++i) {
next_coord(x,y);
}
std::cout << "Distance is " << abs(x)+abs(y) << std::endl;
}
return 0;
}
// track spiral totals in part 2
std::map<std::string,int> sum;
// x,y -> "x,y"
std::string idx(int x,int y)
{
std::string idx = std::to_string((long long)x);
idx.push_back(',');
idx.append(std::to_string((long long)y));
return idx;
}
// lookup a sum at cell x,y
int lookup(int x, int y)
{
std::string id = idx(x,y);
if (sum.find(id)!=sum.end())
return sum[id];
return 0;
}
// sum the 8 neighbors of x,y and save it
int save_sum(int x,int y)
{
int tot=lookup(x-1,y-1)+lookup(x,y-1)+lookup(x+1,y-1)+lookup(x-1,y+1)+lookup(x,y+1)+lookup(x+1,y+1)+lookup(x-1,y)+lookup(x+1,y);
return (sum[idx(x,y)]=tot);
}
int main2(int argc, char* argv[])
{
sum[idx(0,0)] = 1;
std::cout << "Enter value (-1 to quit part 2)" << std::endl;
int val,sum;
while (1) {
std::cin >> val;
if (val==-1) break;
d=e, x=0, y=0, count=1, done=0;
do {
next_coord(x,y);
sum = save_sum(x,y);
} while (sum<=val);
std::cout << "Value is " << sum << std::endl;
}
return 0;
}
int main(int argc, char* argv[])
{
main1(argc,argv);
main2(argc,argv);
}