Skip to content

Commit

Permalink
Add initial code for day 5 part 2 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmorris180 committed Jan 10, 2024
1 parent 605b753 commit a49f809
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class Day02Challenge implements Challenge {

private static final Logger logger = LoggerFactory.getLogger(Day01Challenge.class);
private static final Logger logger = LoggerFactory.getLogger(Day02Challenge.class);
private static final Map<String, Integer> MAX_VALUES = Map.of("red", 12, "green", 13, "blue", 14);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class Day03Challenge implements Challenge {

private static final Logger logger = LoggerFactory.getLogger(Day01Challenge.class);
private static final Logger logger = LoggerFactory.getLogger(Day03Challenge.class);
private static final List<List<Integer>> DIRECTIONS =
List.of(
List.of(-1, -1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class Day04Challenge implements Challenge {

private static final Logger logger = LoggerFactory.getLogger(Day01Challenge.class);
private static final Logger logger = LoggerFactory.getLogger(Day04Challenge.class);
private static final String REGEX = "[\\s]{2,}";

@Override
Expand Down
53 changes: 46 additions & 7 deletions src/main/java/com/jonathanmorris/challenges/Day05Challenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class Day05Challenge implements Challenge {

private static final Logger logger = LoggerFactory.getLogger(Day01Challenge.class);
private static final Logger logger = LoggerFactory.getLogger(Day05Challenge.class);
private static final String MAP = "map";
private static final List<String> MAPS =
List.of(
Expand Down Expand Up @@ -96,6 +96,7 @@ public class Day05Challenge implements Challenge {
public void execute(List<String> lines) {
List<Long> seeds =
Arrays.stream(lines.get(0).split(":")[1].trim().split(" ")).map(Long::valueOf).toList();

Map<String, GardenerMap> gardenerMaps = new HashMap<>();
List<Long> locations = new ArrayList<>();
String mapName = "";
Expand Down Expand Up @@ -129,6 +130,26 @@ public void execute(List<String> lines) {
}
logger.debug("locations are {}", locations);
logger.debug("Answer for part one is {}", locations.stream().reduce(Long.MAX_VALUE, Long::min));
this.executePartTwo(seeds, gardenerMaps);
}

private void executePartTwo(List<Long> seeds, List<GardenerMap> gardenerMaps) {
List<List<Long>> initialSeedRanges = new ArrayList<>();

for (int i = 0; i < seeds.size(); i++) {
if (i % 2 == 0) {
initialSeedRanges.add(new ArrayList<>(List.of(seeds.get(i))));
} else {
initialSeedRanges.get(initialSeedRanges.size() - 1).add(seeds.get(i));
}
}

for (List<Long> initialSeedRange : initialSeedRanges) {
for (String map : MAPS) {
String currentDest = map.split("to-")[1];
logger.info("getting value for {}", currentDest);
GardenerMap gardenerMap = gardenerMaps.get(map);
}
}

private class GardenerMap {
Expand All @@ -153,19 +174,13 @@ private Long getValue(Long val) {
Long sourceNum = line.getSourceNum();
Long destNum = line.getDestNum();
Long rangeLength = line.getRangeLength();
logger.debug("sourceNum is {}", sourceNum);
logger.debug("destNum is {}", destNum);
logger.debug("rangeLength is {}", rangeLength);

if (val >= sourceNum && val < sourceNum + rangeLength) {
Long offset = val - sourceNum;
logger.debug("offset is {}", offset);
Long result = destNum + offset;
logger.debug("result is {}", result);
return result;
}
}
logger.debug("result is {}", val);
return val;
}
}
Expand Down Expand Up @@ -194,4 +209,28 @@ private Long getDestNum() {
return this.destNum;
}
}

private class Range {
private Long start;
private Long range;
private Long displacement;

private Range(Long start, Long range, Long displacement) {
this.start = start;
this.range = range;
this.displacement = displacement;
}

private Long getStart() {
return this.start;
}

private Long getRange() {
return this.range;
}

private Long getDisplacement() {
return this.displacement;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class Day06Challenge implements Challenge {

private static final Logger logger = LoggerFactory.getLogger(Day01Challenge.class);
private static final Logger logger = LoggerFactory.getLogger(Day06Challenge.class);
private static final String REGEX = "[\\s]{2,}";

@Override
Expand Down

0 comments on commit a49f809

Please sign in to comment.