~aperezdc/aoc2022

213adc59756c71a4cd491e98e4cbb6c3f44efe73 — Adrian Perez de Castro 2 years ago ced9f0c
Day 4, first part redux + second part
2 files changed, 16 insertions(+), 7 deletions(-)

M README.md
M day04/day04.krk
M README.md => README.md +1 -0
@@ 12,6 12,7 @@ Languages used
--------------

- [C11](https://en.wikipedia.org/wiki/C11)
- [Kuroko](https://kuroko-lang.github.io) (version 1.3.0)
- [Lua](https://www.lua.org/) (version 5.4)
- [Python](https://python.org) (version 3.10, most programs probably work all
  the way down to version 3.6)

M day04/day04.krk => day04/day04.krk +15 -7
@@ 2,14 2,22 @@

from fileio import stdin

let count = 0
def parse_line(line):
    let l, r = line.strip().split(",")
    let ll, lr = map(int, l.split("-"))
    let rl, rr = map(int, r.split("-"))
    return set(range(ll, lr + 1)), set(range(rl, rr + 1))

let count1, count2 = 0, 0

for line in stdin.readlines():
    let a, b = line.strip().split(",")
    let al, ar = map(int, a.split("-"))
    let bl, br = map(int, b.split("-"))
    let l, r = parse_line(line)
    let c = l & r

    if (al <= bl and br <= ar) or (bl <= al and ar <= br):
        count += 1
    if c == l or c == r:
        count1 += 1
    if len(c) > 0:
        count2 += 1

print(count)
print("Part 1:", count1)
print("Part 2:", count2)