feat: Day 2 !
This commit is contained in:
parent
c20eb0ca69
commit
6a9ed1c5a9
|
@ -0,0 +1,32 @@
|
|||
with open("data/input.txt", "r") as f:
|
||||
games = f.read()
|
||||
|
||||
limits = {
|
||||
"red": 12,
|
||||
"green": 13,
|
||||
"blue": 14
|
||||
}
|
||||
|
||||
s = 0
|
||||
|
||||
for game in games.split("\n")[:-1]:
|
||||
parts = game.split(":")
|
||||
head = parts[0]
|
||||
identifier = head.split(" ")[1]
|
||||
overflow = False
|
||||
for draw in parts[1].split(";"):
|
||||
draw = draw.strip()
|
||||
for count in draw.split(","):
|
||||
count = count.strip()
|
||||
count_parts = count.split(" ")
|
||||
color = count_parts[1]
|
||||
amount = int(count_parts[0])
|
||||
if amount > limits[color]:
|
||||
overflow = True
|
||||
break
|
||||
if overflow:
|
||||
break
|
||||
if not overflow:
|
||||
s += int(identifier)
|
||||
|
||||
print(s)
|
|
@ -0,0 +1,62 @@
|
|||
#!/usr/bin/env perl
|
||||
use warnings;
|
||||
use strict;
|
||||
|
||||
my $filename = '../data/input.txt';
|
||||
|
||||
open(FH, '<', $filename) or die $!;
|
||||
|
||||
sub possible_id {
|
||||
my $line = $_[0];
|
||||
my $reds = $_[1];
|
||||
my $greens = $_[2];
|
||||
my $blues = $_[3];
|
||||
my @colors = qw(red green blue);
|
||||
my @quantities = ($reds, $greens, $blues);
|
||||
# Extract the id and game draws
|
||||
my @game_parts = split /:/, $line, 2;
|
||||
my @id_parts = split / /, $game_parts[0], 2;
|
||||
my $id = $id_parts[1];
|
||||
my @game_plays = split /;/, $game_parts[1];
|
||||
foreach my $play (@game_plays) {
|
||||
my @color_counts = (0, 0, 0);
|
||||
my @play_draws = split /,/, $play;
|
||||
foreach my $draw (@play_draws) {
|
||||
my @draw_parts = split / /, $draw;
|
||||
my $color = $draw_parts[2];
|
||||
my $amount = int($draw_parts[1]);
|
||||
for ((0..2)) {
|
||||
my $idx = $_;
|
||||
my $limit_color = $colors[$idx];
|
||||
my $limit_amount = $quantities[$idx];
|
||||
if ($color eq $limit_color) {
|
||||
$color_counts[$idx] += $amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
# print join(", ", @color_counts) . "\n";
|
||||
for ((0..2)) {
|
||||
if ($quantities[$_] < $color_counts[$_]) {
|
||||
return 0;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return int($id);
|
||||
}
|
||||
|
||||
my $reds_limit = 12;
|
||||
my $greens_limit = 13;
|
||||
my $blues_limit = 14;
|
||||
|
||||
my $sum = 0;
|
||||
while (<FH>) {
|
||||
# print $_;
|
||||
my $id = possible_id($_, $reds_limit, $greens_limit, $blues_limit);
|
||||
$sum += $id;
|
||||
# print $id . "\n";
|
||||
}
|
||||
|
||||
close(FH);
|
||||
|
||||
print $sum . "\n";
|
|
@ -0,0 +1,31 @@
|
|||
|
||||
import numpy as np
|
||||
|
||||
with open("data/input.txt", "r") as f:
|
||||
games = f.read()
|
||||
|
||||
limits = {
|
||||
"red": 12,
|
||||
"green": 13,
|
||||
"blue": 14
|
||||
}
|
||||
|
||||
s = 0
|
||||
|
||||
for game in games.split("\n")[:-1]:
|
||||
parts = game.split(":")
|
||||
head = parts[0]
|
||||
identifier = head.split(" ")[1]
|
||||
max_counts = dict(red=0, green=0, blue=0)
|
||||
for draw in parts[1].split(";"):
|
||||
draw = draw.strip()
|
||||
for count in draw.split(","):
|
||||
count = count.strip()
|
||||
count_parts = count.split(" ")
|
||||
color = count_parts[1]
|
||||
amount = int(count_parts[0])
|
||||
if amount > max_counts[color]:
|
||||
max_counts[color] = amount
|
||||
s += np.prod(list(max_counts.values()))
|
||||
|
||||
print(s)
|
Loading…
Reference in New Issue