Browse Source

day 4

master
mid-kid 2 years ago
parent
commit
ddf2eaaeac
  1. 2
      Makefile
  2. 58
      d04.cc
  3. 12
      d04.py
  4. 1000
      d04_input.txt
  5. 2
      d04_output.txt
  6. 31
      template.cc

2
Makefile

@ -1,6 +1,6 @@
CXXFLAGS := -std=c++11 -Wall -Wextra -O2
progs := $(patsubst %.cc,%,$(wildcard *.cc))
progs := $(patsubst %.cc,%,$(wildcard d*.cc))
.PHONY: all
all: $(addsuffix _output.txt,$(progs))

58
d04.cc

@ -0,0 +1,58 @@
#include <iostream>
#include <vector>
#include <array>
using namespace std;
class Range {
public:
unsigned min;
unsigned max;
bool contains(const Range &in) {
return min <= in.min && max >= in.max;
}
bool overlaps(const Range &in) {
return (min <= in.min && in.min <= max)
|| (min <= in.max && in.max <= max)
|| (in.min <= min && min <= in.max)
|| (in.min <= max && max <= in.max);
}
};
typedef vector<array<Range, 2>> Input;
Input parse()
{
Input data;
for (string line; getline(cin, line);) {
array<Range, 2> entry;
sscanf(line.c_str(), "%u-%u,%u-%u",
&entry[0].min, &entry[0].max,
&entry[1].min, &entry[1].max);
data.push_back(entry);
}
return data;
}
unsigned p1(const Input &input)
{
unsigned count = 0;
for (auto i : input) if (i[0].contains(i[1]) || i[1].contains(i[0])) count++;
return count;
}
unsigned p2(const Input &input)
{
unsigned count = 0;
for (auto i : input) if (i[0].overlaps(i[1])) count++;
return count;
}
int main()
{
Input input = parse();
cout << p1(input) << endl;
cout << p2(input) << endl;
}

12
d04.py

@ -0,0 +1,12 @@
#!/usr/bin/env python3
from sys import stdin
data = [(range(int(a1), int(a2)+1), range(int(b1), int(b2)+1))
for (a1, a2), (b1, b2) in [(a.split("-"), b.split("-"))
for a, b in [x.split(",") for x in stdin.readlines()]]]
print(sum(all(x in b for x in a) or all(x in a for x in b) for a, b in data))
print(sum(bool(set(a) & set(b)) for a, b in data))
for a, b in data:
if set(a) & set(b):
print((a,b))

1000
d04_input.txt

File diff suppressed because it is too large

2
d04_output.txt

@ -0,0 +1,2 @@
599
928

31
template.cc

@ -0,0 +1,31 @@
#include <iostream>
#include <vector>
using namespace std;
typedef vector<unsigned> Input;
Input parse()
{
Input data;
return data;
}
unsigned p1(const Input &input)
{
(void)input;
return 0;
}
unsigned p2(const Input &input)
{
(void)input;
return 0;
}
int main()
{
Input input = parse();
cout << p1(input) << endl;
cout << p2(input) << endl;
}
Loading…
Cancel
Save