Skip to content

Commit 68516b1

Browse files
committed
AOC 2021: Solution for day 09
1 parent a88b0cf commit 68516b1

File tree

3 files changed

+487
-6
lines changed

3 files changed

+487
-6
lines changed

2015/day_06/puzzle.cpp

+76-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,89 @@
11
//
2-
// Copyright (C) Martin Beaudoin. 2019. All Rights Reserved.
2+
// Copyright (C) Martin Beaudoin. 2020. All Rights Reserved.
33
//
44
// See the repository's LICENSE file for the full license details.
55
//
66

77
#include <iostream>
88
#include <cassert>
99
#include <vector>
10+
#include <sstream> // std::stringstream
1011
#include "myutils.h"
1112

1213
using namespace std;
1314

15+
typedef enum
16+
{
17+
ON,
18+
OFF,
19+
TOGGLE
20+
} Command;
21+
22+
class Instruction
23+
{
24+
public:
25+
Command command; // Command
26+
myutils::point c1; // Corner points
27+
myutils::point c2;
28+
29+
// Default constructor
30+
Instruction() {};
31+
32+
// Copy constructor
33+
Instruction(const Instruction &b)
34+
{
35+
command = b.command;
36+
c1 = b.c1;
37+
c2 = b.c2;
38+
};
39+
40+
};
41+
1442
// Solve puzzle #1
1543
template <typename T>
1644
constexpr int solve_puzzle1(T data)
1745
{
46+
vector<Instruction> instructions(0);
47+
48+
for(auto d : data)
49+
{
50+
Instruction instr;
51+
52+
cout << d << endl;
53+
54+
size_t last_index = d.find_first_of("0123456789");
55+
string result = d.substr(last_index - 1);
56+
cout << result << endl;
57+
58+
myutils::point c1, c2; // Corner points
59+
string c_scrap;
60+
char sep;
61+
std::stringstream ss(d.substr(last_index - 1));
62+
ss >> instr.c1.x >> sep >> instr.c1.y
63+
>> c_scrap
64+
>> instr.c2.x >> sep >> instr.c2.y;
65+
66+
if(d.find("turn on") != string::npos)
67+
{
68+
instr.command = ON;
69+
}
70+
else if(d.find("turn off") != string::npos)
71+
{
72+
instr.command = OFF;
73+
}
74+
else if(d.find("toggle") != string::npos)
75+
{
76+
instr.command = TOGGLE;
77+
}
78+
else
79+
{
80+
cout << " Error: bad command : " << d << endl;
81+
}
82+
83+
instructions.push_back(instr);
84+
}
85+
86+
bool grid[1000][1000];
1887
return 42;
1988
}
2089

@@ -43,20 +112,21 @@ int main(int argc, char *argv[])
43112
}
44113

45114
// Reading the data
46-
auto data = myutils::read_file<int, std::vector<int> >(filename);
115+
vector<string> data;
116+
myutils::read_file(data, filename, true, false);
47117

48118
// --------- Puzzle #1 ---------
49119
// Verify puzzle1 examples
50-
const auto example1 = 1;
51-
assert(solve_puzzle1<vector<int>>({example1}) == 42 && "Error verifying puzzle #1");
120+
//const auto example1 = "1";
121+
//assert(solve_puzzle1<vector<string>>({example1}) == 42 && "Error verifying puzzle #1");
52122

53123
// Solve puzzle #1
54124
std::cout << "Answer for puzzle #1: "<< solve_puzzle1(data) << std::endl;
55125

56126
// --------- Puzzle #2 ---------
57127
// Verify puzzle2 examples
58-
const auto example2 = 2;
59-
assert(solve_puzzle2<vector<int>>({example2}) == 42 && "Error verifying puzzle #2");
128+
const auto example2 = "2";
129+
assert(solve_puzzle2<vector<string>>({example2}) == 42 && "Error verifying puzzle #2");
60130

61131
// Solve puzzle #2
62132
std::cout << "Answer for puzzle #2: "<< solve_puzzle2(data) << std::endl;

2021/day_09/Makefile

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Compilation variables
2+
CXX=g++
3+
CPPFLAGS= -I../include
4+
CXXFLAGS= -std=c++17 -O2 $(CPPFLAGS)
5+
6+
# wget command and args to retrieve the day's input data
7+
# using the input session cookie from ${HOME}/.aoc_session_cookie
8+
WGET=wget
9+
WGET_ARGS=--load-cookies ${HOME}/.aoc_session_cookie
10+
11+
# Extract the day number from the current directory name
12+
CUR_DIR:=$(notdir $(CURDIR))
13+
DAY_NUM:=$(subst day_0, , $(CUR_DIR))
14+
DAY_NUM:=$(strip $(subst day_, , $(DAY_NUM)))
15+
16+
# AOC event year
17+
AOC_EVENT_YEAR=2021
18+
19+
# AOC day's input datafile name
20+
INPUT_DATAFILE=input.txt
21+
22+
# Input datafile URL
23+
AOC_INPUT_URL=https://adventofcode.com/$(AOC_EVENT_YEAR)/day/$(DAY_NUM)/input
24+
25+
SRCS = puzzle.cpp
26+
OBJS = $(SRCS:.cpp=.o)
27+
28+
all: getdata puzzle run
29+
30+
puzzle: $(OBJS)
31+
@echo "Compiling puzzle"
32+
$(CXX) -o puzzle $(CXXFLAGS) $(OBJS)
33+
34+
run: getdata puzzle
35+
@echo "Solving puzzle"
36+
./puzzle $(INPUT_DATAFILE)
37+
38+
getdata:
39+
@echo "Getting data for day # $(DAY_NUM)"
40+
test -f $(INPUT_DATAFILE) || $(WGET) $(WGET_ARGS) -O $(INPUT_DATAFILE) $(AOC_INPUT_URL)
41+
clean:
42+
@echo "Cleaning up"
43+
rm -f puzzle $(OBJS) *~ $(INPUT_DATAFILE) Makefile.bak
44+
45+
.cpp.o:
46+
$(CXX) $(CXXFLAGS) -o $@ -c $<
47+
48+
depend:
49+
makedepend -- $(CPPFLAGS) -- $(SRCS)

0 commit comments

Comments
 (0)