aboutsummaryrefslogtreecommitdiff
path: root/1
blob: 514d500510bc324d864c4749537558f9ec889f79 (plain)
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
#!/bin/sh -eu
p1() (
	max=0
	temp=0
	while IFS= read -r l
	do
		if [ -z $l ]
		then
			if [ $temp -gt $max ]
			then
				max=$temp
			fi
			temp=0
		else
			temp=$((temp + l))
		fi
	done < input.txt
	printf "%s\n" "$max"
)

p2() (
	set -- 0 0 0
	temp=0
	while IFS= read -r l
	do
		# TODO: check EOF
		if [ -z $l ]
		then
			if [ $temp -gt $1 ]
			then
				shift
				set -- $@ $temp
				set -- $(printf "%s\n" "$@" | sort -n)
			fi
			temp=0
		else
			temp=$((temp + l))
		fi
	done < input.txt
	sum=0
	for i in $@
	do
		sum=$((sum + i))
	done
	printf "%s\n" "$sum"
)

p1
p2