From 86745e1d2c9f2e19135a30b4a78684dde480708a Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sun, 12 Dec 2021 07:17:21 +0100 Subject: [PATCH] day12: defaultdicts ... TIL --- day12.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/day12.py b/day12.py index d418af9..52b5be1 100644 --- a/day12.py +++ b/day12.py @@ -1,19 +1,13 @@ +from collections import defaultdict from tools.aoc import AOCDay from typing import Any, Dict, List def getCaveMap(lines: List[List[str]]) -> Dict[str, List[str]]: - m = {} - for l in lines: - if l[0] in m: - m[l[0]].append(l[1]) - else: - m[l[0]] = [l[1]] - - if l[1] in m: - m[l[1]].append(l[0]) - else: - m[l[1]] = [l[0]] + m = defaultdict(list) + for l, r in lines: + m[l].append(r) + m[r].append(l) return m