summary refs log tree commit diff homepage
path: root/2021/day12.pl
blob: 90f3e54ea4c2e1c2a247c4dff8bfca7021b1ac3f (plain) (blame)
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
use strict;
use warnings;
my %edges;
while (<>) {
	/(\w+)-(\w+)/;
	$edges{$1} //= [];
	$edges{$2} //= [];
	push @{$edges{$1}}, $2;
	push @{$edges{$2}}, $1;
}
my $paths = 0;
my @queue = (['start']);
while (@queue) {
	my @path = @{pop @queue};
	my %visited = map { $_ => 1 } @path;
	for (@{$edges{$path[0]}}) {
		if ($_ eq 'end') {
			$paths++;
		} elsif (/[a-z]/ && $visited{$_}) {
			next;
		} else {
			my @next = ($_, @path);
			push @queue, \@next;
		}
	}
}
print "$paths\n";
$paths = 0;
@queue = (['start']);
while (@queue) {
	my @path = @{pop @queue};
	my (%visited, $twice);
	for (@path) {
		$twice = 1 if $visited{$_}++ && /[a-z]/;
	}
	for (@{$edges{$path[0]}}) {
		next if $_ eq 'start';
		if ($_ eq 'end') {
			$paths++;
		} elsif (/[a-z]/ && $visited{$_} && $twice) {
			next;
		} else {
			my @next = ($_, @path);
			push @queue, \@next;
		}
	}
}
print "$paths\n";
ed9393b173853d2e960f9b176aeb0bc4ed35be2&follow=1'>Add submodule links in tree listingLars Hjemli 2007-05-11Add submodules.sh and use it during buildsLars Hjemli 2007-05-11Added git as a submoduleLars Hjemli 2007-05-09Add support for downloading single blobsLars Hjemli 2007-05-08ui-view: show pathname if specified in querystringLars Hjemli 2007-05-08Update to libgit 1.5.2-rc2Lars Hjemli 2007-02-21Layout updateLars Hjemli 2007-02-08Make snapshot feature configurableLars Hjemli 2007-02-08Add support for snapshotsLars Hjemli 2007-02-05cgit v0.2Lars Hjemli 2007-02-05Add support for prefix and gitsrc arguments to 'make'Lars Hjemli 2007-02-04Update cgitrc templateLars Hjemli 2007-02-04Add support for lightweight tagsLars Hjemli 2007-02-04Read repo-info from /etc/cgitrcLars Hjemli 2007-02-04Do not die if tag has no messageLars Hjemli 2007-02-03Fix search for non-virtual urlsLars Hjemli 2007-01-28Update README with install/config informationLars Hjemli